mambolis
mambolis

Reputation: 323

Container Managed Transactions and Session.AUTO_ACKNOWLEDGE

In the The Java EE 6 Tutorial, Chapter 17 A Message-Driven Bean Example , I came across this:

 @MessageDriven(mappedName="jms/Queue", activationConfig =  {
    @ActivationConfigProperty(propertyName = "acknowledgeMode",
                              propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType",
                              propertyValue = "javax.jms.Queue")
})
public class SimpleMessageBean implements MessageListener {
    @Resource
    private MessageDrivenContext mdc;
...

Since the MDB is transacted - (default value for transaction attributes points to TransactionAttributeType.REQUIRED and TransactionManagementType.CONTAINER))

how makes @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") any sence?

In Controlling Message Acknowledgment I read

In transacted sessions (see Using JMS API Local Transactions), acknowledgment happens automatically when a transaction is committed. If a transaction is rolled back, all consumed messages are redelivered.

This got me confused; I would expect not to declare acknowledgeMode here (since we are in a transacted session)

Upvotes: 1

Views: 995

Answers (1)

Artem
Artem

Reputation: 391

In short: this property won't affect message acknowledgment for MDB using CONTAINER transaction.

For answer you can look EJB 3.2 specification

JMS message-driven beans should not attempt to use the JMS API for message acknowledgment. Mes - sage acknowledgment is automatically handled by the container. If the message-driven bean uses con - tainer-managed transaction demarcation, message acknowledgment is handl ed automatically as a part of the transaction commit. If bean-man aged transaction demarcation is us ed, 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. The property name used to specify the acknowledgment mode is acknowledgeMode . If the acknowledgeMode property is not specified, JMS AUTO_ACKNOWLEDGE semantics are assumed. The value of the acknowledgeMode property must be either Auto-acknowledge or Dups-ok-acknowledge for a JMS message-driven bean.

So in your case this property will be affected only if you switch your bean to BMT by adding @TransactionManagement(TransactionManagementType.BEAN) annotation.

Also I recommend you to read this article about Transaction and redelivery in JMS

Upvotes: 1

Related Questions