根据WebSphere MQ的JAVA恳求回复实例ITeye - AG环亚娱乐集团

根据WebSphere MQ的JAVA恳求回复实例ITeye

2019年03月28日08时42分49秒 | 作者: 静枫 | 标签: 行列,音讯,本地 | 浏览: 355

        Requester.java将音讯发送到本地的长途发送行列,然后从本地行列中读取呼应音讯,Response.java从本地行列(此本地行列和Requester.java中的长途行列对应)中获取音讯,判别音讯的类型如果是MQMT_REQUEST音讯,就往长途行列(此长途行列和Requester.java中的本地行列对应)中发送回复音讯。

        先运转Requester.java,并在Requester.java获取音讯超时之前运转Response.java。不然,Requester.java会抛出如下反常。

MQJE001: 完结代码为 2,原由于 2033。
An MQ Error Occurred: Completion Code is :t2nn The Reason Code is :t2033
com.ibm.mq.MQException: MQJE001: 完结代码为 2,原由于 2033。
 at com.ibm.mq.MQDestination.getInt(MQDestination.java:647)
 at com.ibm.mq.MQDestination.get(MQDestination.java:456)
 at com.bijian.study.Requester.main(Requester.java:68)

Requester.java

package com.bijian.study;
import com.ibm.mq.*;
public class Requester {
 public static void main(String args[]) {
 try {
 String hostName = "10.38.25.205";
 int port = 1418;
 String channel = "DC.SVRCONN";
 String qManager = "M01";
 String requestQueue = "REMOTEQ";
 String replyToQueue = "LOCALQ";
 String replyToQueueManager = "M01";
 // Set up the MQEnvironment properties for Client Connections
 MQEnvironment.hostname = hostName;
 MQEnvironment.channel = channel;
 MQEnvironment.port = port;
 MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
 MQEnvironment.CCSID = 1381;
 // Connection To the Queue Manager
 MQQueueManager qMgr = new MQQueueManager(qManager);
 int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
 // Open the queue
 MQQueue queue = qMgr.accessQueue(requestQueue, openOptions, null, null, null);
 // Set the put message options , we will use the default setting.
 MQPutMessageOptions pmo = new MQPutMessageOptions();
 pmo.options = pmo.options + MQC.MQPMO_NEW_MSG_ID;
 pmo.options = pmo.options + MQC.MQPMO_SYNCPOINT;
 MQMessage outMsg = new MQMessage();
 // Create the message buffer
 outMsg.format = MQC.MQFMT_STRING;
 // Set the MQMD format field.
 outMsg.messageFlags = MQC.MQMT_REQUEST;
 outMsg.replyToQueueName = replyToQueue;
 outMsg.replyToQueueManagerName = replyToQueueManager;
 // Prepare message with user data
 String msgString = "Test Request Message from Requester program ";
 outMsg.writeString(msgString);
 // Now we put The message on the Queue
 queue.put(outMsg, pmo);
 // Commit the transaction.
 qMgr.commit();
 System.out.println(" The message has been Sussesfully putnn#");
 // Close the the Request Queue
 queue.close();
 // Set openOption for response queue
 openOptions = MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING;
 MQQueue respQueue = qMgr.accessQueue(replyToQueue, openOptions, null, null, null);
 MQMessage respMessage = new MQMessage();
 MQGetMessageOptions gmo = new MQGetMessageOptions();
 gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;
 // Get messages under syncpoint control
 gmo.options = gmo.options + MQC.MQGMO_WAIT;
 // Wait for Response Message
 gmo.matchOptions = MQC.MQMO_MATCH_CORREL_ID;
 gmo.waitInterval = 10000;
 respMessage.correlationId = outMsg.messageId;
 System.out.println("The response message correlID : " + respMessage.correlationId);
 // Get the response message.
 respQueue.get(respMessage, gmo);
 String response = respMessage.readString(respMessage.getMessageLength());
 System.out.println("The response message is : " + response);
 qMgr.commit();
 respQueue.close();
 qMgr.disconnect();
 } catch (MQException ex) {
 System.out.println("An MQ Error Occurred: Completion Code is :t" + ex.completionCode
 + "nn The Reason Code is :t" + ex.reasonCode);
 ex.printStackTrace();
 } catch (Exception e) {
 e.printStackTrace();
}

Response.java

package com.bijian.study;
import com.ibm.mq.*;
public class Response {
 public static void main(String args[]) {
 try {
 String hostName = "10.38.66.58";
 int port = 1414;
 String channel = "DC.SVRCONN";
 String qManager = "M02";
 String qName = "LOCALQ";
 String replyToQueue = "REMOTEQ";
 String replyToQueueManager = "M02";
 // Set up the MQEnvironment properties for Client
 // Connections
 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_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING;
 // Open the queue
 MQQueue queue = qMgr.accessQueue(qName, openOptions, null, null, null);
 // Set the put message options.
 MQGetMessageOptions gmo = new MQGetMessageOptions();
 gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;
 // Get messages under syncpoint control
 gmo.options = gmo.options + MQC.MQGMO_WAIT;
 // Wait if no messages on the Queue
 gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;
 // Fail if QeueManager Quiescing
 gmo.waitInterval = 3000;
 // Sets the time limit for the wait.
 * 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.
 * 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 inMsg = new MQMessage();
 // Create the message buffer Get the message from the queue on to the message buffer.
 queue.get(inMsg, gmo);
 // Read the User data from the message.
 String msgString = inMsg.readString(inMsg.getMessageLength());
 System.out.println(" The Message from the Queue is : " + msgString);
 // Check if message if of type request message and reply to the
 // request.
 if (inMsg.messageFlags  MQC.MQMT_REQUEST) {
 System.out.println("Preparing To Reply To the Request ");
 String replyQueueName = replyToQueue;
 openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
 MQQueue respQueue = qMgr.accessQueue(replyQueueName, openOptions, replyToQueueManager, null, //inMsg.replyToQueueManagerName, null,
 null);
 MQMessage respMessage = new MQMessage();
 respMessage.correlationId = inMsg.messageId;
 System.out.println("The response CorrelID " + respMessage.correlationId);
 MQPutMessageOptions pmo = new MQPutMessageOptions();
 respMessage.format = MQC.MQFMT_STRING;
 respMessage.messageFlags = MQC.MQMT_REPLY;
 String response = "Reply from the Responder Program ";
 respMessage.writeString(response);
 respQueue.put(respMessage, pmo);
 System.out.println("The response Successfully send ");
 qMgr.commit();
 respQueue.close();
 queue.close();
 qMgr.disconnect();
 } catch (MQException ex) {
 System.out.println("An MQ Error Occurred: Completion Code is :t" + ex.completionCode
 + "nn The Reason Code is :t" + ex.reasonCode);
 ex.printStackTrace();
 } catch (Exception e) {
 e.printStackTrace();
}

 

参阅文章:http://www.cnblogs.com/windows/archive/2012/09/25/2701620.html

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表AG环亚娱乐集团立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章