Websphere MQ2

package com.ibm.test; 
 
import java.io.IOException; 
 
import com.ibm.mq.MQC; 
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; 
 
public class MQSample { 
    //定义队列管理器和队列的名称  
    private static String qmName;  
    private static String qName; 
     
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
         
         qmName ="QM_APPLE"; 
         qName = "Q1"; 
        System.out.println("QManager:"+qmName); 
        System.out.println("QueueName:"+qName); 
        try { 
            //定义并初始化队列管理器对象并连接   
            MQQueueManager qMgr = new MQQueueManager(qmName);  
 
            // 设置将要连接的队列属性  
            // Note. All WebSphere MQ Options are prefixed with MQC in Java.   
            @SuppressWarnings("deprecation") 
            int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;  
 
            //连接队列   
            MQQueue localQ = qMgr.accessQueue(qName, openOptions);  
             
            //定义一个简单的消息  
            MQMessage putMessage = new MQMessage();  
            putMessage.writeUTF("Hello World!");  
 
            //设置写入消息的属性(默认属性)  
            MQPutMessageOptions pmo = new MQPutMessageOptions();  
             
            //将消息写入队列   
            localQ.put(putMessage,pmo);  
                         
//            MQMessage retrievedMessage = new MQMessage();  
//            retrievedMessage.messageId = putMessage.messageId;   
//  
//            //设置取出消息的属性(默认属性)  
            // MQGetMessageOptions gmo = new MQGetMessageOptions();   
//  
//            // 从队列中取出消息  
//            localQ.get(retrievedMessage, gmo);   
//            String msgText = retrievedMessage.readUTF();  
 
//            System.out.println("The message is: " + msgText);   
 
            //关闭队列  
            localQ.close();  
            //从队列管理器断开   
            qMgr.disconnect();  
        }catch (MQException ex) {  
            System.out.println("A WebSphere MQ error occurred : Completion code "  
            + ex.completionCode + " Reason code " + ex.reasonCode);  
        }catch (IOException ex) {  
            System.out.println("An error occurred whilst writing to the message buffer: " + ex);  
        }catch(Exception ex){ 
            ex.printStackTrace(); 
        } 
 
    } 
 

你可能感兴趣的:(websphere)