Pattern AppendixThere now follows a breakdown of the various Enterprise Integration Patterns Messaging SystemsMessage ChannelCamel supports the Message Channel For more details see Using This PatternIf 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. MessageCamel supports the Message To support various message exchange patterns like one way event messages Using This PatternIf 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. Pipes and FiltersCamel supports the Pipes and Filters With Camel you can split your processing across multiple independent Endpoint instances which can then be chained together. Using Routing LogicYou can create pipelines of logic using multiple Endpoint or Message Translator instances as follows from("direct:a").pipeline("direct:x", "direct:y", "direct:z", "mock:result"); In the above example we are routing from a single Endpoint to a list of different endpoints specified using URIs. If you find the above a bit confusing, try reading about the Architecture or try the Examples Using This PatternIf 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 RouterThe Message Router 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("queue:a").choice() .when(header("foo").isEqualTo("bar")).to("queue:b") .when(header("foo").isEqualTo("cheese")).to("queue:c") .otherwise().to("queue:d"); } }; Using the Spring XML Extensions <camelContext id="buildSimpleRouteWithChoice" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="queue:a"/> <choice> <when> <predicate> <header name="foo"/> <isEqualTo value="bar"/> </predicate> <to uri="queue:b"/> </when> <when> <predicate> <header name="foo"/> <isEqualTo value="cheese"/> </predicate> <to uri="queue:c"/> </when> <otherwise> <to uri="queue:d"/> </otherwise> </choice> </route> </camelContext> Using This PatternIf 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 TranslatorCamel supports the Message Translator Using the Fluent Builders from("direct:start").setBody(body().append(" World!")).to("mock:result"); or you can add your own Processor from("direct:start").process(new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(in.getBody(String.class) + " World!"); } }).to("mock:result"); For further examples of this pattern in use you could look at one of the JUnit tests Using This PatternIf 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 EndpointCamel supports the Message Endpoint When using the DSL to create Routes you typically refer to Message Endpoints by their URIs rather than directly using the Endpoint For more details see Using This PatternIf 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. Messaging ChannelsPoint to Point ChannelCamel supports the Point to Point Channel
Using This PatternIf 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. Publish Subscribe ChannelCamel supports the Publish Subscribe Channel
Using Routing LogicAnother option is to explicitly list the publish-subscribe relationship in your routing logic; this keeps the producer and consumer decoupled but lets you control the fine grained routing configuration using the DSL or Xml Configuration. Using the Fluent Builders RouteBuilder builder = new RouteBuilder() { public void configure() { from("queue:a").to("queue:b", "queue:c", "queue:d"); } }; Using the Spring XML Extensions <camelContext id="buildStaticRecipientList" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="queue:a"/> <to> <uri>queue:b</uri> <uri>queue:c</uri> <uri>queue:d</uri> </to> </route> </camelContext> Using This PatternIf 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. Dead Letter ChannelCamel supports the Dead Letter Channel RedeliveryIt is common for a temporary outage or database deadlock to cause a message to fail to process; but the chances are if its tried a few more times with some time delay then it will complete fine. So we typically wish to use some kind of redelivery policy to decide how many times to try redeliver a message and how long to wait before redelivery attempts. The RedeliveryPolicy
Once all attempts at redelivering the message fails then the message is forwarded to the dead letter queue. Redelivery headerWhen a message is redelivered the DeadLetterChannel Configuring via the DSLThe following example shows how to configure the Dead Letter Channel configuration using the DSL RouteBuilder<Exchange> builder = new RouteBuilder<Exchange>() { public void configure() { errorHandler(deadLetterChannel("queue:errors")); from("queue:a").to("queue:b"); } }; You can also configure the RedeliveryPolicy RouteBuilder<Exchange> builder = new RouteBuilder<Exchange>() { public void configure() { errorHandler(deadLetterChannel("queue:errors").maximumRedeliveries(2).useExponentialBackOff()); from("queue:a").to("queue:b"); } }; Using This PatternIf 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. Guaranteed DeliveryCamel supports the Guaranteed Delivery
Using This PatternIf 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 BusCamel supports the Message Bus Folks often assume that a Message Bus is a JMS though so you may wish to refer to the JMS component for traditional MOM support. Also worthy of node is the XMPP component for supporting messaging over XMPP (Jabber) Using This PatternIf 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 RoutingContent Based RouterThe Content Based Router 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("queue:a").choice() .when(header("foo").isEqualTo("bar")).to("queue:b") .when(header("foo").isEqualTo("cheese")).to("queue:c") .otherwise().to("queue:d"); } }; Using the Spring XML Extensions <camelContext id="buildSimpleRouteWithChoice" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="queue:a"/> <choice> <when> <predicate> <header name="foo"/> <isEqualTo value="bar"/> </predicate> <to uri="queue:b"/> </when> <when> <predicate> <header name="foo"/> <isEqualTo value="cheese"/> </predicate> <to uri="queue:c"/> </when> <otherwise> <to uri="queue:d"/> </otherwise> </choice> </route> </camelContext> For further examples of this pattern in use you could look at the junit test case Using This PatternIf 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 FilterThe Message Filter 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("queue:a").filter(header("foo").isEqualTo("bar")).to("queue: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="queue:a"/> <filter> <predicate> <header name="foo"/> <isEqualTo value="bar"/> </predicate> </filter> <to uri="queue:b"/> </route> </camelContext> For further examples of this pattern in use you could look at the junit test case Using This PatternIf 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 ListThe Recipient List Static Receipient ListThe 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("queue:a").to("queue:b", "queue:c", "queue:d"); } }; Using the Spring XML Extensions <camelContext id="buildStaticRecipientList" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="queue:a"/> <to> <uri>queue:b</uri> <uri>queue:c</uri> <uri>queue:d</uri> </to> </route> </camelContext> Dynamic Recipient ListUsually one of the main resons for using the Recipient List Using the Fluent Builders RouteBuilder builder = new RouteBuilder() { public void configure() { from("queue: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="queue: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 PatternIf 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. SplitterThe Splitter ExampleThe 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("queue:a").splitter(bodyAs(String.class).tokenize("\n")).to("queue: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="queue:a"/> <splitter> <recipients> <bodyAs class="java.lang.String"/> <tokenize token=" "/> </recipients> </splitter> <to uri="queue:b"/> </route> </camelContext> For further examples of this pattern in use you could look at one of the junit test case Using This PatternIf 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. ResequencerThe Resequencer 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:a").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")) Using the Spring XML Extensions For further examples of this pattern in use you could look at the junit test case Using This PatternIf 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 TransformationContent EnricherCamel supports the Content Enricher Using the Fluent Builders Here is a simple example using the DSL directly from("direct:start").setBody(body().append(" World!")).to("mock:result"); In this example we add our own Processor from("direct:start").process(new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(in.getBody(String.class) + " World!"); } }).to("mock:result"); For further examples of this pattern in use you could look at one of the JUnit tests Using This PatternIf 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. Content FilterCamel supports the Content Filter A common way to filter messages is to use an Expression in the DSL like XQuery, SQL or one of the supported Scripting Languages. Using the Fluent Builders Here is a simple example using the DSL directly from("direct:start").setBody(body().append(" World!")).to("mock:result"); In this example we add our own Processor from("direct:start").process(new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(in.getBody(String.class) + " World!"); } }).to("mock:result"); For further examples of this pattern in use you could look at one of the JUnit tests Using This PatternIf 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. NormalizerCamel supports the Normalizer See AlsoUsing This PatternIf 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. Messaging EndpointsMessaging MapperCamel supports the Messaging Mapper See also
Using This PatternIf 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. Event Driven ConsumerCamel supports the Event Driven Consumer The Event Driven Consumer is implemented by consumers implementing the Processor For more details see Using This PatternIf 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. Polling ConsumerCamel supports implementing the Polling Consumer So in your Java code you can do Endpoint endpoint = context.getEndpoint("activemq:my.queue");
PollingConsumer consumer = endpoint.createPollingConsumer();
Exchange exchange = consumer.receive();
There are 3 main polling methods on PollingConsumer
Scheduled Poll ComponentsQuite a few inbound Camel endpoints use a scheduled poll pattern to receive messages and push them through the Camel processing routes. That is to say externally from the client the endpoint appears to use an Event Driven Consumer but internally a scheduled poll is used to monitor some kind of state or resource and then fire message exchanges. Since this a such a common pattern, polling components can extend the ScheduledPollConsumer There is also the Quartz Component which provides scheduled delivery of messages using the Quartz enterprise scheduler. For more details see
Using This PatternIf 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. Competing ConsumersCamel supports the Competing Consumers You can use the following components to implement competing consumers:-
Using This PatternIf 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 DispatcherCamel supports the Message Dispatcher You can use a component like JMS with selectors to implement a Selective Consumer as the Message Dispatcher implementation. Or you can use an Endpoint as the Message Dispatcher itself and then use a Content Based Router as the Message Dispatcher. See AlsoUsing This PatternIf 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. Selective ConsumerThe Selective Consumer The first solution is to provide a Message Selector to the underlying URIs when creating your consumer. For example when using JMS you can specify a selector parameter so that the message broker will only deliver messages matching your criteria. The other approach is to use a Message Filter which is applied; then if the filter matches the message your consumer is invoked as shown in the following example Using the Fluent Builders RouteBuilder builder = new RouteBuilder() { public void configure() { from("queue:a").filter(header("foo").isEqualTo("bar")).process(myProcessor); } }; Using the Spring XML Extensions <camelContext id="buildCustomProcessorWithFilter" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="queue:a"/> <filter> <predicate> <header name="foo"/> <isEqualTo value="bar"/> </predicate> </filter> <process ref="#myProcessor"/> </route> </camelContext> Using This PatternIf 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. Durable SubscriberCamel supports the Durable Subscriber Another alternative is to combine the Message Dispatcher or Content Based Router with File or JPA components for durable subscribers then something like Queue for non-durable. See AlsoUsing This PatternIf 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. Idempotent ConsumerThe Idempotent Consumer This pattern is implemented using the IdempotentConsumer The Idempotent Consumer essentially acts like a Message Filter to filter out duplicates. Using the Fluent Builders The following example will use the header myMessageId to filter out duplicates RouteBuilder builder = new RouteBuilder() { public void configure() { from("queue:a").idempotentConsumer( header("myMessageId"), memoryMessageIdRepository(200) ).to("queue:b"); } }; The above example return new SpringRouteBuilder() { public void configure() { from("direct:start").idempotentConsumer( header("messageId"), jpaMessageIdRepository(bean(JpaTemplate.class), "myProcessorName") ).to("mock:result"); } }; In the above example Using the Spring XML Extensions <camelContext id="buildCustomProcessorWithFilter" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="queue:a"/> <filter> <predicate> <header name="foo"/> <isEqualTo value="bar"/> </predicate> </filter> <process ref="#myProcessor"/> </route> </camelContext> For further examples of this pattern in use you could look at the junit test case Using This PatternIf 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. Transactional ClientCamel recommends supporting the Transactional Client Transaction Oriented Endpoints (Camel Toes) like JMS support using a transaction for both inbound and outbound message exchanges. Endpoints that support transactions will participate in the current transaction context that they are called from. You should use the SpringRouteBuilder For inbound endpoint to be transacted, they normally need to be configured to use a Spring PlatformTransactionManager You first define needed object in the spring configuration. <bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager"> <property name="connectionFactory" ref="jmsConnectionFactory" /> </bean> <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616"/> </bean> Then you look them up and use them to create the JmsComponent. PlatformTransactionManager transactionManager = (PlatformTransactionManager) spring.getBean("jmsTransactionManager"); ConnectionFactory connectionFactory = (ConnectionFactory) spring.getBean("jmsConnectionFactory"); JmsComponent component = JmsComponent.jmsComponentTransacted(connectionFactory, transactionManager); component.getConfiguration().setConcurrentConsumers(1); ctx.addComponent("activemq", component); Transaction PoliciesOutbound endpoints will automatically enlist in the current transaction context. But what if you do not want your outbound endpoint to enlist in the same transaction as your inbound endpoint? The solution is to add a Transaction Policy to the processing route. You first have to define transaction policies that you will be using. The policies use a spring TransactionTemplate <bean id="PROPAGATION_REQUIRED" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="jmsTransactionManager"/> </bean> <bean id="PROPAGATION_NOT_SUPPORTED" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="jmsTransactionManager"/> <property name="propagationBehaviorName" value="PROPAGATION_NOT_SUPPORTED"/> </bean> <bean id="PROPAGATION_REQUIRES_NEW" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="jmsTransactionManager"/> <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/> </bean> Then in your SpringRouteBuilder public void configure() { ... Policy requried = new SpringTransactionPolicy(bean(TransactionTemplate.class, "PROPAGATION_REQUIRED")); Policy notsupported = new SpringTransactionPolicy(bean(TransactionTemplate.class, "PROPAGATION_NOT_SUPPORTED")); Policy requirenew = new SpringTransactionPolicy(bean(TransactionTemplate.class, "PROPAGATION_REQUIRES_NEW")); ... } Once created, you can use the Policy objects in your processing routes: // Send to bar in a new transaction from("activemq:queue:foo").policy(requirenew).to("activemq:queue:bar"); // Send to bar without a transaction. from("activemq:queue:foo").policy(notsupported ).to("activemq:queue:bar"); See AlsoUsing This PatternIf 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. Messaging GatewayCamel has several endpoint components that support the Messaging Gateway Components like Bean, CXF and Pojo provide a a way to bind a Java interface to the message exchange. See AlsoUsing This PatternIf 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. Service ActivatorCamel has several endpoint components that support the Service Activator Components like Bean, CXF and Pojo provide a a way to bind the message exchange to a Java interface/service. See AlsoUsing This PatternIf 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. System ManagementWire TapThe Wire Tap The following example shows how to route a request from an input queue:a endpoint to the wire tap location queue:tap before it is received by queue:b Using the Fluent Builders RouteBuilder builder = new RouteBuilder() { public void configure() { from("queue:a").to("queue:tap", "queue:b"); } }; Using the Spring XML Extensions <camelContext id="buildWireTap" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="queue:a"/> <to> <uri>queue:tap</uri> <uri>queue:b</uri> </to> </route> </camelContext> Using This PatternIf 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. |