Amit Bhati
Amit Bhati

Reputation: 5649

Restrict the null body from entering into a camel endpoint in xml

<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

Answers (3)

dey
dey

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

Ankush soni
Ankush soni

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");
        }
    }

}
  1. 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

Ankush soni
Ankush soni

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

Related Questions