MQ7

package mq;

import java.io.IOException;

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.MQGetMessageOptions;
import com.ibm.mq.MQQueue;  
import com.ibm.mq.MQQueueManager;  
 
public class Test {  
    public static MQMessage receiveMessage(){
     MQMessage message = new MQMessage();  
     try {  
       
            //   建立MQ客户端调用上下文环境  
            MQEnvironment.hostname = "127.0.0.1"; //   服务器ip地址     
            MQEnvironment.port = 1414; //   服务器MQ服务端口     
            MQEnvironment.CCSID = 1208; //   服务器MQ服务使用的编码     
            MQEnvironment.channel = "SERVERCONN"; //   服务器连接通道名        
            MQQueueManager queueManager = null;  
            MQQueue queue = null;  
            queueManager = new MQQueueManager("MQ1"); //队列管理器名称  
            queue = queueManager.accessQueue("cosys_counter", MQC.MQOO_INPUT_AS_Q_DEF); //队列名称  
            MQGetMessageOptions gmo = new MQGetMessageOptions(); //消息属性  
            gmo.options = MQC.MQGMO_LOGICAL_ORDER; 
           
            queue.get(message, gmo);  
            //   关闭队列     
            queue.close();  
            //   关闭队列管理器     
            queueManager.disconnect();  
        } catch (MQException e) {  
            e.printStackTrace();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } 
       
        return message;
    }
 
    //发送消息  
    public static void sendMessage() {  
        try {  
 
            //   建立MQ客户端调用上下文环境  
            MQEnvironment.hostname = "127.0.0.1"; //   服务器ip地址     
            MQEnvironment.port = 1414; //   服务器MQ服务端口     
            MQEnvironment.CCSID = 1208; //   服务器MQ服务使用的编码     
            MQEnvironment.channel = "SERVERCONN"; //   服务器连接通道名        
            MQQueueManager queueManager = null;  
            MQQueue queue = null;  
            queueManager = new MQQueueManager("MQ1"); //队列管理器名称  
            queue = queueManager.accessQueue("cosys_counter", MQC.MQOO_OUTPUT); //队列名称  
            MQPutMessageOptions pmo = new MQPutMessageOptions(); //消息属性  
            pmo.options = MQC.MQPMO_LOGICAL_ORDER;  
            MQMessage message = new MQMessage();  
            //message.format = "MQSTR"; //字符串  
            //message.writeUTF("");  
            message.writeString("Hello!");  
            //message.writeBytes("");  
            System.out.println("message==" + message.encoding + "  " 
                    + message.characterSet);  
            queue.put(message, pmo);  
            //   关闭队列     
            queue.close();  
            //   关闭队列管理器     
            queueManager.disconnect();  
        } catch (MQException e) {  
            e.printStackTrace();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
      
    public static void main(String arge[]) throws IOException {  
        Test aa = new Test();  
        aa.sendMessage();  
        System.out.println(aa.receiveMessage());
    }  

你可能感兴趣的:(IBM)