`

基于WebSphere MQ的JAVA请求回复实例

阅读更多

        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

分享到:
评论

相关推荐

    基于WebSphere MQ发送消息的简单JMS实例

    本文将深入探讨一个基于WebSphere MQ的简单Java Message Service (JMS) 实例,帮助你理解如何在实际项目中实现消息的发送。 首先,我们需要了解JMS。JMS是Java平台上的一个标准接口,它定义了用于在分布式环境中...

    Java监控WebSphere MQ 7.5中间件信息

    Java监控WebSphere MQ 7.5中间件信息主要涉及到的是如何使用Java编程语言与IBM的WebSphere MQ消息中间件进行交互,以便收集、分析和管理MQ的运行时信息。WebSphere MQ,以前被称为MQSeries,是IBM提供的一种企业级的...

    Websphere MQ入门教程7

    最后,书籍深入到应用程序的设计和实现,包括编程接口(如Java Message Service, JMS)的使用、编写消息驱动的程序,以及提供实例程序帮助读者巩固所学知识。 综上所述,《Websphere MQ入门教程7》不仅涵盖了...

    IBM Websphere MQ PCF 编程实例

    在"IBM WebSphere MQ PCF 编程实例"中,我们将探讨如何使用PCF API来实现以下关键知识点: 1. **PCF消息结构**:PCF消息由一组参数组成,每个参数都有一个类型、代码和值。这些参数用于定义对MQ对象的操作,如创建...

    Websphere+MQ入门教程7

    【IBM WebSphere MQ入门教程7】是一份专为初学者设计的详细指南,旨在帮助读者理解和掌握IBM的这款强大...通过学习这份教程,你将能够自信地应对各种基于WebSphere MQ的消息传递挑战,为企业的IT架构增添稳固的基石。

    WebSphere MQ开发JMS

    【WebSphere MQ开发JMS】涉及的关键知识点包括Java消息服务(JMS)、Enterprise JavaBean (EJB)、无状态会话Bean(Stateless Session Bean,SSB)、消息驱动Bean(Message-Driven Bean,MDB)、Java Naming and ...

    IBM MQ 入门实例

    IBM MQ 入门实例 IBM MQ(Message Queue)是一种应用程序对应用程序的通信方法,应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接它们。MQ 环境基础知识点: 1. 消息队列...

    WebSphere_MQ_V7_Data_Sheet.rar_MQ7

    WebSphere MQ V7 进一步加强了与Service-Oriented Architecture (SOA)的集成,提供更灵活的消息传递模式,如请求-响应、发布-订阅等,支持企业构建松耦合、可复用的服务架构。 **6. JMS 1.1 和WS-Messaging 支持** ...

    MQ 自己做的小系统

    总之,“MQ 自己做的小系统”是一个实践WebSphere MQ消息队列处理的实例,涉及到了Java编程、JSP动态网页开发以及MQ的配置和使用。通过对这个系统的深入学习,开发者可以掌握如何在分布式环境中利用消息队列提高系统...

    MQ简单实例需要安装MQ的软件执行这个代码就能看见效果

    在本实例中,你可能有一个包含Python、Java或任何支持MQ API的编程语言的代码文件。代码通常会包含以下关键部分: 1. **连接配置**:定义连接MQ服务器的参数,如主机、端口、通道、队列管理器名等。 2. **打开/关闭...

    WebSphere Message Broker HttpInput 节点

    在WebSphere Message Broker(WMB,以前称为WebSphere MQ Integrator)环境中,HttpInput节点扮演着接收来自HTTP客户端请求的关键角色。它允许系统与各种基于HTTP的应用程序和服务进行集成,从而实现数据交换和流程...

    WebSphere快速入门

    - **Integrated Solution Console**:用于管理WebSphere实例的图形界面,提供配置、监控和诊断等功能。 - **Deployment Manager**:用于集中管理多个WebSphere节点的工具,支持分布式部署和集群配置。 - **...

    IBM websphere 7.0 监控

    在 WebSphere Application Server 7.0 中,提供了专门的功能来监测用户的请求处理时间和整个应用程序栈的响应情况。这种监测可以帮助管理员及时发现性能瓶颈,并采取相应的措施优化系统。 #### 四、监测系统整体...

    《Java中间件技术及其应用开发》-李华飚-源代码

    5. **交易中间件**:如Tuxedo、Websphere MQ,支持复杂的事务处理,确保数据的一致性和完整性。 6. **负载均衡器**:如Nginx、HAProxy,分发网络流量,提高系统的可用性和响应速度。 7. **搜索引擎中间件**:如...

    webservice

    综上所述,本资源提供的“WAS中配置MQ+spring jms操作MQ.docx”文档应该包含了在WAS环境下配置MQ服务和使用Spring JMS进行消息操作的详细步骤和实例,对于理解和实践基于Java的分布式系统通信具有很高的参考价值。...

    JMS与MDB介绍.doc

    5. **JMS Provider**:如IBM WebSphere MQ、Apache ActiveMQ或RabbitMQ等,它们实现JMS规范,提供消息的存储和传输服务。 6. **MDB配置**:在Java EE应用服务器中,MDB需要通过XML部署描述符(如ejb-jar.xml或META-...

    spring jms tomcat 异步消息传递入门实例

    2. **Tomcat与JMS**:虽然Tomcat本身并不直接支持JMS,但可以在Tomcat中部署一个支持JMS的容器,如Apache ActiveMQ或IBM WebSphere MQ。这些服务器将作为消息代理,接收和分发消息。在Tomcat中配置JMS,需要在`...

Global site tag (gtag.js) - Google Analytics