How do you set a message selector using Java API?

拟墨画扇 提交于 2020-01-15 02:38:06

问题


I'm trying to write a simple test case to pull messages from a queue based on a message property, hitting a 7.5.0.3 QMgr and using the 7.5.0.3 client jars.

Everything I have seen online says that I need to specify the message selector when I open the queue. I'm fine with that, but I only see two ways to open it:

MQQueueManager.accessQueue(
    String queueName, 
    int openOptions);

MQQueueManager.accessQueue(
    String queueName, 
    int openOptions, 
    String queueMgr, 
    String dynamicQueueName, 
    String altUserId);

Neither of these allow me to specify a message selector. I'm running this from a command line batch application, not in an app server, so using JMS selectors is not possible.

Here is the IBM documentation on selectors: WebSphere MQ Message Selectors which shows that selection must happen as part of the MQOPEN call.


回答1:


MQ JMS API provides the type of message selection syntax you are looking for. The base MQ Java API provides message selection based on MessageId and CorrelationId and it does not yet provide type selection syntax you are looking for. The documentation link you provided is for MQ C API.

Using MQ JMS API, message selection can be done as shown here:

      // Create JMS objects
      connection = cf.createConnection();
      session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Create queue destination 
      Destination queDest= session.createQueue(que);

      // Create consumer with selector
      String selector = "category='bucket1'";         
      MessageConsumer cons= session.createConsumer(queDest, selector);

      connection.start();
      // receive messages
      Message inMessage = cons.receive(5000);



回答2:


You should specify selector when trying to read message from queue, like below:

            MQMessage ResponseMsg = new MQMessage();
            ResponseMsg.correlationId = CorrelationId;
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            gmo.options = MQConstants.MQGMO_WAIT;
            gmo.waitInterval = WaitTime * 1000;
            gmo.matchOptions = MQConstants.MQMO_MATCH_CORREL_ID;
            ResponseQueue.get(ResponseMsg, gmo);


来源:https://stackoverflow.com/questions/29016024/how-do-you-set-a-message-selector-using-java-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!