现在的位置: 首页 > 综合 > 正文

如何用Java去访问WebSphere MQ

2013年10月10日 ⁄ 综合 ⁄ 共 2629字 ⁄ 字号 评论关闭
Java 访问WebSphere MQ有以下2种方法
一、通过MQ API
这种方法直接调用MQ提供的API来访问,步骤如下:
1. 在MQ Explorer中创建一个Queue Manager,名字为“MQSI_SAMPLE_QM”,
2. 在MQSI_SAMPLE_QM下创建一个local queue, 名字为“lq”,
3. 创建一个Server-Connection channel,名字为“BridgeChannel”,
4. 把<MQ_install_dir>/Java/lib下的Jar包加到ClassPath中,
5. 用下面的程序与MQ进行通信
 
  String qManager = "MQSI_SAMPLE_QM"; //QueueManager name
  String qName = "lq";//Queue Name
  try {
            //configure connection parameters
            MQEnvironment.hostname="9.181.65.87";//MQ Server name or IP
            MQEnvironment.port=1415;//listenr port
            MQEnvironment.channel="BridgeChannel";//Server-Connection Channel
            // Create a connection to the QueueManager
            System.out.println("Connecting to queue manager: "+qManager);
            MQQueueManager qMgr = new MQQueueManager(qManager);
            // Set up the options on the queue we wish to open

            int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
            // Now specify the queue that we wish to open and the open options
            System.out.println("Accessing queue: "+qName);
            MQQueue queue = qMgr.accessQueue(qName, openOptions);
            // Define a simple WebSphere MQ Message ...

            MQMessage msg = new MQMessage();
            // ... and write some text in UTF8 format
            msg.writeUTF("Hello, World!");
            // Specify the default put message options
            MQPutMessageOptions pmo = new MQPutMessageOptions();
            // Put the message to the queue
            System.out.println("Sending a message...");
            queue.put(msg, pmo);
            // Now get the message back again. First define a WebSphere MQ message

            // to receive the data
            MQMessage rcvMessage = new MQMessage();
           
            // Specify default get message options
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            // Get the message off the queue.
            System.out.println("...and getting the message back again");
            queue.get(rcvMessage, gmo);
            // And display the message text...
            String msgText = rcvMessage.readUTF();
            System.out.println("The message is: " + msgText);
            // Close the queue
            System.out.println("Closing the queue");
            queue.close();
            // Disconnect from the QueueManager
            System.out.println("Disconnecting from the Queue Manager");
            qMgr.disconnect();
            System.out.println("Done!");
        }
        catch (MQException ex) {
            System.out.println("A WebSphere MQ Error occured : Completion Code "
                    + ex.completionCode + " Reason Code " + ex.reasonCode);
        }
        catch (java.io.IOException ex) {
            System.out.println("An IOException occured whilst writing to the message buffer: "
                    + ex);
        }
 
二、通过JMS API
这是通用的调用WebSphere MQ的方法,在此略去

抱歉!评论已关闭.