Assume that we are building an order processing system. When an incoming order is received, we first validate the order and then verify that the ordered item is available in the warehouse. This function is performed by the inventory system. This sequence of processing steps is a perfect candidate for the Pipes and Filters style. We create two filters, one for the validation step and one for the inventory system, and route the incoming messages through both filters. However, in many enterprise integration scenarios more than one inventory system exists with each system being able to handle only specific items.
How do we handle a situation where the implementation of a single logical function (e.g., inventory check) is spread across multiple physical systems?
Use a Content-Based Router to route each message to the correct recipient based on message content.
The Content-Based Router examines the message content and routes the message onto a different channel based on data contained in the message. The routing can be based on a number of criteria such as existence of fields, specific field values etc. When implementing a Content-Based Router, special caution should be taken to make the routing function easy to maintain as the router can become a point of frequent maintenance. In more sophisticated integration scenarios, the Content-Based Router can take on the form of a configurable rules engine that computes the destination channel based on a set of configurable rules.
... Read the entire pattern in the book Enterprise Integration Patterns
Example: Apache CamelNEW
Many open source ESBs like Apache Camel have direct support for many Enterprise Integration Patterns, wither in a Java-based DSL or as configuration. For example, setting up a Content-Based Router in Camel is quite easy and idiomatic by using a RouteBuilder:
RouteBuilder builder = new RouteBuilder() { public void configure() { errorHandler(deadLetterChannel("mock:error")); from("direct:in") .choice() .when(header("type").isEqualTo("widget")) .to("direct:widget") .when(header("type").isEqualTo("gadget")) .to("direct:gadget") .otherwise() .to("direct:other"); } };
Related patterns:
Dynamic Router, Message Filter, Invalid Message Channel, Message Router, Pipes and Filters, Publish-Subscribe Channel, Routing Slip, Transactional Client