按Jms ID选择消息的语法

时间:2013-05-17 14:55:22

标签: java jms activemq message-queue

根据相应的JMS ID选择消息的正确语法是什么?

目前我使用:

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

以上语法适用于使用模拟代理的测试。但是对于ActiveMQ,消息不会被轮询。

原始代码:

@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;
}

1 个答案:

答案 0 :(得分:4)

由于从未启动过连接,因此未读取消息。

更正后的代码:

@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;
}