Sanjeet Behera
Sanjeet Behera

Reputation: 61

Passing Exchange Property between routes within same Camelcontext

I have below requirement:

Route 1: from(timer:foo?repeatcount=1).recepientList("rest service")

Route 2: from(sourcequeue).process(new myprocessor()).to(destinationqueue)

Need to use the json response from route 1 and pass it to Route 2 processor. My problem is whenever i set the json response in exchange property and try to use in Route 2 processor it is null.

Any suggestion on how to pass the exchange property between these routes would be of great help.

Thanks in advance.

Upvotes: 3

Views: 2677

Answers (2)

burki
burki

Reputation: 7005

The reason you cannot use Exchange properties to pass information between routes is that they are not part of the message.

Take a look at this picture of the Camel Exchange model.

When a message is received by Camel, it embeds it into an Exchange and the Exchange is passed through the route. But when you send a message (.to(...)), only the message is sent.

Therefore you have to use (as answered by Thomas) the message body or a message header.

Upvotes: 2

Thomas
Thomas

Reputation: 1140

If you use http camel component, the http response should be in the body. You can load it from your processor.

String json = exchange.getIn().getBody(String.class);

from(timer:foo?repeatcount=1).recepientList("http://rest_service")
.to(direct:sourcequeue)

You can also use headers to pass data throw your route.

from(timer:foo?repeatcount=1).recepientList("http://rest_service")
.setHeader(“myJsonResponse”, simple("${body}"))
.to(direct:sourcequeue)

String json = exchange.getIn().getHeader(“myJsonResponse”, String.class);

Upvotes: 1

Related Questions