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
相关推荐
本人自学IBM的WebSphereMQ自学笔记,内有MQ安装文档,...3、WebSphere MQ Java编程实例 11 3.1 开发前步骤 11 3.1.1将消息发送至本地队列 11 3.1.2 将消息发送至远程队列 14 3.1.3 在客户机 - 服务器配置上发送消息 19
WebSphere MQ For JAVA编程实例 主要讲解通过Java语言怎样与MQ进行进行数据通信,里面有示例代码
Java下操作IBM Websphere MQ的项目案例, eclipse工程压缩包, 导入直接可用.
6.2.4 WebSphere MQ对象配置实例 81 6.3通道的维护 83 6.3.1通道的状态 83 6.3.2通道维护命令 84 6.3.3设置MaxChannels和MaxActiveChannels属性 88 6.4配置侦听程序 88 6.4.1 Windows 平台 88 6.4.2 unix 平台 88 ...
NULL 博文链接:https://bijian1013.iteye.com/blog/2308754
WebSphere MQ7.0配置与测试 WebSphere MQ发送接收消息的实现 附java 源码
WebSphere MQ Using Java,MQ作为一种中间件还是不错的,可以由一个系统发送一些消息给MQ Server,然后另外一个系统来取得这些消息。比较方便的实现不同系统、不同语言间的通信。
Websphere Message Broker实践,WebSphere MQ Java编程,Message Broker 计时器节点编程模式,MessageBroker TCPIP通信协议,wmb关于ws服务的引用,WMB连接oracle数据库实践,全部组件
IBM WebSphere MQ是IBM业界领先面向消息的中间件产品,也是MQ系列产品的基础和核心,它使不同的应用程序能够以企业级的性能,在广泛的平台上安全而可靠地通讯。
Websphere MQ Programming Guide,Websphere MQ Using C++,WebSphere MQ Using Java,WEBSPHERE MQ6.0 JAVA编程,WebSphere MQ基础教程,IBM WEBSPHERE MQ教程,精通WebSphere MQ,WebSphere MQ开发快速入门,IBM ...
Java 通过PCF连接WebSphere MQ7.1以上版本获取数据。!!!
IBM WebSphere MQ Java编程(中文版) [消息中间件]
Websphere MQ JavaJMS 客户端的 SSL 配置.doc
多实例队列管理器特征是MQV7.0.1版本之后引进的新特征,它是MQ产品的内置功能,丰富了 MQ 高可用性的解决方案。 用户可以在不同机器上定义并启动此队列管理器的多个实例,包括一个活动实例和一个备用实例。 激活的...
全书共分为3部分共14章,第一部分 WebSphere MQ原理和体系结构,分为两章;第二部分 WebSphere MQ系统管理,分为六章,分别介绍安装、配置、管理、控制命令和问题确定;第三部分 WebSphere MQ应用开发,由五章组成,...
Websphere MQ java编程发送和接收代码
NULL 博文链接:https://bijian1013.iteye.com/blog/2308734
Websphere MQ using java. Websphere MQ class for java and JMS.
IBM Websphere MQ PCF 编程 实例
IBM websphere MQ教学材料0094