You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by codemode <ar...@gmail.com> on 2015/11/04 18:36:18 UTC

How to write an asynchronous processor?

Hi,

I am writing a custom load balancer based on this link:
http://camel.apache.org/load-balancer.html

and changing it a bit like the follows:

String firstURI =
"ahc:http://localhost:7000/?bridgeEndpoint=true&throwExceptionOnFailure=false";
	   String secondURI =
"ahc:http://localhost:7001/?bridgeEndpoint=true&throwExceptionOnFailure=false";
	   String thirdURI =
"ahc:http://localhost:7002/?bridgeEndpoint=true&throwExceptionOnFailure=false";
	  
from(jetty:http://0.0.0.0:8080/?matchOnUriPrefix=true&enableMultipartFilter=false").loadbalance(new
MyLoadBalancer()).to(firstURI, secondURI, thirdURI);

Both the JettyConsumer as well as the AHC Producers are asynchronous and I'd
like to write MyLoadBalancer in such a way such that the entire route
becomes an asynchronous route. It looks like the example given in the link
isn't really async as it seems to be returning true. I believe async
processors would return false
(http://camel.apache.org/asynchronous-processing.html).

public static class MyLoadBalancer extends LoadBalancerSupport {
 
    public boolean process(Exchange exchange, AsyncCallback callback) {
        String body = exchange.getIn().getBody(String.class);
        try {
            if ("x".equals(body)) {
                getProcessors().get(0).process(exchange);
            } else if ("y".equals(body)) {
                getProcessors().get(1).process(exchange);
            } else {
                getProcessors().get(2).process(exchange);
            }
        } catch (Throwable e) {
            exchange.setException(e);
        }
        callback.done(true);
        return true;
    }
}

Can someone please give me an example of how to write a truly async
processor given the route I have described above (if we were to randomly
choose from one of those producer endpoints)?





--
View this message in context: http://camel.465427.n5.nabble.com/How-to-write-an-asynchronous-processor-tp5773378.html
Sent from the Camel - Users mailing list archive at Nabble.com.