DefaultMessageListenerContainer的Javadoc说
强烈建议将“ sessionTransacted”设置为“ true”
或指定一个外部“ transactionManager”。
为什么强烈推荐?这个神秘建议的背后是什么?
最佳答案
如果未处理会话,则在JVM意外关闭的情况下冒着消息丢失的风险。
这是AbstractMessageListenerContainer中各种ack模式的说明:
侦听器容器提供以下消息确认
选项:
"sessionAcknowledgeMode" set to "AUTO_ACKNOWLEDGE" (default): Automatic message acknowledgment before listener execution; no redelivery in case of exception thrown.
"sessionAcknowledgeMode" set to "CLIENT_ACKNOWLEDGE": Automatic message acknowledgment after successful listener execution; no redelivery in case of exception thrown.
"sessionAcknowledgeMode" set to "DUPS_OK_ACKNOWLEDGE": Lazy message acknowledgment during or after listener execution; potential redelivery in case of exception thrown.
"sessionTransacted" set to "true": Transactional acknowledgment after successful listener execution; guaranteed redelivery in case of exception thrown.
具体的行为可能会因具体的侦听器而异
容器和使用的JMS提供程序。
由于消息传递通常基于有保证的传递,因此建议使用将提供最佳安全措施以防止消息丢失的方法。您必须为特定用例决定是否值得。