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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • Building a Sentiment Analysis Pipeline With Apache Camel and Deep Java Library (DJL)
  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Powering LLMs With Apache Camel and LangChain4j

Trending

  • Top JavaScript/TypeScript Gen AI Frameworks for 2026
  • A System Cannot Protect What It Does Not Understand
  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  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.

By 
Gnanaguru Sattanathan user avatar
Gnanaguru Sattanathan
·
Jun. 28, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
11.8K 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.

Related

  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • Building a Sentiment Analysis Pipeline With Apache Camel and Deep Java Library (DJL)
  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Powering LLMs With Apache Camel and LangChain4j

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook