...
The asynchronous API in Camel have been rewritten for Camel 2.0, and the information on this page applies for Camel 2.0 and later.
A bit of background
The Async API in Camel is primarily divided in two areas
1. Initiating an Asyc messaging from the client
2. Turning a route into Async using the DSL
Before we look at these two areas we start with a bit of background information and looks at the concept from at a higher level using diagrams.
Then we check out the first area how a client can initiate an Asyc message exchange and we also throw in the synchronous message exchange in the mix as well so we can compare and distill the difference.
And finally we turn our attention towards the last area the new async DSL and what it can be used for.
Background
The new Async API in Camel 2.0 leverages in much greater detail the Java Concurrency API and its support for executing tasks asynchronous.
Therefore the Camel Async API should be familiar for users with knowledge of the Java Concurrency API.
...
With these diagrams in mind lets turn out attention to the Async API and how to use it with Camel.
The Async Client API
Camel provides the Async Client API in the ProducerTemplate where we have added about 10 new methods to Camel 2.0. We have listed the most important in the table below:
...
Code Block | ||||
---|---|---|---|---|
| ||||
Endpoint endpoint = context.getEndpoint("http://slowserver.org/myservice"); Exchange exchange = endpoint.createExchange(); exchange.getIn().setBody("Order ABC"); // create a regular producer Producer producer = endpoint.createProducer(); // normally you will use a shared exectutor service with pools ExecutorService executor = Executors.newSingleThreadExecutor(); // send it async with the help of this helper Future<Exchange> future = AsyncProcessorHelper.asyncProcess(executor, producer, exchange); // here we got the future handle and we can do other stuff while the exchange is being routed in the other asynchronous thread ... // and to get the response we use regular Java Concurrency API Exchange response = future.get(); |
2) Using the Async DSL
...
In Camel 2.0 the async
DSL replaces the old thread
DSL.
The async
DSL can be used to turn a synchronous route into Async. What happens is that from the point forwards from async
the messages is routed asynchronous in a new thread. The caller will either wait for a reply if a reply is expected, such as when we use Request Reply messaging. Or the caller will complete as well if no reply was expected such as Request Only messaging.
The async
DSL allows a bit of configuration.
Option | Description |
---|---|
poolSize | A number to indicate the core pool size of the underlying Java |
executorService | You can provide a custom |
waitForTaskToComplete | Option to specify if the caller should wait for the async task to be complete or not before continuing. The following 3 options is supported: Always, Newer or IfReplyExpected. The first two options is self explained. The last will only wait if the message is Request Reply based. The default option is IfReplyExpected. |