Pages

Wednesday, November 17, 2010

Spring - TIBCO - JMS Integration - Synchronous Approach

Here I am going to provide how to use Spring Integration with JMS and TIBCO synchrnously.

For simplicity I divided into 2 different XML files called as :

- tibcoContext.xml and
- messagingContext.xml,

These both files should be imported in your spring applicationContext.xml (or) what ever name given in your project.

The namespaces for tibcoContext.xml goes here:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:integration="http://www.springframework.org/schema/integration"
       xmlns:jms="http://www.springframework.org/schema/integration/jms"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:stream="http://www.springframework.org/schema/integration/stream"
       xmlns:si-xml="http://www.springframework.org/schema/integration/xml"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/integration
                           http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
                  http://www.springframework.org/schema/integration/jms
                  http://www.springframework.org/schema/integration/jms/spring-integration-jms-1.0.xsd
                  http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context.xsd
                  http://www.springframework.org/schema/integration/stream
                  http://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd
                  http://www.springframework.org/schema/integration/xml
                  http://www.springframework.org/schema/integration/xml/spring-integration-xml-1.0.xsd">

The namespaces for messagingContext.xml goes here:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:jms="http://www.springframework.org/schema/integration/jms"
             xmlns:stream="http://www.springframework.org/schema/integration/stream"
             xmlns:si-xml="http://www.springframework.org/schema/integration/xml"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/integration
                           http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
                           http://www.springframework.org/schema/integration/jms
                           http://www.springframework.org/schema/integration/jms/spring-integration-jms-1.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/integration/stream
                           http://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd
                                 http://www.springframework.org/schema/integration/xml
                                 http://www.springframework.org/schema/integration/xml/spring-integration-xml-1.0.xsd ">

Coming to configuration in tibcoContext.xml file:

Step 1: You need the below connection factory in order to connect to your EMS server:

<bean id="tibConnectionFactory" class="com.tibco.tibjms.TibjmsQueueConnectionFactory">
        <property name="serverUrl" value="tcp://localhost:7222"/>
        <property name="userName" value=""/>
        <property name="userPassword" value=""/>
</bean>


<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory" ref="tibConnectionFactory"/>
        <property name="sessionCacheSize" value="10"/>
        <property name="cacheProducers" value="true"/>
        <property name="cacheConsumers" value="true"/>
 </bean>


Step 2: Configure the Queue where you need to send the messages:

<bean id="senderQueue" class="com.tibco.tibjms.TibjmsQueue">
        <constructor-arg value="jms/tibcoSenderQueue"/>
</bean>


Step 3: Configure the Jaxb Marshaller to Marshal and unmarshall the messages you would like to send and receive:

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.test.Test1</value>
                <value>com.test.Test2</value>
            </list>
        </property>
</bean>


Step 4: Configure the transformer to convert your payload result to String:

<bean id="resultToStringTransformer"
          class="org.springframework.integration.xml.transformer.ResultToStringTransformer"/>


Step 5: The last and final step in your tibcoContext.xml is to configure the Gateway to send and recieve the Messages:

    <integration:gateway id="dynamicQueryMessageGateway"
                         default-request-channel="senderChannel"
                         service-interface="com.test.ISenderGateway"/>

     <integration:gateway id="dynamicQueryReplyGateway"
                         default-reply-channel="marshaledRecieverChannel"
                         service-interface="com.test.IRecieverGateway"/>


Now We need to create the above gateway interfaces in our code which looks like below:

@Service("senderGatewayService")
public interface ISenderGateway {

    @Gateway(requestChannel = "senderChannel")
    public void sendMessage(@Header("requestType") String requestType, TestOb ob);
}


@Service("receiverGatewayService")
public interface IRecieverGateway {

    @Gateway(replyChannel = "marshaledRecieverChannel", replyTimeout = 5000)
    public ResultOb recieveMessage();

}

Now here the configuration of messagingContext.xml file:

Step 1: Create Approprate required channels:

<channel id="senderChannel"/>
<channel id="marshaledSenderChannel"/>

<channel id="recieverChannel"/>
<channel id="marshaledRecieverChannel">
        <queue/>
</channel>


Step 2: Create Marshaler on SenderChannel:

<si-xml:marshalling-transformer id="defaultMarshaller"
                                    marshaller="jaxbMarshaller"
                                    input-channel="senderChannel"
                                    output-channel="marshaledSenderChannel"
                                    result-transformer="resultToStringTransformer"/>


Step 3: Create JMS outbound gateway on marshaledSenderChannel which holds the results after marshaling:

 <jms:outbound-gateway id="outBoundGateway"
                          request-channel="marshaledSenderChannel"
                          request-destination="senderQueue"
                          connection-factory="connectionFactory"
                          reply-channel="recieverChannel"/>


Step 4: Create unmarshaler on the receiverChannel:

<si-xml:unmarshalling-transformer id="defaultUnmarshaller"
                                      input-channel="recieverChannel"
                                      output-channel="marshaledRecieverChannel"
                                      unmarshaller="jaxbMarshaller"/>


Here we finished all the required configuration for sending and recieving jms messages using spring integration in Synchronous way.

Client code to test the above:

@Autowired
    private ISenderGateway senderGateway;


@Autowired
    private IReceiverGateway receiverGateway;


public void testSynchronousMessaging() {
        try {
                      
            senderGateway.sendMessage("requestType", testOb);

            System.out.println(receiverGateway.recieveMessage());
        } catch(Exception e) {
            e.printStackTrace();
        }

}

Any comments, improvments or suggetions are welcome.

1 comment:

  1. The issue I faced with SI-1.0.3 is resolved when moved to SI-1.0.4 or later versions when working with Synchronous way of communication - The issue is My receiever gateway times out for the second response. The issue is logged here:

    http://forum.springsource.org/showthread.php?t=97955

    ReplyDelete