Message Router

The Message Router from the EIP patterns allows you to consume from an input destination, evaluate some predicate then choose the right output destination.

image

In Camel the Message Router can be archived in different ways such as:

The Content Based Router is recommended to use when you have multiple predicates to evaluate where to send the message.

The Routing Slip and Dynamic Router are arguably more advanced where you do not use predicates to choose where to route the message, but use an expression to choose where the message should go.

Example

The following example shows how to route a request from an input direct:a endpoint to either direct:b, direct:c, or direct:d depending on the evaluation of various Predicate:

from("direct:a")
    .choice()
        .when(simple("${header.foo} == 'bar'"))
            .to("direct:b")
        .when(simple("${header.foo} == 'cheese'"))
            .to("direct:c")
        .otherwise()
            .to("direct:d");

And the same example using XML:

<route>
    <from uri="direct:a"/>
    <choice>
        <when>
            <simple>${header.foo} == 'bar'</simple>
            <to uri="direct:b"/>
        </when>
        <when>
            <simple>${header.foo} == 'cheese'</simple>
            <to uri="direct:c"/>
        </when>
        <otherwise>
            <to uri="direct:d"/>
        </otherwise>
    </choice>
</route>