Steps carried out for enabling remote communication between WSO2 ESB 4.0.3 and IBM WebSphere MQ 6.0.1 using RefFSContextFactory

Concept behind RefFSContextFactory

RefFSContextFactory uses a JNDI folder as the provider URL. That JNDI folder has .binding files which contains the mapping between JNDI name and actual IBM Websphere MQ objects, such as a queue manager, queue, topic, and so forth. 





Configuring WebSphere MQ

The WebSphere installation directory will be reffered to as

  1. Go to the /Java/bin directory and open the JSAdmin.config file with a text editor.
  2. Comment out the existing INITIAL_CONTEXT_FACTORY and add a INITIAL_CONTEXT_FACTORY named com.sun.jndi.fscontext.RefFSContextFactory
  3. Comment the default PROVIDER_URL and use a directory path instead. Make sure that directory is created in the file system (ex: C:/JNDI-Directory).


Note: If exists, delete any earlier versions of the .bindings files from that folder.



ex: After changing your JSAdmin.config file should look like this.

#  appropriate one should be uncommented.
#
#INITIAL_CONTEXT_FACTORY=com.sun.jndi.ldap.LdapCtxFactory
INITIAL_CONTEXT_FACTORY=com.sun.jndi.fscontext.RefFSContextFactory
#INITIAL_CONTEXT_FACTORY=com.ibm.ejs.ns.jndi.CNInitialContextFactory
#INITIAL_CONTEXT_FACTORY=com.ibm.websphere.naming.WsnInitialContextFactory
#INITIAL_CONTEXT_FACTORY=com.ibm.websphere.naming.WMQInitialContextFactory
#
#  The following line specifies the URL of the service provider's initial
#  context. It currently refers to an LDAP root context. Examples of a
#  file system URL and WebSphere's JNDI namespace are also shown, commented
#  out.
#
#PROVIDER_URL=ldap://polaris/o=ibm,c=us
PROVIDER_URL=file:/C:/JNDI-Directory
#PROVIDER_URL=iiop://localhost/
#PROVIDER_URL=localhost:1414/SYSTEM.DEF.SVRCONN
......



  1. 4. Restart the IBM WebSphere service


Creating a queue on WebSphere MQ





  1. Open the WebSphere MQ explorer and right click on Queue Manager and select New >Queue Manager.
  2. Provide a name for the Queue Manager (myQueueManager) and Make it default. Press Next.
  3. Select the options to Start Queue Manager, Autostart Queue Manager and Create server connection channel. Then press on Next.
  4. Select the option to Create listener configuration for TCP/IP and provide a port number (1415).
  5. Once the Queue Manager is created, right click on the Queues icon belonging to the queue manager (myQueueManager) and select New->Local Queue.
  6. Provide a name for the Queue (myQueue) and press on Next. Go with the default configurations and press on Finish.
  7. Right click on the Channels icon under the Advanced icon and select New > Server-connection Channel. Provide a name for the channel (myChannel) and press on Next. Set the transmission protocol as TCP and press on Finish.
  8. Once these steps are completed, a listener will be created running on port 1415 and you should be able to view it by clicking on the listeners icon.





Generating .bindings file



  1. RefFSContextFactory uses a file system url as the provider url. We are going to create a .bindings file inside C:/JNDI-Directory folder using JSAdmin.config file we prepared. Navigate to /Java/bin directory and run JMSAdmin.bat file.
  2. create bindings as appropriate. You have to use the names of resources you have defined. You can choose JNDI names as you want.


//define a context factory. If in same machine you can use "define qcf(MYCF) //qmgr(CUSTOMER_QUEUE_MANAGER) transport(BIND)" as well.

define qcf(MYCF) qmgr(CUSTOMER_QUEUE_MANAGER) host() port() CHANNEL(SYSTEM.DEF.SVRCONN) transport(CLIENT)

//define a queue
define q(JMS_QUEUE) qmgr(CUSTOMER_QUEUE_MANAGER) queue(MY_QUEUE)

Following is an example commands that goes with configs we have made under “Creating a queue on WebSphere MQ”. You can check created objects by display ctx command.



Welcome to the Admin Tool's command-line interface

InitCtx> def qcf(MQ_JMS_MANAGER) qmgr(myQueueManager) tran(client) chan(myChannel) host(175.157.120.102) port(1415)

InitCtx> def q(JMS_QUEUE) qmgr(myQueueManager) queue(myQueue)

InitCtx> display ctx

 Contents of InitCtx

     .bindings                 java.io.File
  a  JMS_QUEUE                 com.ibm.mq.jms.MQQueue
  a  MQ_JMS_MANAGER            com.ibm.mq.jms.MQQueueConnectionFactory

 3 Object(s)
   0 Context(s)
   3 Binding(s), 2 Administered

InitCtx> end

Stopping Websphere MQ classes for Java(tm) Message Service Administration

C:\IBM\WebSphere MQ\Java\bin>

Now go to C:/JNDI-Directory . You should be able to view .bindings file there.



Configure an ESB instance running on a different machine



The following configuration changes were made on the ESB for it to be able to remotely
communicate with WebSphere MQ.
The ESB installation directory will be reffered to as .

  1. Copy C:/JNDI-Directory from above machine to a suitable place in new machine. We will consider this place in Ubuntu machine as /home/user/JNDI-Directory.
  2. Copy all the jars found in the jar_files.zip attachment to the /repository/ components/lib directory. In addition, you have to copy fscontext.jar file found in /Java/lib directory as well.
  3. Add the following jms transport receiver configuration to the /repository/ conf/axis2.xml file.



 <transportReceiver name="jms" class="org.apache.axis2.transport.jms.JMSListener">
<parameter name="myQueueConnectionFactory" locked="false">
<parameter name="java.naming.factory.initial" locked = "false">com.sun.jndi.fscontext.RefFSContextFactory</parameter>
<parameter name="java.naming.provider.url" locked="false">file:/home/user/JNDI-Directory</parameter>
<parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">MQ_JMS_MANAGER</parameter>
<parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
<parameter name="transport.jms.Destination">JMS_QUEUE</parameter>
<parameter name="transport.jms.JMSSpecVersion">1.0</parameter>
</parameter>

<parameter name="default" locked="false">
<parameter name="java.naming.factory.initial" locked = "false">com.sun.jndi.fscontext.RefFSContextFactory</parameter>
<parameter name="java.naming.provider.url" locked="false">file:/home/user/JNDI-Directory</parameter>
<parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">MQ_JMS_MANAGER</parameter>
<parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
<parameter name="transport.jms.Destination">JMS_QUEUE</parameter>
<parameter name="transport.jms.JMSSpecVersion">1.0</parameter>
</parameter>
</transportReceiver>

Note the changed values.

  1. Enable transport sender
            

  1. Start the ESB by providing the -Duser.name=SYSTEM jvm parameter. ex: ./wso2server.sh -Duser.name=SYSTEM

  1. On a successful connection, an INFO level log message similar to the following is printed on the esb startup.

INFO - JMSConnectionFactory JMS ConnectionFactory : myQueueConnectionFactory initialized


Test the setup


  1. Copy paste the following proxy service to ESB synapse configuration. This will listen to messages coming from created queue at Websphere MQ (myQueue) then log it and drop the message.


    <proxy name="SimpleStockQuoteService" transports="jms" startOnLoad="true">
        <target>
            <inSequence>
                <property name="OUT_ONLY" value="true"/>
                <log level="full"/>
                <drop/>
            </inSequence>
            <outSequence/>
        </target>
        <parameter name="transport.jms.ContentType">
            <rules>
                <jmsProperty>contentType</jmsProperty>
                <default>application/xml</default>
            </rules>
        </parameter>
        <parameter name="transport.jms.ConnectionFactory">myQueueConnectionFactory</parameter>
        <parameter name="transport.jms.DestinationType">queue</parameter>
        <parameter name="transport.jms.Destination">JMS_QUEUE</parameter>
    </proxy>

  1. Right click queue in MQ explorer and  put a test message to the queue (should be a xml). At that instance you will see that your message is consumed by the queue and logged at ESB running on remote machine.






Hasitha Hiranya

2 comments:

  1. Hi

    Thanks for the above code, it worked really well for me have you got any examples of sending messages to IBM MQ?

    Thanks again

    ReplyDelete
  2. Where I can find the jar_files.zip file?

    ReplyDelete

Instagram