Amit Bhati
Amit Bhati

Reputation: 5659

How to avoid null body from entering into next processor in multiple steps

How can I restrict the null body from entering into multiple processor at one common place. In below code instead of putting null body check at every processor, how can I define it at a single place ?

<choice>
    <when>
        <simple>${body} != null</simple>
        <process ref="processor1" />
        <choice>
            <when>
                <simple>${body} != null</simple>
                <process ref="processor2" />
                <!-- Splitter -->
                <split>
                    <simple>body</simple>
                    <process ref="processor3" />
                </split>
            </when>   
        </choice>
    </when>
</choice>     

Upvotes: 0

Views: 2082

Answers (3)

Arun Prakash
Arun Prakash

Reputation: 1727

By adding an <stop> tag you can do this.

<choice>
    <when>
        <simple>${body} != null</simple>
        <stop></stop>            
    </when>   
    <otherwise>
        <process ref="processor2" />
        <!-- Splitter -->
        <split>
        <simple>body</simple>
        <process ref="processor3" />
        </split>
    <otherwise>
</choice>

Upvotes: 1

Fritz Duchardt
Fritz Duchardt

Reputation: 11940

I suggest you leave the root all-together, thus rendering further null checks obsolete. A quick and easy way to stop route-processing for your current message is setting the Exchange.ROUTE_STOP property on your exchange object AND return null:

exchange.getProperty(Exchange.ROUTE_STOP, Boolean.TRUE)

Upvotes: 4

piezol
piezol

Reputation: 963

Code in comment will be awfully displayed, so i post an answer. Doing it in processor is a simple null check, no philosophy

public class SomeProcessor implements Processor {

    public void process(Exchange exchange) {
        if(exchange.getIn().getBody() != null){
            // Your processing here
            // Is only performed
            // When body is not null
            // Otherwise null body is being resent
        }
    }

}

Edit (answer to comment): It is not possible AFAIK and it wouldn't be a proper way to do it. Router which you're already using is how it should be performed. If you want to throw away your message, i think this could work(i didn't check it, though):

<choice>
    <when>
        <simple>${body} == null</simple>
        #<stop />
        # OR
        #<to uri="wherever-you-want-to-send-nonvalid-messages" />
    </when>
    <otherwise>
        <camel:process ref="processor1" />
        <camel:process ref="processor2" />
        <camel:process ref="processor3" />
        <to uri="where-you-want-to-send-valid-messages" />
    </otherwise>

</choice>

It will only check for nulls before first processor, obviously, so if i.e. second processor gives null out message, it will not be thrown out.

Upvotes: 1

Related Questions