Steve
Steve

Reputation: 772

How do you set a message selector using Java API?

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.

Upvotes: 3

Views: 5128

Answers (2)

Shashi
Shashi

Reputation: 15273

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

Upvotes: 5

user3714601
user3714601

Reputation: 1281

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

Upvotes: -1

Related Questions