Java Reference
In-Depth Information
8-9. Conditional Routing with Routers
You want to conditionally move a message through different processes based on some criteria. This is
the EAI equivalent to an if-else branch.
You can use a router component to alter the processing flow based on some predicate. You can also use
a router to multicast a message to many subscribers (as you did with the splitter ).
How It Works
With a router you can specify a known list of channels on which the incoming Message should be passed.
This has some powerful implications. It means you can change the flow of a process conditionally, and
it also means that you can forward a Message to as many (or as few) channels as you want. There are
some convenient default routers available to fill common needs, such as payload type-based routing
( PayloadTypeRouter ) and routing to a group or list of channels ( RecipientListRouter ).
Imagine for example a processing pipeline that routes customers with high credit scores to one
service and customers with lower credit scores to another process in which the information is queued
up for a human audit and verification cycle. The configuration is, as usual, very straightforward. In the
following example, you show the configuration. One router element, which in turn delegates the routing
logic to a class, is CustomerCreditScoreRouter .
<beans:bean id="customerCreditScoreRouter".
<channel id="safeCustomerChannel"/>
<channel id="riskyCustomerChannel"/>
<router input-channel="customerIdChannel" ref="customerCreditScoreRouter"/>
The Java code is similarly approachable. It feels a lot like a workflow engine's conditional element,
or even a JSF backing-bean method, in that it extricates the routing logic into the XML configuration,
away from code, delaying the decision until runtime. In the example, the String s returned are the names
of the channels on which the Message should pass.
import org.springframework.integration.annotation.Router;
public class CustomerCreditScoreRouter {
public String routeByCustomerCreditScore(Customer customer) {
if (customer.getCreditScore() > 770) {
return "safeCustomerChannel";
} else {
return "riskyCustomerChannel";
Search WWH ::

Custom Search