`
a3mao
  • 浏览: 559526 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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[0];
        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 楼 sjz64472418 2013-01-24  
哥们,请教个问题,如果是从队列中按照messageI的获取指定的消息,以获取之后就删除的方式获取,应该怎么获取呢?

相关推荐

Global site tag (gtag.js) - Google Analytics