Almost a month back we had written an article Steps to Create A Remote Queue in WebSphere MQ 7 in which we had given the steps to configure a remote queue in WebSphere MQ and in the article How to configure EJB3 MDBs on JBoss AS 5.1 for remote IBM Websphere MQ 7? which showed the steps to connect with WebSphere MQ using AS 5.1.

However in this article we would be doing the same but using JBoss AS 7.1.1 final which is the latest version of JBoss AS 7.  We would be sharing the configuration from JBoss AS 7.1.1 final side which would help everyone to consume messages from Websphere MQ 7 which is residing on a remote box. We will also see how can one send messages (i.e. using a JAVA standalone code) to a remote queue on Box-1 having Websphere MQ 7 and those messages can be received by the consumer (i.e. using EJB3 MDB deployed on JBoss) from Box-2 which is a very basic scenario in most of the people.

The main concept for this is that we would be sending messages to the remote queue of Websphere MQ 7 on box-1 and on box-2 we would be creating a resource adapter which would be using wmq.jmsra.rar to communicate with Websphere MQ having the queue on box-1. Using the same resource adapter and Websphere MQ’s hostName, port, queueManager, destination and transportType in the MDB we would be able to consume message from box-2 easily.

In Box-1 (10.10.10.10)

Lets say you have your Websphere MQ 7 running in this box and you can configure the remote queue in MQ 7 by following the steps given in the below article

Steps to Create A Remote Queue in WebSphere MQ 7

In Box-2 (20.20.20.20)

Below are the steps to consume messages from WebShere MQ using a MDB deployed on JBoss AS 7.1.1 final and we would be using the “standalone-full.xml”

  1. In “standalone-full.xml” following things has to be done
    1. Need to add the resource adapter under subsystem
    2. Replace

       <subsystem xmlns="urn:jboss:domain:resource-adapters:1.0"/>
      

      With

      <subsystem xmlns="urn:jboss:domain:resource-adapters:1.0">
      	<resource-adapters>
      		<resource-adapter>
      			<archive>
      				wmq.jmsra.rar
      			</archive>
      		</resource-adapter>
      	</resource-adapters>
      </subsystem>
      
    3. Need to add the resource adapter reference under mdb
    4. Replace

      <mdb>
      	<resource-adapter-ref resource-adapter-name="hornetq-ra"/>
      	<bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>
      </mdb>
      

      With

      <mdb>
      	<resource-adapter-ref resource-adapter-name="wmq.jmsra.rar"/>
      	<bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>
      </mdb>
      
  2. Deploy “wmq.jmsra.rar” in the “/jboss-as-7.1.1.Final/standalone/deployments/” folder
  3. We would create a MDB now having the following ActivationConfigProperty and ResourceAdapter, which has to be kept “MDB_30.jar” file in the “/jboss-as-7.1.1.Final/standalone/deployments/” folder
    1. Create a folder called “MDB_30.jar
    2. Inside “MDB_30.jar” create a “MyMDB.java” file and update this file with the below details
    3. import javax.ejb.*;
      import javax.jms.Message;
      import javax.jms.TextMessage;
      import javax.jms.JMSException;
      import javax.ejb.MessageDriven;
      import javax.jms.MessageListener;
      import javax.ejb.ActivationConfigProperty;
      import org.jboss.ejb3.annotation.ResourceAdapter;
      
      @MessageDriven( name="MyMDB",
              activationConfig = 
              { 
                  @ActivationConfigProperty(propertyName = "destinationType",propertyValue = "javax.jms.Queue"),
                  @ActivationConfigProperty(propertyName = "useJNDI", propertyValue = "false"),
                  @ActivationConfigProperty(propertyName = "hostName", propertyValue = "MQ.HOST.NAME"),
                  @ActivationConfigProperty(propertyName = "port", propertyValue = "MQ.PORT"),
                  @ActivationConfigProperty(propertyName = "channel", propertyValue = "MQ.CHANNEL.NAME"),
                  @ActivationConfigProperty(propertyName = "queueManager", propertyValue = "MQ.QUEUE.MANAGER"),
                  @ActivationConfigProperty(propertyName = "destination", propertyValue = "MQ.QUEUE.NAME"),
                  @ActivationConfigProperty(propertyName = "transportType", propertyValue = "MQ.CLIENT")
              }) 
      @ResourceAdapter(value = "wmq.jmsra.rar")
      @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
      
      public class MyMDB implements MessageListener{
      
          public void onMessage(Message message) {
              TextMessage textMessage = (TextMessage) message;
              try {
                  System.out.println("nnt Message Received by MDB : "+ textMessage.getText());
              } catch (JMSException e) {
                  e.printStackTrace();
              }
          }
      }
      
    4. Once every thing has been put at its place, then you compile the MyMDB.java code and if you want then you can also create an JAR archive file for above folder

After following the above given steps, we have successfully configured all the setting from JBoss end. Now its time to test our configuration by following the below steps.

Testing

  1. Create a Directory somewhere in your file system like: “/urs/JBoss/Queue” to write the QueueSend.java and as we have an MDB we would not need a QueueReceive.java programs.
  2. In QueueSend.java copy the below program
    import java.io.*;
    import java.io.*;
    import java.util.Hashtable;
    import javax.jms.JMSException;
    import javax.jms.Queue;
    import javax.jms.QueueConnection;
    import javax.jms.QueueConnectionFactory;
    import javax.jms.QueueSender;
    import javax.jms.QueueSession;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    public class QueueSend
    {
    public final static String JNDI_FACTORY="com.ibm.mq.jms.context.WMQInitialContextFactory";
    
    //*************** Connection Factory JNDI name *************************
    public final static String JMS_FACTORY="MyRemoteMQMgr"; 
    
    //*************** Queue Factory JNDI name *************************
    public final static String QUEUE="MyRemoteQueue";
    
    private QueueConnectionFactory qconFactory;
    private QueueConnection qcon;
    private QueueSession qsession;
    private QueueSender qsender;
    private Queue queue;
    private TextMessage msg;
    
    public void init(Context ctx, String queueName)throws NamingException, JMSException
    {
    qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
    qcon = qconFactory.createQueueConnection();
    qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    queue = (Queue) ctx.lookup(queueName);
    qsender = qsession.createSender(queue);
    msg = qsession.createTextMessage();
    qcon.start();
    }
    
    public void send(String message) throws JMSException {
    msg.setText(message);
    qsender.send(msg);
    }
    
    public void close() throws JMSException {
    qsender.close();
    qsession.close();
    qcon.close();
    }
    
    public static void main(String[] args) throws Exception {
    if (args.length != 1) {
    System.out.println("Usage: java QueueSend URL");
    return;
    }
    InitialContext ic = getInitialContext(args[0]);
    QueueSend qs = new QueueSend();
    qs.init(ic, QUEUE);
    readAndSend(qs);
    qs.close();
    }
    
    private static void readAndSend(QueueSend qs) throws IOException, JMSException
    {
    String line="Test Message Body with counter = ";
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    boolean readFlag=true;
    System.out.println("ntStart Sending Messages (Enter QUIT to Stop):n");
    while(readFlag)
    {
    System.out.print("<Msg_Sender> ");
    String msg=br.readLine();
    if(msg.equals("QUIT") || msg.equals("quit"))
    {
    qs.send(msg);
    System.exit(0);
    }
    qs.send(msg);
    System.out.println();
    }
    br.close();
    }
    
    private static InitialContext getInitialContext(String url) throws NamingException
    {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    env.put(Context.PROVIDER_URL, url);
    return new InitialContext(env);
    }
    }
    
  3. Now open just 1 – command prompt and run the “. ./setMQ_CLASSPATH.sh” by adding two DOTs separated by a single space, in both the prompts to set the Environment (PATH & CLASSPATH).
  4. Note: You would need the following jar files from Websphere MQ side to run the above program, hence change the path given in the below script under MQ_LIB

    1. dhbcore.jar
    2. mqcontext.jar
    3. com.ibm.mq.jar
    4. com.ibm.mqjms.jar
    5. com.ibm.mq.pcf.jar
    6. com.ibm.mq.jmqi.jar
    7. com.ibm.mq.headers.jar
    8. com.ibm.mq.commonservices.jar

    setMQ_CLASSPATH.sh

    #!/bin/sh
    
    echo "Exporting CLASSPATH for IBM MQ...."
    
    MQ_LIB=/home/urs/Remote_MQ/lib
    
    export CLASSPATH=$CLASSPATH:$MQ_LIB/com.ibm.mq.commonservices.jar:$MQ_LIB/com.ibm.mq.headers.jar:$MQ_LIB/com.ibm.mq.jmqi.jar:$MQ_LIB/com.ibm.mq.pcf.jar:$MQ_LIB/com.ibm.mqjms.jar:$MQ_LIB/dhbcore.jar:$MQ_LIB/mqcontext.jar:$MQ_LIB/com.ibm.mq.jar:.:
    echo "====================================="
    echo "CLASSPATH =" $CLASSPATH
    echo "====================================="
    
  5. Then compile the QueueSend.java programs.
  6. Now run the code with the following command
  7. Prompt-1

    java QueueSend 10.10.10.10:1415/MyRemoteChannel
     

Once everything is up and running properly you just have to send around few messages in “Prompt-1” and you would notice that those messages would be shown in shell on which the standalone-full is running which is on Box-2. Hence when you are sending a messages to the Websphere MQ 7 queue which is on Box-1 the same messages are been consumed by the MDB which is been deployed on standalone-full shell is running on Box-2.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.