Acknowledgement from Consumer in ActiveMQ

百般思念 提交于 2019-12-11 07:38:17

问题


I am writing a Java application using ActiveMQ. I have a Producer Class (which takes user input) and a Consumer Class with a listener, so that as soon as messages arrive, Message Listener executes the onMessage(msg) function. My question, though, is when does a consumer sends back the acknowledgement to the broker, so that the msg is de-queued from the broker? Is it after completing the actions written in the onMessage(msg) function or is it just when the onMessage(msg) function is invoked?


回答1:


In the normal case the ack is sent after the onMessage completes since it needs to handle the case of onMessage throwing an exception.




回答2:


Depends how it's configured, and in part on how you use transactions.

Are you using a message-driven bean? In that case, i believe the acknowledgement will be sent when the transaction is successfully committed. See the specification (specifically the 'ejbcore' version), section 5.4.14 Message Acknowledgment for JMS Message-Driven Beans, which says:

Message acknowledgment is automatically handled by the container. If the message-driven bean uses container-managed transaction demarcation, message acknowledgment is handled automatically as a part ofthe transaction commit.

It goes on to say:

If bean-managed transaction demarcation is used, the message receipt cannot be part of the bean-managed transaction, and, in this case, the receipt is acknowledged by the container. If bean-managed transaction demarcation is used, the Bean Provider can indicate whether JMS AUTO_ACKNOWLEDGE semantics or DUPS_OK_ACKNOWLEDGE semantics should apply by using the activationConfig element of the MessageDriven annotation or by using the activation-config-property deployment descriptor element.

But since you really shouldn't be using bean-managed transactions, that is hopefully irrelevant.



来源:https://stackoverflow.com/questions/9532948/acknowledgement-from-consumer-in-activemq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!