如何使用JmsTemplate进行手动确认并从Rabbitmq队列中删除消息

如何使用JmsTemplate进行手动确认并从Rabbitmq队列中删除消息,第1张

如何使用JmsTemplate进行手动确认并从Rabbitmq队列中删除消息

您没有使用

JmsTemplate
,而是使用
SimpleMessageListenerContainer
来接收消息。

如果您 使用模板,你将不得不使用的

execute
方法有
SessionCallback
,因为必须确认其内接收消息的会话的范围内

但是,使用

SimpleMessageListenerContainer
,您只需将设置
sessionAcknowledgeMode
Session.CLIENT_ACKNOWLEDGE
。参见容器javadocs

/** * Message listener container that uses the plain JMS client API's * {@pre MessageConsumer.setMessageListener()} method to * create concurrent MessageConsumers for the specified listeners. * * <p>This is the simplest form of a message listener container. * It creates a fixed number of JMS Sessions to invoke the listener, * not allowing for dynamic adaptation to runtime demands. Its main * advantage is its low level of complexity and the minimum requirements * on the JMS provider: Not even the ServerSessionPool facility is required. * * <p>See the {@link AbstractMessageListenerContainer} javadoc for details * on acknowledge modes and transaction options. Note that this container * exposes standard JMS behavior for the default "AUTO_ACKNOWLEDGE" mode: * that is, automatic message acknowledgment after listener execution, * with no redelivery in case of a user exception thrown but potential * redelivery in case of the JVM dying during listener execution. * * <p>For a different style of MessageListener handling, through looped * {@pre MessageConsumer.receive()} calls that also allow for * transactional reception of messages (registering them with XA transactions), * see {@link DefaultMessageListenerContainer}.   ...

编辑

使用时

JmsTemplate
,您必须在会话范围内进行工作-这是…

首先,您必须在模板中启用客户端确认…

this.jmsTemplate.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);

然后,将

execute
方法与
SessionCallback
…一起使用

Boolean result = this.jmsTemplate.execute(session -> {    MessageConsumer consumer = session.createConsumer( this.jmsTemplate.getDestinationResolver().resolveDestinationName(session, "bar", false));    String result = null;    try {        Message received = consumer.receive(5000);        if (received != null) { result = (String) this.jmsTemplate.getMessageConverter().fromMessage(received); // Do some stuff here. received.acknowledge(); return true;        }    }    catch (Exception e) {        return false;    }    finally {        consumer.close();    }}, true);


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4893964.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存