我正在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,我注意到在函数中,acknowledgeCallbacknull.这使得代码不能执行我认为的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);
        }
    }
}

推荐答案

这里的问题是,您使用的是org.springframework.jms.core.JmsTemplate中的browseSelected方法.此方法的JavaDoc声明:

在JMS队列中 Select Browse条消息.回调提供对JMS会话和QueueBrowser的访问,以便浏览队列并对内容做出react .[强调我的]

当您判断带有QueueBrowser的消息时,您需要can't将其从队列中删除.适用于QueueBrowser个国家的Java文档声明:

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

try 以不同的方式接收消息(例如使用doReceive).

Java相关问答推荐

将偶数元素移动到数组的前面,同时保持相对顺序

如何使用CSS为选定但未聚焦的表格行设置背景 colored颜色 ?

找到允许的最大底片

如何在带有Micronaut的YAML中使用包含特殊字符的字符串作为键

对某一Hyroby控制器禁用@cacheable

RichFaces 3.x-Spring Boot-迁移web.xml

相同的Java SerializedLambda为implMethodKind返回不同的结果

生成桥方法以解决具有相同擦除的冲突方法

如何在EXCEL单元格中添加形状和文本

如何在JavaFX中处理多个按钮

如何从日志(log)行中删除包名称?

在Java 15应用程序中运行Java脚本和Python代码

是否为计划任务补偿系统睡眠?

将ByteBuffer异步写入InputStream或Channel或类似对象

为什么Instant没有从UTC转换为PostgreSQL的时区?

升级版本后出现非法访问错误

这是JavaFX SceneBuilder的错误吗?

UuidGenerator Bean 类型不匹配?

对于 Hangman 游戏,索引 0 超出长度 0 的范围

如何使用 JDBC 更改 Postgres Enum 类型