DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Asynchronous APIs Using Apache Camel

Asynchronous APIs Using Apache Camel

Take a look at a few different patterns for asynchronous transactions for APIs in Apache Camel.

Gnanaguru Sattanathan user avatar by
Gnanaguru Sattanathan
·
Jun. 28, 16 · Tutorial
Like (3)
Save
Tweet
Share
9.46K Views

Join the DZone community and get the full member experience.

Join For Free

Do you want to expose an asynchronous service/API in Apache Camel ? Of course, that's one core functionality Camel can provide, but there are multiple ways to implement it. It also depends on what kind of protocol/systems you are dealing with. (Also it's important to understand naturally whether that protocol is meant for Asynchronous or Synchronous transactions).

These are some of the patterns you can build, but it is not limited this alone.

1. HTTP Asynchronous / Hybrid Endpoint

Regardless of what Camel component you use for exposing an HTTP endpoint, the approach to expose an Async HTTP endpoint doesn’t change in most scenarios.

Using a wiretap endpoint, no matter what exchange pattern the upstream endpoints sets, wiretap will replace it with InOnly exchange pattern.

<from ref="http-endpoint" />
<to ref="transaction-id-generator-service" />
<to ref="xslt-transformer-generate-ticket-response" />
<wiretap ref="credit-card-application-processing" />

So the above route will receive an HTTP request for a credit card application, call a transaction ID service synchronously, send out an HTTP response that possesses a transaction ID with which users can come back later to check the application status, then call the ‘credit-card-application-processing’ route asynchronously. Here, how much ever time application processing is going to take, the user doesn’t have to wait—the whole credit card processing is going to be asynchronously done.

There is one more way to do this, to make the entire flow asynchronous.

<from ref="http-endpoint" />
<setExchangePattern pattern="InOnly" />
<to ref="credit-card-application-processing" />

Here, the entire flow is asynchronous; once the user hits the HTTP endpoint for a credit-card-application, the asynchronous flow will kick off immediately. The drawback of this approach is, generally people hitting an HTTP endpoint will expect a response or the client will have to add some exception handling to suppress any read timeouts calling this HTTP endpoint. (Or you can prefer a JMS endpoint.)

2. JMS Asynchronous Endpoint

JMS is a bit tricky; whether your JMS service is asynchronous or not, the client always has control to call it synchronously or asynchronously.

For example, a client can just simply call the JMS endpoint asynchronously without setting any JMSReplyTo header.

Also, if you want to call a JMS endpoint asynchronously within a Camel context, it's easy. You can either set the exchange Pattern to InOnly using:

<setExchangePattern pattern="InOnly" />

or by passing the exchange pattern in the JMS URI:

<to uri="jms:queue:credit-card-application?exchangePattern=InOut />

3. Asynchronous Routes

The same approach is used in an application for calling any routes internally within a Camel context. Like I said in the HTTP example, the ‘credit-card-application-processing‘ endpoint can be a direct:bla, jms:bla, etc..

The seda: component is especially meant for asynchronous processing, so I would recommend using seda: as internal seda endpoints.

Apache Camel

Published at DZone with permission of Gnanaguru Sattanathan. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Three SQL Keywords in QuestDB for Finding Missing Data
  • Automated Performance Testing With ArgoCD and Iter8
  • Why Every Fintech Company Needs DevOps
  • Cloud Native London Meetup: 3 Pitfalls Everyone Should Avoid With Cloud Data

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: