我尝试通过队列内容中的id读取队列,然后使用Sping Boot 和ActiveMQ Classic将它们出队。
为此,我创建了下面的代码。代码根据requestId从响应中拉取队列,执行一些操作然后返回。代码抓取队列没有任何问题。但它并不从ActiveMQ中dequeue
。
Object obj = jmsTemplate.browseSelected("responses", "requestId='"+id+"'", new BrowserCallback() {
@Override
public ResponseEntity<OperationResultResponse> doInJms(Session session, QueueBrowser browser) throws JMSException {
Enumeration<?> enumeration = browser.getEnumeration();
if (enumeration.hasMoreElements()) {
Message message = (Message) enumeration.nextElement();
// process the message
if (message instanceof ActiveMQObjectMessage objectMessage){
try{
// extract the requestId and put in QueueResponse object
QueueResponse queueResponse = (QueueResponse) objectMessage.getObject();
String requestId = queueResponse.getRequestId();
// some other operations..
return something...;
}catch (Exception exception){
// handle error
return new ResponseEntity<>( HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
// Handle the case where queue not exist
return new ResponseEntity<>( HttpStatus.NOT_FOUND);
}
为了解决这个问题,我将message.acknowledge()
添加到try
块中,并将acknowledge-mode
更改为client
,但没有任何效果。我也尝试添加session.close()
,但仍然没有效果。
我用调试器跟踪了acknowledge
,我注意到在函数中,acknowledgeCallback
是null
。这使得代码不执行acknowledge
我认为。我不知道如何使它非空。
public void acknowledge() throws JMSException {
if (this.acknowledgeCallback != null) {
try {
this.acknowledgeCallback.execute();
} catch (JMSException var2) {
throw var2;
} catch (Throwable var3) {
throw JMSExceptionSupport.create(var3);
}
}
}
1条答案
按热度按时间zysjyyx41#
这里的问题是您正在使用
org.springframework.jms.core.JmsTemplate
中的browseSelected
方法。此方法的JavaDoc声明:浏览JMS队列中的选定消息。回调提供对JMS Session和QueueBrowser的访问权限,以便浏览队列并对内容做出React。[强调我的]
当你检查带有
QueueBrowser
的消息时,你 * 不能 * 将它们从队列中删除。JavaDoc forQueueBrowser
声明:客户端使用QueueBrowser对象查看队列上的消息,而不删除它们。
尝试以不同的方式接收消息(例如:使用
doReceive
)。