...
As opposed to the synchronous Request Only the Async counter part will not wait for the processing of the message to complete. In this case the client can immediately continue doing other work while the message is being routed and processed in Camel. This is illustrated in the diagram below:
TODO: diagram
1. The client sends a Request only and we can still use Http despite http being Request Reply by nature. The control is immediately returned to the client application, that can continue and do other work while Camel routes the message.
2. Camel invokes an external TCP service using synchronous Request Reply. The client application can do other work simultaneously.
3. The message completes but no result is returned to the client.
Notice: As Camel always returns a Future
handle for Async messaging to the client. The client can then use this handler or not to get hold of the status of the processing whether the task is complete or an Exception occured during processing.
...
Camel provides the Async API in the Producer Template where we have added about 10 new methods to Camel 2.0. We have listed the most important in the table below:
Method | Returns | Description |
---|---|---|
setExecutorService | void | Is used to set the Java ExecutorService. Camel will by default provide a ScheduledExecutorService with 5 thread in the pool. |
asyncSend | Future<Exchange> | Is used to send an async exchange to a Camel Endpoint. Camel will imeddiately return control to the caller thread after the task has been submitted to the executor service. This allows you to do other work while Camel processes the exchange in the other async thread. |
asyncSendBody | Future<Object> | As above but for sending body only. This is a request only messaging style so no reply is expected. Uses the |
asyncRequestBody | Future<Object> As above but for sending body only. This is a Request Reply messaging style so a reply is expected. Uses the | |
extractFutureBody | T | Is used to get the result from the asynchronous thread using the Java Concurrency Future handle. |
...
The java.util.concurrent.Future
API have among others the following methods:
Method | Returns | Description |
---|---|---|
isDone | boolean | Returns a boolean whether the task is done or not. Will even return |
get() | Object | Gets the response of the task. In case of an exception was thrown the |
...
Wiki Markup |
---|
{snippet:id=e2|lang=java|url=camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpSyncTest.java} |
Using the Async
...
TODO: About the async
DSL.
Using the Async API with the Camel classic API
...
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(); |
Using the Async DSL
TODO: About the async
DSL.