Getting a simple Spring JMS client acknowledge to work

廉价感情. 提交于 2019-11-30 11:58:24
Tarun

Read this documentation: Spring JMS container does not use the message.acknowledge()

The listener container offers the following message acknowledgment options:

"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.

I found the answer at http://ourcraft.wordpress.com/2008/07/21/simple-jms-transaction-rollbacks-work/

It appears it works well if you change acknowledge="transacted" and make sure you throw new RuntimeException("Message could not be consumed. Roll back transaction"); at the end of the OnMessage() routine.

Still no idea what acknowledge="client" achieves though

gelin

Nowadays Spring provides good wrapper over plain JMS message listeners.

See JavaDocs of AbstractMessageListenerContainer.

"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).

So, when you define a @JmsListener method, the acknowledgement is sent automatically when it's completed successfully, but you can throw an exception to receive the message again.

In my practice client acknowledgement rarely if ever used. Typical setup is auto acknowledge so if your code returns from onMessage() normally (w/o exception) the message is automatically acknowledged. If your code throws exception from onMessage() there is no acknowledgement and message typically would be re-delivered up to pre-configured number of times, after which it would typically be discarded or put into dead message queue.

In your case, from JMS server point of view, it looks like client asked for message but never acknowledged so it's still 'being processed' by the client. In such case message would be invisible for other consumers on the same Queue so you might get the impression that message was 'take off the queue' while as the matter of fact it's still there. Obviously you won't see such message in dead message queue either.

I suggest you read JMS specification to get clear understanding of different acknowledgement modes.

Call acknowledge() method on message in your consumer.

Consider the following scenario: An application receives but does not acknowledge a message. The application receives a subsequent message and acknowledges it. What happens to the former message? The former message is also considered acknowledged. Generally, acknowledging a particular message acknowledges all prior messages the session receives. In the above output, only message 5 is explicitly acknowledged. All the messages before message 5 are implicitly acknowledged. Messages after message 5 are not acknowledged.

For more details refer this article

Also Check this article Sun Java System Message Queue 4.3 Developer's Guide for Java Clients

Use following code it will work.

<bean id="{containerName}"  class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref={connectionFactoryBean} />
    <property name="destinationName" ref="{queue}" />
    <property name="messageListener" ref="{listner}" />
    <property name="sessionAcknowledgeModeName" value="CLIENT_ACKNOWLEDGE"/>
</bean>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!