gpt4 book ai didi

java - MessageConsumer 不消费消息

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:51:14 26 4
gpt4 key购买 nike

我的应用程序在 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);
}

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

/*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();

}

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

最佳答案

找到了,我只需要在开始消费之前添加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();
}

关于java - MessageConsumer 不消费消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11760997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com