MessageConsumer不消费消息

Tom*_*mer 6 java jms message-queue java-ee jboss7.x

我的应用程序在Jboss 7.1.1上运行.我有一个每分钟运行一次的调度程序,需要检查DLQ中是否有消息并在数据库中进行一些更新.

我写了一个消息,消费者听取预定义的自定义DLQ.问题是我可以看到自定义DLQ中有消息但consumer.receiveNoWait()始终返回null.

以下是创建消费者的代码:

/*this is running fine and creating the consumer*/
public DestinationHandlerImpl(ConnectionFactory connectionFactory,
    Destination destination, boolean useTransaction, int delMode,
    boolean isProducer) throws JMSException {
    connection = connectionFactory.createConnection();
    consumer = session.createConsumer(destination);
}
Run Code Online (Sandbox Code Playgroud)

这是使用消息的代码(每隔一分钟运行一次):

/*this always return null, event when there are messages in the queue*/
public <T extends BaseEvent> T recieveMessage()
        throws JMSException {

    Message message = consumer.receiveNoWait(); // ----> always return null!!!

    if (message != null && !(message instanceof ObjectMessage)) {
        throw new IllegalArgumentException(
                "message object has to be of type ObjectMessage");
    }

    // Extract the object from the message
    return message == null ? null : (T) ((ObjectMessage) message).getObject();

}
Run Code Online (Sandbox Code Playgroud)

我已经使用了调试模式,我可以看到消费者目标属性设置为正确的队列,所以我做错了什么?

Tom*_*mer 12

找到它,我只需要connection.start()在开始消费之前添加.

public <T extends BaseEvent> T recieveMessage()
    throws JMSException {

    connection.start(); // --->**added this line**
    Message message = consumer.receiveNoWait(); 

    if (message != null && !(message instanceof ObjectMessage)) {
        throw new IllegalArgumentException(
            "message object has to be of type ObjectMessage");
    }

    // Extract the object from the message
    return message == null ? null : (T) ((ObjectMessage) message).getObject();
}
Run Code Online (Sandbox Code Playgroud)