JMS Sample - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

In this sample we want to listen for messages on a queue and process the messages with our business logic Java code and send them along. Since it is based on a unit test the destination is a mock endpoint.

First we configure the standard Spring XML to declare a JMS connection factory, a JMS transaction manager and our ActiveMQ component that we use in our routing.

<!-- setup JMS connection factory -->
<bean id="jmsConnectionFactory" 
   class="org.apache.activemq.ActiveMQConnectionFactory">
   <property name="brokerURL" 
      value="vm://localhost?broker.persistent=false&broker.useJmx=false"/>
</bean>

<!-- setup Spring jms TX manager -->
<bean id="jmsTransactionManager" 
   class="org.springframework.jms.connection.JmsTransactionManager">
   <property name="connectionFactory" ref="jmsConnectionFactory"/>
</bean>

<!-- define our activemq component -->
<bean id="activemq" 
   class="org.apache.activemq.camel.component.ActiveMQComponent">
   <property name="connectionFactory" ref="jmsConnectionFactory"/>
   <!-- define the jms consumer/producer as transacted -->
   <property name="transacted" value="true"/>
   <!-- setup the transaction manager to use -->
   <!-- if not provided then Camel will automatically use a 
       JmsTransactionManager, however if you for instance use a JTA  
       transaction manager then you must configure it -->
   <property name="transactionManager" ref="jmsTransactionManager"/>
</bean>

And then we configure our routes. Notice that all we have to do is mark the route as transacted using the transacted tag.

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <!-- disable JMX during testing -->
    <jmxAgent id="agent" disabled="true"/>
    <route>
        <!-- 1: from the jms queue -->
        <from uri="activemq:queue:okay"/>
        <!-- 2: mark this route as transacted -->
        <transacted/>
        <!-- 3: call our business logic that is myProcessor -->
        <process ref="myProcessor"/>
        <!-- 4: if success then send it to the mock -->
        <to uri="mock:result"/>
    </route>
</camelContext>

<bean id="myProcessor" 
    class="org.apache.camel.component.jms.tx.JMSTransactionalClientTest \\ 
    $MyProcessor"/>

Note

Which error handler? When a route is marked as transacted using transacted, Camel will automatically use the TransactionErrorHandler as Error Handler. It supports basically the same feature set as the DefaultErrorHandler, so you can for instance use Exception Clause as well.