Friday, October 14, 2011

Interacting with IBM MQ Series using standalone Java client

In general java developers using JMS will not encounter any issues as we are having JMS libraries in the Application Server along with other J2EE libraries. But interacting with MQ Series from java applications is a tricky thing. This is not visible to developers in many applications. Many of the cases the application interaction with MQ Series is hidden inside a framework/jar file. Below is a simple program to actual interaction with MQ Series. We would be required ibm.mq libraries in the classpath in order to compile and run the program.

MQSender.java
-------------------------------------------------------------------------------
package com.dev.mqtest;

import java.io.IOException;
import java.util.Hashtable;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
public class MQSender {

/**
* @param args
*/
public static void main(String[] args) {

try {
Hashtable properties = new Hashtable();
// MQQueueManager qMgr = new MQQueueManager("MQ Queue Manager Name", properties);

//hostipaddress, port,channel
MQEnvironment.hostname = "11.122.66.45";
MQEnvironment.port=11126;
MQEnvironment.channel = "";

MQQueueManager qMgr = new MQQueueManager("QUEUE_MANAGER_NAME");
            int openOptions =MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_OUTPUT ;

            //Now specify the queue that we wish to open, and the open options...
            MQQueue queue = qMgr.accessQueue("MQ_NAME",openOptions);
            MQMessage mqmsg =new MQMessage();
            mqmsg.characterSet = ((Integer) properties.get("mqcharset")).intValue();
            mqmsg.writeString("This is the message to post....... ");
//            mqmsg.replyToQueueName = "Reply Queue Name";

            //Set the put message options...
            MQPutMessageOptions pmo =new MQPutMessageOptions();

            //put the message on the queue...
            queue.put(mqmsg,pmo);

            //Close the queue...
            queue.close();
            //Disconnect from the queue manager
            qMgr.disconnect();
     

} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

refer to below link for more details on IBM MQ Series using Java


2 comments:

  1. The code you have is seemingly identical to my testing. To be sure, I have replaced the entire test to be sure. Following adding the com.ibm.mq.jar file to the classpath, I am still receiving an error when attempting to define the hostname.

    Exception in thread "main" java.lang.NoClassDefFoundError: javax.resource.ResourceException

    I am using Websphere 7.0 and have the jar file in the classpath. I am completely stumped as to how to resolve this message.

    ReplyDelete