Over a million developers have joined DZone.

Asynchronous APIs Using Apache Camel

DZone's Guide to

Asynchronous APIs Using Apache Camel

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

Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

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.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

apache camel ,integration framework ,integration patterns ,asynchronous

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}