5

What is the correct syntax for selecting messages based on their corresponding JMS ID?

Currently I use:

String selector = "JMSMessageID='ID:305:1:5:1:1'";
messageConsumer = session.createConsumer(getRetryQueue(), selector);

The above syntax works in test with a mocked broker. But towards ActiveMQ the messages are not polled.

Original code:

@Override
public Message readMessageFromRetryQueueByJmsId(String jmsId) throws QueueingException {
    Connection connection = null;
    Session session = null;
    MessageConsumer messageConsumer = null;
    Message message = null;
    try {
        connection = getConnectionFactory().createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        messageConsumer = session.createConsumer(getRetryQueue(), "JMSMessageID='"+jmsId+"'");
        message = messageConsumer.receiveNoWait();
    } catch (JMSException e) {
        throw new QueueingException("Failed to read message from MessageConsumer.");
    } finally {
        try { messageConsumer.close(); } catch (Exception e) { /* NOP */ }
        try { session.close(); } catch (Exception e) { /* NOP */ }
        try { connection.close(); } catch (Exception e) { /* NOP */ }
    }
    return message;
}
aksamit
  • 2,325
  • 8
  • 28
  • 40
  • Format of `JMSMessageID` is vendor specific. Did you try to see what the format is for ActiveMQ? – AlexR May 17 '13 at 15:19
  • Yes it seems to be alright: http://activemq.apache.org/activemq-message-properties.html – aksamit May 17 '13 at 15:22
  • What is all right? The document you sent does not mention the format at all. Try to consume the messages without selector at all and print the property `JMSMessageID` – AlexR May 17 '13 at 15:24
  • The JMS ID is obtained through a queue browser that selects messages based on some criterias and then extracted with: msg.getJMSMessageID() So format should be alright. Syntax also seems to match what I have seen here: https://bitbucket.org/mirror/activemq/src/bf071376b576cb9a1d294822ba721b324070480e/activemq-unit-tests/src/test/java/org/apache/activemq/selector/SelectorTest.java – aksamit May 17 '13 at 16:10
  • It must be something wrong with your other code. That selector is really ok (Given you have such a message id in your queue). Did you remember to start the connection before reading? – Petter Nordlander May 17 '13 at 20:22
  • Thank you! Yes you were right, the connection was never started. – aksamit May 18 '13 at 02:33

1 Answers1

6

Messages were not read since the connection was never started.

Corrected code:

@Override
public Message readMessageFromRetryQueueByJmsId(String jmsId) throws QueueingException {
    Connection connection = null;
    Session session = null;
    MessageConsumer messageConsumer = null;
    Message message = null;
    try {
        connection = getConnectionFactory().createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        connection.start();
        messageConsumer = session.createConsumer(getRetryQueue(), "JMSMessageID='"+jmsId+"'");
        message = messageConsumer.receiveNoWait();
    } catch (JMSException e) {
        throw new QueueingException("Failed to read message from MessageConsumer.");
    } finally {
        try { connection.close(); } catch (Exception e) {}
    }
    return message;
}
Tim Bish
  • 17,475
  • 4
  • 32
  • 42
aksamit
  • 2,325
  • 8
  • 28
  • 40