message.acknowledge() 在使用 Spring Boot 在 ActiveMQ 中读取消息后不会将消息出队

问题描述 投票:0回答:1

我正在尝试通过内容中的 id 读取队列,然后使用 Spring 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);
        }
    }
}
java spring-boot activemq
1个回答
0
投票

这里的问题是您正在使用 browseSelected

 中的 
org.springframework.jms.core.JmsTemplate
 方法。此方法的 JavaDoc 指出:

浏览 JMS 队列中选定的消息。回调提供对 JMS Session 和 QueueBrowser 的访问,以便浏览队列并对内容做出反应。 [强调我的]

当您使用

QueueBrowser
检查消息时,您无法将其从队列中删除。
QueueBrowser
的 JavaDoc 指出:

客户端使用 QueueBrowser 对象查看队列上的消息而不删除它们。

尝试以不同的方式接收消息(例如使用

doReceive
)。

© www.soinside.com 2019 - 2024. All rights reserved.