放置消息

  • 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 Send {  
    •   
    •  public static void main(String[] args) {  
    •    try {  
    •   
    •       // 主机名称  
    •       String hostName = "192.168.1.101";  
    •       // 端口(缺省 1414)  
    •       int port = 1515;  
    •       // 通道名称(缺省)  
    •       String channel = "SYSTEM.DEF.SVRCONN";  
    •       // 队列管理器名称  
    •       String qManager = "send";  
    •       // 队列名称 (远程队列名)  
    •       String qName = "Q1";  
    •   
    •       // Set up the MQEnvironment properties for Client Connections.  
    •       // 建立MQEnvironment 属性以便客户机连接.  
    •       MQEnvironment.hostname = hostName;  
    •       MQEnvironment.port = port;  
    •       MQEnvironment.channel = channel;  
    •       MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);  
    •       MQEnvironment.CCSID = 1381;   
    •         
    •       // Connection To the Queue Manager.  
    •       // 连接到队列管理器.  
    •       MQQueueManager qMgr = new MQQueueManager(qManager);  
    •   
    •       /** 
    •        * Set up the open options to open the queue for out put 
    •        * and additionally we have set the option to fail if the queue manager 
    •        * is quiescing. 
    •        * 
    •        * 建立打开选项以便打开用于输出的队列,进一步而言,如果队列管理器是 
    •        * 停顿的话,我们也已设置了选项去应对不成功情况. 
    •        */  
    •       int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;  
    •   
    •       // Open the queue.  
    •       // 打开队列.  
    •       MQQueue queue = qMgr.accessQueue(qName, openOptions, null, null,  
    •         null);  
    •   
    •       // Set the put message options , we will use the default setting.  
    •       // 设置放置消息选项我们将使用默认设置.  
    •       MQPutMessageOptions pmo = new MQPutMessageOptions();  
    •   
    •       /** 
    •        * Next we Build a message The MQMessage class encapsulates the data 
    •        * buffer that contains the actual message data, together with all the MQMD 
    •        * parameters that describe the message. 
    •        * 
    •        * 下一步我们建立消息,MQMessage类压缩了包含实际消息数据的数据缓冲区, 
    •        * 和描述消息的所有MQMD 参数. 
    •        * 
    •        * To Build a new message, create a new instance of MQMessage class 
    •        * and use writxxx (we will be using writeString method). 
    •        * The put() method of MQQueue also takes an instance of the 
    •        * MQPutMessageOptions class as a parameter. 
    •        * 
    •        * 欲建立新消息,创建MQMessage类新实例以及使用writxxx(我们将使用writeString 方法.). 
    •        * MQQueue 的put()方法也可作为参数MQPutMessageOptions 类的实例. 
    •        */  
    •   
    •       // Create The message buffer.  
    •       // 创建消息缓冲区.  
    •       MQMessage outMsg = new MQMessage();  
    •   
    •       // Set the MQMD format field.  
    •       // 设置MQMD 格式字段.  
    •       outMsg.format = MQC.MQFMT_STRING;  
    •                 
    •   
    •       // Prepare message with user data.  
    •       // 准备用户数据消息.  
    •       String msgString = "HEAD7C5218991310615536   BOCOMC     0100521899        200909170720470020090917101000000022164428000000020000000000000000000000020000  1110408715  0000    0 1564439990000044399900000                          1 1  000                                               DOMN200909170720470016100115400000000156          HA1310011130028738445 AT032007101900000068835700000110000000000000000020090915000000010000000000431643000000000000                       PENG PING          6532CAFCR102                                                                    000281548016372856DQPD        2007100120071030                               000002000000000000000000000068835700000110000000000000000000000001000000000043164300000048411900000010000000620090917072055550                                                                                                                                      19790117                                                               518000          20090917000000272977113714719646         1310710130979       MALE                    SSNO180001016790                             MS0000000000                2007101920090912000000002007103001                  0           00000004841192007102004064394C       000000000           TAIL";  
    •   
    •       // Now we put The message on the Queue.  
    •       // 现在我们在队列上放置消息.  
    •       outMsg.writeString(msgString);  
    •   
    •   
    •       // Commit the transaction.  
    •       // 提交事务处理.  
    •       queue.put(outMsg, pmo);  
    •       qMgr.commit();  
    •       System.out.println(" The message has been Sussesfully put/n/n#########");  
    •   
    •       // Close the the Queue and Queue manager objects.  
    •       // 关闭队列和队列管理器对象.  
    •       queue.close();  
    •       qMgr.disconnect();  
    •   
    •      } catch (MQException ex) {  
    •   
    •       System.out.println("An MQ Error Occurred: Completion Code is :/t" +  
    •       ex.completionCode + "/n/n The Reason Code is :/t" + ex.reasonCode);  
    •       ex.printStackTrace();  
    •   
    •      } catch (Exception e) {  
    •   
    •       e.printStackTrace();  
    •   
    •      }  
    •   
    •  }  
    •   
    • }  
原文地址:https://www.cnblogs.com/yanduanduan/p/7157841.html