我正在try 通过队列内容中的id读取队列,然后使用Spring Boot和ActiveMQ Classic将其出列.
为此,我创建了下面的代码.代码根据请求ID从响应中拉出一个队列,执行一些操作,然后返回.代码可以毫无问题地抓取队列.但它不是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);
}
为了解决这个问题,我在try
块中添加了message.acknowledge()
,并将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);
}
}
}