Edmondo
Edmondo

Reputation: 20080

Correlating messages on two Camel Routes

In my application I have a generic Camel Route such as the following

from("direct:something").to("direct:outgoing")

and then dynamically in my code I deploy another route:

from("direct:outgoing").process(processor)

When flowing from route 1 to route 2 a new Exchange will be created. Is there an idiomatic way to correlate both? Should I set EXCHANGE.Correlation_ID header on the first route before sending it out?

Upvotes: 1

Views: 849

Answers (2)

Peter Keller
Peter Keller

Reputation: 7636

From the Camel doc:

Some EIP patterns will spin off a sub message, and in those cases, Camel will add a correlation id on the Exchange as a property with they key Exchange.CORRELATION_ID, which links back to the source Exchange. For example the Splitter, Multicast, Recipient List, and Wire Tap EIP does this.

Thus, Exchange.CORRELATION_ID is set by Camel and should not be set by your application. But feel free to set a custom header or property if you need to such as:

exchange.getIn().setProperty("myProperty", myIdentifier);

Upvotes: 1

vikingsteve
vikingsteve

Reputation: 40388

This should definitely all be processed on the one exchange. Run this test and you'll see the same camel Exchange, with the same properties, etc.

public class CamelExchangeTest {
    public static void main(String[] args) throws Exception {
        final Processor showExchangeIdProcessor = new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                System.out.println(exchange.getExchangeId());
            }
        };

        Main camelMain = new Main();
        camelMain.addRouteBuilder(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("timer:foo?period=1s&repeatCount=1")
                        .log("excgabge created!")
                        .process(showExchangeIdProcessor)
                        .to("direct:outgoing")
                ;

                from("direct:outgoing")
                        .log("outgoing!")
                        .process(showExchangeIdProcessor)
                ;
            }
        });
        camelMain.run();
    }
}

Output:

ID-MYPC-55760-1411129552791-0-2
ID-MYPC-55760-1411129552791-0-2

So something else is going on. When you say "direct:outgoing", do you mean exactly that or is it something different - a different component perhaps?

When you say the route is created dynamically, how exactly is that done, and when (and why?)

Upvotes: 2

Related Questions