4

根据相应的 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;
}
4

1 回答 1

5

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

更正的代码:

@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;
}
于 2013-05-18T02:39:51.420 回答