10477777 发表于 2017-5-25 11:57:19

IBM MQ系列编程3--------接收消息

import com.ibm.mq.*; //Include the WebSphere MQ classes for Java package

public class MQReceiver {
    // define the name of the QueueManager
private static String qManager;
    // and define the name of the Queue
private static String qName;
   
    // main method: simply call the runReceiver() method
    public static void main(String args[]) {
      if (args == null || args.length != 1) {
System.out.println("needs one argument: <Queue Name>");
System.exit (0);
      }
      new MQReceiver().runReceiver(args);
    }

    public void runReceiver(String args[]) {
      qName = args;
      qManager = System.getProperty ("message.queue.manager");
      try {
            // Create a connection to the QueueManager
System.out.println("Connecting to queue manager: "+qManager);
MQEnvironment.channel = System.getProperty ("message.chanel.name");
MQEnvironment.hostname = System.getProperty ("message.queue.server");
MQEnvironment.port = new Integer ((System.getProperty ("message.queue.port"))).intValue();
MQEnvironment.CCSID = new Integer ((System.getProperty ("message.queue.ccsid"))).intValue();

            MQQueueManager qMgr = new MQQueueManager(qManager);

            // Now specify the queue that we wish to open and the open options
            System.out.println("Accessing queue: "+qName);

            // 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();

            // Set up the options on the queue we wish to open
    int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING;
            MQQueue queue = qMgr.accessQueue(qName, openOptions);
    while (true) {
      // Get the message off the queue.
      gmo.options = MQC.MQGMO_WAIT | MQC.MQGMO_CONVERT;
      gmo.waitInterval = 15000000; // 15000 seconds
      gmo.matchOptions = MQC.MQMO_NONE;

      rcvMessage.correlationId = MQC.MQCI_NONE;
      rcvMessage.messageId = MQC.MQMI_NONE;
      rcvMessage.groupId = MQC.MQGI_NONE;
      rcvMessage.clearMessage();

      queue.get(rcvMessage, gmo);
      //      String msgText = rcvMessage.readUTF();
      String msgText = rcvMessage.readStringOfByteLength( rcvMessage.getMessageLength( ) );
      System.out.println("The message is: " + msgText);
      if (msgText.equals ("exit"))
break;
    }
    // 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);
      }
    }
}
页: [1]
查看完整版本: IBM MQ系列编程3--------接收消息