Message Routing

Content Based Router

The Content Based Router from the EIP patterns allows you to route messages to the correct destination based on the contents of the message exchanges.

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

Using the Fluent Builders

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        from("seda:a").choice()
                .when(header("foo").isEqualTo("bar")).to("seda:b")
                .when(header("foo").isEqualTo("cheese")).to("seda:c")
                .otherwise().to("seda:d");
    }
};

Using the Spring XML Extensions

<camelContext id="buildSimpleRouteWithChoice" xmlns="http://activemq.apache.org/camel/schema/spring"><route><from uri="seda:a"/><choice><when><predicate><header name="foo"/><isEqualTo value="bar"/></predicate><to uri="seda:b"/></when><when><predicate><header name="foo"/><isEqualTo value="cheese"/></predicate><to uri="seda:c"/></when><otherwise><to uri="seda:d"/></otherwise></choice></route></camelContext>

For further examples of this pattern in use you could look at the junit test case

Using This Pattern

If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.

Message Filter

The Message Filter from the EIP patterns allows you to filter messages

The following example shows how to create a Message Filter route consuming messages from an endpoint called queue:a which if the Predicate is true will be dispatched to queue:b

Using the Fluent Builders

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        from("seda:a").filter(header("foo").isEqualTo("bar")).to("seda:b");
    }
};

You can of course use many different Predicate languages such as XPath, XQuery, SQL or various Scripting Languages. Here is an XPath example

from("direct:start").filter(
        xpath("/person[@name='James']")
).to("mock:result");

Using the Spring XML Extensions

<camelContext id="buildSimpleRouteWithHeaderPredicate" xmlns="http://activemq.apache.org/camel/schema/spring"><route><from uri="seda:a"/><filter><predicate><header name="foo"/><isEqualTo value="bar"/></predicate></filter><to uri="seda:b"/></route></camelContext>

For further examples of this pattern in use you could look at the junit test case

Using This Pattern

If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.

Recipient List

The Recipient List from the EIP patterns allows you to route messages to a number of destinations.

Static Receipient List

The following example shows how to route a request from an input queue:a endpoint to a static list of destinations

Using the Fluent Builders

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        from("seda:a").to("seda:b", "seda:c", "seda:d");
    }
};

Using the Spring XML Extensions

<camelContext id="buildStaticRecipientList" xmlns="http://activemq.apache.org/camel/schema/spring"><route><from uri="seda:a"/><to><uri>seda:b</uri><uri>seda:c</uri><uri>seda:d</uri></to></route></camelContext>

Dynamic Recipient List

Usually one of the main resons for using the Recipient List pattern is that the list of recipients is dynamic and calculated at runtime. The following example demostrates how to create a dynamic recipient list using an Expression (which in this case it extracts a named header value dynamically) to calculate the list of endpoints which are either of type Endpoint or are converted to a String and then resolved using the endpoint URIs.

Using the Fluent Builders

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        from("seda:a").recipientList(header("foo"));
    }
};

The above assumes that the header contains a list of endpoint URIs. The following takes a single string header and tokenizes it

from("direct:a").recipientList(header("recipientListHeader").tokenize(","));

Using the Spring XML Extensions

<camelContext id="buildDynamicRecipientList" xmlns="http://activemq.apache.org/camel/schema/spring"><route><from uri="seda:a"/><recipientList><recipients><header name="foo"/></recipients></recipientList></route></camelContext>

For further examples of this pattern in use you could look at one of the junit test case

Using This Pattern

If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.

Splitter

The Splitter from the EIP patterns allows you split a message into a number of pieces and process them individually

Example

The following example shows how to take a request from the queue:a endpoint the split it into pieces using an Expression, then forward each piece to queue:b

Using the Fluent Builders

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        from("seda:a").splitter(bodyAs(String.class).tokenize("\n")).to("seda:b");
    }
};

The splitter can use any Expression language so you could use any of the Languages Supported such as XPath, XQuery, SQL or one of the Scripting Languages to perform the split. e.g.

from("activemq:my.queue").splitter(xpath("//foo/bar")).to("file://some/directory")

Using the Spring XML Extensions

<camelContext id="buildSplitter" xmlns="http://activemq.apache.org/camel/schema/spring"><route><from uri="seda:a"/><splitter><recipients><bodyAs class="java.lang.String"/>
          <tokenize token="
"/>
        </recipients></splitter><to uri="seda:b"/></route></camelContext>

For further examples of this pattern in use you could look at one of the junit test case

Using This Pattern

If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.

Resequencer

The Resequencer from the EIP patterns allows you to reorganise messages based on some comparator. By default in Camel we use an Expression to create the comparator; so that you can compare by a message header or the body or a piece of a message etc.

The following example shows how to reorder the messages so that they are sorted in order of the body() expression. That is messages are collected into a batch (either by a maximum number of messages per batch or using a timeout) then they are sorted in order and then sent out to their output.

Using the Fluent Builders

from("direct:start").resequencer(body()).to("mock:result");

So the above example will reorder messages from endpoint direct:a in order of their bodies, to the endpoint mock:result. Typically you'd use a header rather than the body to order things; or maybe a part of the body. So you could replace this expression with

resequencer(header("JMSPriority"))

for example to reorder messages using their JMS priority.

You can of course use many different Expression languages such as XPath, XQuery, SQL or various Scripting Languages.

You can also use multiple expressions; so you could for example sort by priority first then some other custom header

resequencer(header("JMSPriority"), header("MyCustomerRating"))

For further examples of this pattern in use you could look at the junit test case

Using the Spring XML Extensions

Using This Pattern

If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.