Sunday, November 11, 2012

Java class to connect to MQ. Post and Retreive messages


package com.kiran.mq;

import java.io.IOException;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

/**
 * Java class to connect to MQ. Post and Retreive messages.
 */
public class MQConnectionTest {

    String qMngrStr = "";
    //String user = "";
    //String password = "";
    String queueName = "";
    String hostName = "";
    int port = 0;
    String channel = "";
    //message to put on MQ.
    String msg = "Hello World, WelCome to MQ.";
    //Create a default local queue.
    MQQueue defaultLocalQueue;
    MQQueueManager qManager;
    
    /**
     * Initialize the MQ
     *
     */
    public void init(){
        
        //Set MQ connection credentials to MQ Envorinment.
         MQEnvironment.hostname = hostName;
         MQEnvironment.channel = channel;
         MQEnvironment.port = port;
         MQEnvironment.userID = user;
         MQEnvironment.password = password;
         //set transport properties.(optional)
         MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
         
         try {
             //initialize MQ manager.
            qManager = new MQQueueManager(qMngrStr);
        } catch (MQException e) {
            e.printStackTrace();
        }
    }
    
    
    public void putAndGetMessage(){
        
        int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; 
        try {
            defaultLocalQueue = qManager.accessQueue(queueName, openOptions);
            
            MQMessage putMessage = new MQMessage();
            putMessage.writeUTF(msg);
            
            //specify the message options...
            MQPutMessageOptions pmo = new MQPutMessageOptions(); 
            // accept 
            // put the message on the queue
            defaultLocalQueue.put(putMessage, pmo);
            
            System.out.println("Message is put on MQ.");
            
            //get message from MQ.
            MQMessage getMessages = new MQMessage();
            //assign message id to get message.
            getMessages.messageId = putMessage.messageId;
            
            //get message options.
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            defaultLocalQueue.get(getMessages, gmo);
            
            String retreivedMsg = getMessages.readUTF();
            System.out.println("Message got from MQ: "+retreivedMsg);
            
        } catch (MQException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        
        System.out.println("Processing Main...");
        
        MQConnectionTest mqt= new MQConnectionTest();
        
        //initialize MQ.
        mqt.init();
        
        //put and retreive message from MQ.
        mqt.putAndGetMessage();
        
        System.out.println("Okkkkkkkkkkkkkkk...........Done!");
    }
    
}


Jars required for this above Program are,
com.ibm.mq.jar
com.ibm.mq.jms.admin.jar
com.ibm.msg.client.jms.jar
com.ibm.msg.client.jms.internal.jar
com.ibm.ws.sib.client.thin.jms.jar
j2ee.jar,(search all jms amd mq related jars in websphere application server home)

No comments:

Post a Comment