- Apache Camel Developer's Cookbook
- Scott Cranton Jakub Korab
- 562字
- 2021-07-19 18:46:38
Routing Slip – routing a message to a fixed list of endpoints
When you need to dynamically compute the list of endpoints to route a message to at runtime, a Routing Slip EIP can help. The Routing Slip can use a message, an external source, or typically a message header (generated from a previous route step) to determine the list of endpoints to route the message to.

The Routing Slip is different from the Dynamic Router in that the Routing Slip Expression is only called once to determine the list of endpoints, whereas the Dynamic Router Expression is called multiple times until it decides it is time to stop routing (returns null
).
This recipe will show you how to specify an Expression that will determine how to route each individual message to a runtime provided with a list of endpoints.
Getting ready
The Java code for this recipe is located in the org.camelcookbook.routing.routingslip
package. The Spring XML files are located under src/main/resources/META-INF/spring
and are prefixed with routingSlip
.
How to do it...
Create a route with a routingSlip
statement. The Expression within that statement must return a String or list of Strings, where each String identifies an endpoint URI to route the message to in sequence.
In these examples, the Routing Slip uses the contents of the message header myRoutingSlipHeader
, which could either have been set with the original message or set by the previous route steps:
In the XML DSL, this logic is written as:
<route> <from uri="direct:start"/> <routingSlip> <header>myRoutingSlipHeader</header> </routingSlip> </route>
In the Java DSL, the same thing is expressed as:
from("direct:start") .routingSlip(header("myRoutingSlipHeader"));
How it works...
The Routing Slip uses a Camel Expression to determine the list of endpoints to route to. This Expression is evaluated only once, and the message is routed in sequence to one or more endpoints specified in that list.
If the intent of the Expression is to return a list of endpoint URIs that will be called in sequence, the Expression may separate the URIs with a delimiter which by default is a comma (,
). The separator can be changed using the uriDelimiter
attribute of the routingSlip
element.
If you find that you are trying to route to an endpoint that does not currently exist, and do not want this condition to be considered an error, you can configure the ignoreInvalidEndpoints
option to true
(defaults to false
) to tell Camel to ignore the ResolveEndpointFailedException
that would normally be thrown.
There's more...
We can use Camel's Bean Injection capabilities to put everything into the Java class using Java Annotations. The @RoutingSlip
annotation provides the same information to Camel as the routingSlip
element within a route. The @Consume
annotation tells Camel to create a new route consuming on the provided endpoint URI no need to provide a separate route definition.
@Consume(uri = "direct:routingSlipAnnotated") @RoutingSlip(delimiter = ",") public List<String> routeMe(String body, @Headers Map<String, Object> headers) { //... }
This pattern can also be considered a variation on the Recipient List EIP. In the Recipient List, an Expression is also used to determine a list of endpoint URIs; however, in that pattern each endpoint receives its own copy of the original message.
See also
- Routing Slip: http://camel.apache.org/routing-slip.html
- Dynamic Router: http://camel.apache.org/dynamic-router.html
- Recipient List: http://camel.apache.org/recipient-list.html
- Bean Injection: http://camel.apache.org/bean-integration.html
- Expression: http://camel.apache.org/expression.html
- 解構產品經理:互聯網產品策劃入門寶典
- Learning Cython Programming(Second Edition)
- Learning Chef
- Servlet/JSP深入詳解
- Mastering macOS Programming
- Learning JavaScript Data Structures and Algorithms
- Java EE 8 Application Development
- Mastering Android Game Development
- 軟件品質之完美管理:實戰經典
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- Python算法指南:程序員經典算法分析與實現
- Solr Cookbook(Third Edition)
- 大話Java:程序設計從入門到精通
- ASP.NET開發寶典
- Android智能手機APP界面設計實戰教程