Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The Transactional Client EIP controls its transactions with the messaging system. It makes the client’s session with the messaging system transactional so that the client can specify transaction boundaries. For more information, refer to http://www.eaipatterns.com/TransactionalClient.html.

...

In many business scenarios, there can be data losses loss when transferring from one section to another , due to because of crashes and other interruptions. This example scenario demonstrates how messages containing the MESSAGE_COUNT value (MESSAGE_COUNT=1) are assumed to be causing a failure in mediation, and how to transactions guarantee that data will not be lost.

There are two ways to implement transaction in WSO2 ESB as follows.

The ESB can control transactional behavior with a JMS queue , and by simulating the Transactional Client EAI pattern. For information on Transactions in WSO2 ESB, refer to section Transactional ESB Transactions in the WSO2 ESB documentation.

The diagram below depicts how to simulate the example scenario using the WSO2 ESB.

Figure 2: Example Scenario of the Transactional Client EIP

Before digging into implementation details, let's take a look at the relationship between the example scenario and the Transactional Client EIP by comparing their core components.

Figure 1: Transactional Client EIP Figure 2: (Figure 1)Transactional Client Example Scenario (Figure 2)
Transactional ProducerProxy Service service inSequence
MessageSimple Stock Quote Request
Transactional ConsumerSimple Stock Quote Service

Environment setup

  1. Download an and install the WSO2 ESB from http://wso2.com/products/enterprise-service-bus. For a list of prerequisites and step-by-step installation instructions, refer to Getting Started Installation Guide in the WSO2 ESB documentation. 
  2. Download and install a JMS server. We use ActiveMQ as the JMS provider in this example.    
  3. In order to enable the JMS transport, edit <ESB_HOME>/repository/conf/axis2/axis2.xml as follows.
    • Uncomment the Axis2 t ransport transport listener configuration for ActiveMQ as follows:
      <transportReceiver name="jms" class="org.apache.axis2.transport.jms.JMSListener">...
     
    • Set the transport.jms.SessionTransacted parameter to true. After making this update, the transportReceiversection in axis2.xml should look as follows:

      Code Block
      languagehtml/xml
      <transportReceiver name="jms" class="org.apache.axis2.transport.jms.JMSListener">
              <parameter name="myQueueConnectionFactory" locked="false">
                  <parameter name="java.naming.factory.initial" locked="false">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
                  <parameter name="java.naming.provider.url" locked="false">tcp://localhost:61616</parameter>
                  <parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">QueueConnectionFactory</parameter>
              	<parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
              	<parameter name="transport.jms.SessionTransacted">true</parameter>
              </parameter>
      
              <parameter name="default" locked="false">
                  <parameter name="java.naming.factory.initial" locked="false">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
                  <parameter name="java.naming.provider.url" locked="false">tcp://localhost:61616</parameter>
                  <parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">QueueConnectionFactory</parameter>
                  <parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
      			<parameter name="transport.jms.SessionTransacted">true</parameter>
              </parameter>
      </transportReceiver>
    • Uncomment the Axis2 transporttransport sender configuration as follows:
      <transportSender name="jms" class="org.apache.axis2.transport.jms.JMSSender"/>  
  4. Copy the following ActiveMQ client jar files to the <ESB_HOME>/repository/components/lib directory. It allows the ESB to connect to the JMS provider.
    • activemq-core-5.2.0.jar
    • geronimo-j2ee-management_1.0_spec-1.0.jar
     
  5. You need to add a custom mediator called MessageCounterMediator. Download the MessageCounterMediator file and place it in the <ESB_HOME>/repository/components/lib folder. To learn how to write custom mediators, refer toWriting Custom Mediator Implementations guide in the WSO2 ESB documentation.
  6. Start the ActiveMQ (or equivalent JMS Server) and WSO2 ESB.
  7. Start the sample Axis2 server. For instructions, refer to the section Setting Up the ESB Samples Setup - Starting Sample Back-End Servicesthe Axis2 server in the WSO2 ESB documentation.

...

Start the ESB server and log into its management console UI (https://localhost:9443/carbon). In the management console, navigate to the Main Menu, click Service Bus and then Source View menu and click Source View in the Service Bus section. Next, copy and paste the following configuration, which helps you explore the example scenario, to the source view.

Anchor
step7
step7

...

Simulating the sample scenario

Use the jmsclient that comes with WSO2 ESB by default, ESB's default jmsclient to send messages to the JMS Queue queue in ActiveMQ as follows:

ant

...

jmsclient

...

-Djms_type=pox

...

-Djms_dest=dynamicQueues/StockQuoteProxy

...

-Djms_payload=WSO2

Note in the ESB console of WSO2 ESB that the first attempt will rollback roll back and the second attempt will be committed.

...

Let's investigate the elements of the ESB configuration in detail. The line numbers below are mapped with the ESB configuration illustrated in step 7 shown above.

  • class [line 11 6 in ESB config] - A Custom mediator called MessageCountMediator is loaded. This mediator keeps track of the number of messages that pass through the sequence it calls , by updating the value of a variable named MESSAGE_COUNT
  • switch [line 17 10 in ESB config] - The Switch mediator checks the value of MESSAGE_COUNT
  • case [line 19 11 in ESB config] - If the value of MESSAGE_COUNT is 1, then the transaction is considered to have failed, and no message will be put on the send channel. 
  • default [line 27 18 in ESB config] - The default action of the switch case flow control. This occurs when the MESSAGE_COUNT is anything but 1. The message is put on the send channel, and the transaction is considered a success.