Chapter 20. Configuring Pre-Acknowledgments

JMS specifies three acknowledgement modes:

  • AUTO_ACKNOWLEDGE
  • CLIENT_ACKNOWLEDGE
  • DUPS_OK_ACKNOWLEDGE

In some cases you can afford to lose messages in the event of a failure, so it would make sense to acknowledge the message on the server before delivering it to the client. This extra mode is supported by JBoss EAP messaging and is called pre-acknowledge mode.

The disadvantage of pre-acknowledging on the server before delivery is that the message will be lost if the server’s system crashes after acknowledging the message but before it is delivered to the client. In that case, the message is lost and will not be recovered when the system restarts.

Depending on your messaging case, pre-acknowledge mode can avoid extra network traffic and CPU usage at the cost of coping with message loss.

An example use case for pre-acknowledgement is for stock price update messages. With these messages, it might be reasonable to lose a message in event of a crash since the next price update message will arrive soon, overriding the previous price.

Note

If you use pre-acknowledge mode, you will lose transactional semantics for messages being consumed since they are being acknowledged first on the server, not when you commit the transaction.

20.1. Configuring the Server

A connection factory can be configured to use pre-acknowledge mode by setting its pre-acknowledge attribute to true using the management CLI as below:

/subsystem=messaging-activemq/server=default/connection-factory=RemoteConnectionFactory:write-attribute(name=pre-acknowledge,value=true)

20.2. Configuring the Client

Pre-acknowledge mode can be configured in the client’s JNDI context environment, for example, in the jndi.properties file:

java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
connection.ConnectionFactory=tcp://localhost:8080?preAcknowledge=true

Alternatively, to use pre-acknowledge mode using the JMS API, create a JMS Session with the ActiveMQSession.PRE_ACKNOWLEDGE constant.

// messages will be acknowledge on the server *before* being delivered to the client
Session session = connection.createSession(false, ActiveMQJMSConstants.PRE_ACKNOWLEDGE);