Reputation: 5649
<camel:route id="messageRoute">
<camel:from ref="fromMessageQueue" />
<camel:processor ref="queueMessageProcessor" />
<camel:to ref="toMessageQueue" />
</camel:route>
In this code snippet, receiving a message from q queue then processing it in queue message processor, finally placing it into to message queue. While processing the message in processor, the body of exchange is getting set to null. I need to prevent the exchange with null body from entering into to message queue.
Upvotes: 1
Views: 5436
Reputation: 3140
Try to use simple language and content based router:
<camel:route id="messageRoute">
<camel:from ref="fromMessageQueue" />
<camel:processor ref="queueMessageProcessor" />
<camel:choice>
<camel:when>
<camel:simple>${body} != null</camel:simple>
<camel:to ref="toMessageQueue" />
</camel:when>
</camel:choice>
</camel:route>
Upvotes: 1
Reputation: 1459
@Amit: This is what you can do to avoid NPE: 1. Set the header in your Camel Process based on the body value e.g. Here I am setting the header IsNull based on the In body content.
public class QueueManagerProcessor implements Processor{
@Override
public void process(Exchange exchange) throws Exception {
if(exchange.getIn().getBody()==null){
exchange.getOut().setHeader("IsNull", "true");
}else{
exchange.getOut().setHeader("IsNull", "false");
}
}
}
In your Route Builder you can apply Content based Routing using choice and when condition
<xpath>$IsNull = 'false'</xpath>
<camel:to ref="toMessageQueue" />
Upvotes: 1
Reputation: 1459
Use Exchange Pattern InOut i.e.
<blockquote>
<camel:route id="messageRoute">
<camel:from ref="fromMessageQueue" />
<camel:processor ref="queueMessageProcessor" />
**<setExchangePattern pattern="InOut"/>**
<camel:to ref="toMessageQueue" />
</camel:route>
</blockquote>
Basically you are telling Camel to In the same message, modify it, and send(Out) back the modified message.
Upvotes: 1