gpt4 book ai didi

java - JMS 事务处理标志和确认模式的区别

转载 作者:行者123 更新时间:2023-12-05 07:53:10 24 4
gpt4 key购买 nike

消息生成器组件有这个:

session = connection.createSession(false,
Session.CLIENT_ACKNOWLEDGE);
producer = session.createProducer(session.createQueue(queueName));

我正在使用 AWS SQS 异步发送消息

我假设消息将在队列中,直到它被显式确认。(因为客户端确认模式)

然而它被从队列中移除。

消息消费者组件:

我编写了一个异步消费者来从队列中提取消息。这是 spring 配置文件;

<bean id="ConnectionFactoryBuilder"
class="com.amazon.sqs.javamessaging.SQSConnectionFactory$Builder">
<property name="regionName" value="us-east-1" />
<property name="numberOfMessagesToPrefetch" value="1" />
<property name="awsCredentialsProvider" ref="CredentialsProviderBean" />
</bean>

<bean id="ConnectionFactory" class="com.amazon.sqs.javamessaging.SQSConnectionFactory"
factory-bean="ConnectionFactoryBuilder" factory-method="build" />

<bean id="Connection" class="javax.jms.Connection" factory-bean="ConnectionFactory"
factory-method="createConnection" init-method="start" destroy-method="close" />

<bean id="QueueName" class="java.lang.String">
<constructor-arg value="${sqs.queueName}" />
</bean>

<bean id="amazonMessageListener" class="com.myapp.AsyncMessageListener" />

<bean id="messageListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<property name="delegate" ref="amazonMessageListener" />
<property name="defaultListenerMethod" value="onMessage" />
<property name="messageConverter">
<null />
</property>
</bean>

<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="ConnectionFactory" />
<property name="destinationName" ref="QueueName" />
<property name="messageListener" ref="messageListener" />
<property name="sessionAcknowledgeMode" value="2"/>
</bean>

我使用属性 sessionAcknowledgeMode 为我的消费者配置了客户端确认模式。但是消息仍然从队列中删除,而消费者没有使用 message.acknowledge() 明确确认

最佳答案

根据 AbstractMessageListenerContainer (DefaultMessageListenerContainer 的父类(super class))文档:

"sessionAcknowledgeMode" set to "CLIENT_ACKNOWLEDGE": Automatic message acknowledgment after successful listener execution; best-effort redelivery in case of a user exception thrown as well as in case of other listener execution interruptions (such as the JVM dying).

因此,当使用 DefaultMessageListenerContainer 时,如果监听器中的 onMessage() 方法无一异常(exception)地返回,spring 会自动确认该消息。

关于java - JMS 事务处理标志和确认模式的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32968822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com