On Thu, Apr 30, 2009 at 4:05 PM, Willem Jiang <willem.ji...@gmail.com> wrote:
> 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?
Yes it forks a thread. Its the JDK Executor that handles this. That is
the beauty as we leverage the concurrency API in JDK.

The work I have done today is just experimental and thus nothing is
settled in stone. I just wanted to share what I have "played with"
today.

The different is that with async() DSL we leverage 100% the JDK
concurrency API for async callbacks. So async() DSL will return a
handle to the callback known as the Future handle from the JDK itself.
Then the original caller can use this handle to get the result.

With seda we do not leverage the JDK async API in the same sense. We
send a copy of the exchange to a JDK queue that a consumer in another
thread is listening.

Another difference is the seda is an endpoint. And thus we can use it
with the dynamic recipient list EIP patterns and all the other
patterns that work with endpoints. The async() is a DSL that is fixed
in that sence it breaks the current processing of the Exchange and
returns a Future handle to the original caller. And spaws a thread and
continues routing the exchange. All using the JDK async API.

The code for the async() DSL is basically:

    public void process(final Exchange exchange) throws Exception {
        // let it execute async and return the Future
        Callable<Exchange> task = new Callable<Exchange>() {
            public Exchange call() throws Exception {
                for (Processor output : outputs) {
                    output.process(exchange);
                }
                return exchange;
            }
        };

        Future<Exchange> future = executor.submit(task);
        // set the future as response so we can get this handle and
get the response later
        exchange.getOut().setBody(future);
    }

It was just an experiment as I wanted to see if it was possible to
change the current route into async without seda.




>
> 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");
>>             }
>>         };
>>     }
>>
>>
>>
>
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
Apache Camel Reference Card:
http://refcardz.dzone.com/refcardz/enterprise-integration

Reply via email to