@RoutingSlip Annotation

As of Camel 2.4.0 we now support the use of @RoutingSlip on a bean method to easily create a dynamic Routing Slip using a Java method.

Simple Example using @Consume and @RoutingSlip

package com.acme.foo;

public class RouterBean {

    @Consume(uri = "activemq:foo")
    public String[] route(String body) {
        return new String[]{"activemq:bar", "activemq:whatnot"};

For example if the above bean is configured in Spring when using a <camelContext> element as follows

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd

  <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"/>

  <bean id="myRoutingSlip" class="com.acme.foo.RouterBean"/>


then a route will be created consuming from the foo queue on the ActiveMQ component which when a message is received the message will be forwarded to the endpoints defined by the result of this method call - namely the bar and whatnot queues.

How it works

The return value of the @RoutingSlip method is converted to either a java.util.Collection / java.util.Iterator or array of objects where each element is converted to an Endpoint or a String, or if you are only going to route to a single endpoint then just return either an Endpoint object or an object that can be converted to a String. So the following methods are all valid

public String[] route(String body) { ... }

public List<String> route(String body) { ... }

public Endpoint route(String body) { ... }

public Endpoint[] route(String body) { ... }

public Collection<Endpoint> route(String body) { ... }

public URI route(String body) { ... }

public URI[] route(String body) { ... }

Then for each endpoint or URI the message is routed accordingly to the returned slip. See details at the Routing Slip EIP.

You can then use whatever Java code you wish to figure out what endpoints to route to; for example you can use the Bean Binding annotations to inject parts of the message body or headers or use Expression values on the message.

More Complex Example Using DSL

In this example we will use more complex Bean Binding, plus we will use a separate route to invoke the Routing Slip

public class RouterBean2 {

    public String route(@Header("customerID") String custID String body) {
    	if (custID == null)  return null;
        return "activemq:Customers.Orders." + custID;

public class MyRouteBuilder extends RouteBuilder {
    protected void configure() {
        from("activemq:Orders.Incoming").routingSlip(bean("myRouterBean", "route"));

Notice how we are injecting some headers or expressions and using them to determine the recipients using Routing Slip EIP.
See the Bean Integration for more details.

© 2004-2011 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram