esimran
esimran

Reputation: 89

Understanding Apache Camel Dynamic Routing

I have set up a simple dynamic router:

    public String slip(String body, @Header(Exchange.SLIP_ENDPOINT) String previous) {
                if (previous == null) {
                    return "mock:a";
                } 
                    else if (body.contains("status=2") ) {
                    return "mock:b";
                }
                    else if (body.contains("status=3") ) {
                    return "mock:c";
                }

                // no more so return null
                return null;
            }

Mock a,b,c are routes with custom processors.

public void process(Exchange exchange) throws Exception {
        String str_request = "";
        String str_requestNew = "";

        str_request = (String) exchange.getIn().getBody();

        if(str_request.contains("status=1"))
            str_requestNew = "status=2";
    }
  1. How do I update the message body between routes in my custom processor via Java DSL. exchange.getOut().setBody(newreq); ?

  2. Do I need to create a new producer and send the message back to the dynamic router? ProducerTemplate template = exchange.getContext().createProducerTemplate(); template.sendBody(myDynamicRouterEndpoint, newreq); or will my router pick up the new body if do it via method 1.

Or is there a huge flaw in my logic all together? :)

Upvotes: 3

Views: 7929

Answers (1)

Christian Schneider
Christian Schneider

Reputation: 19626

You can do it like you describe in 1.

It is even simpler if you use the bean component. Then you can have a plain java method for reading and setting the body:

public String doSomething(String body) { }

This will get the body in the parameter and the return value will be the new body. This also makes your bean independent of Camel.

Upvotes: 3

Related Questions