Hi Claus,
Does the async() DSL will fork a thread for the further processing?
What's the difference between the async() DSL and send the message to a
seda endpoint?
Thanks,
Willem
Claus Ibsen wrote:
> Hi
>
> Just another update on #3. I had some more fun with Camel and
> introduced a async() DSL in the route, to turn the route into async
> from the point forward.
> The unit test code explains it, and gives a hint how we can leverage this.
>
> Any thoughts?
>
>
>
> public class AsyncRouteTest extends ContextTestSupport {
>
> public void testAsyncRoute() throws Exception {
> MockEndpoint mock = getMockEndpoint("mock:result");
> mock.expectedBodiesReceived("Bye World");
>
> // send a request reply to the direct start endpoint
> Object out = template.requestBody("direct:start", "Hello");
> // as it turns into a async route later we get a Future as response
> assertIsInstanceOf(Future.class, out);
>
> // cast to future
> Future future = (Future) out;
>
> System.out.println("Look ma I can do other stuff while the async
> runs");
>
> // and use future to get the response
> Exchange response = (Exchange) future.get();
>
> // get the response from the OUT message
> // TODO: add type converters so we can leverage them with
> Future to get the
> // body response more easily
> assertEquals("Bye World", response.getOut().getBody());
>
> assertMockEndpointsSatisfied();
> }
>
> @Override
> protected RouteBuilder createRouteBuilder() throws Exception {
> return new RouteBuilder() {
> @Override
> public void configure() throws Exception {
> // we start this route async
> from("direct:start")
> // we play a bit with the message
> .transform(body().append(" World"))
> // now turn the route into async from this point forward
> // the caller will have a Future<Exchange>
> returned as response in OUT
> // to be used to grap the async response when he
> fell like it
> .async()
> // from this point forward this is the async route
> doing its work
> // so we do a bit of delay to simulate heavy work
> that takes time
> .delay(1000)
> // and we also play with the message so we can
> prepare a response
> .process(new Processor() {
> public void process(Exchange exchange) throws
> Exception {
> assertEquals("Hello World",
> exchange.getIn().getBody());
> exchange.getOut().setBody("Bye World");
> }
> // and we use mocks for unit testing
> }).to("mock:result");
> }
> };
> }
>
>
>