Asynchronous API’s using Apache Camel

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Do you want to expose a Asynchronous service/API in Apache Camel ? Of course thats one core functionality which Camel can provide, there are multiple ways to implement it. It also depends on what kind of protocol/systems you are dealing with. (Also its 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 Async http endpoint doesn’t change in most scenarios.

Using wiretap endpoint – whatever exchange pattern the upstream endpoints sets, wiretap will replace it with InOnly exchange pattern.

So the above route, will receive a http request for credit card application, call a transaction id service synchronously, send out a http response which possess a transaction id with which user can come back later to check the application status, then call ‘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.

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 a HTTP endpoint will expect a response or the client will have to add some exception handling to suppress any read timeout’s calling this http endpoint. (Or you can prefer a JMS endpoint)

2. JMS Asynchronous endpoint

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

For example, 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 camel context, its easy. You can either set exchange Pattern to InOnly using

or by passing the exchange pattern in the jms uri:

3. Asynchronous routes

The same approach is application for calling any routes internally within a camel context. Like I said in the http example,

credit-card-application-processing‘ endpoint can be a direct:bla, jms:bla, etc..

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

Facebooktwittergoogle_plusredditpinterestlinkedinmail
Gnanaguru Sattanathan

Gnanaguru Sattanathan

I am a Integration developer and a open source enthusiast. My areas of interest includes Enterprise messaging & SOA. Twitter: @tallguru

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>