JMS with proper transactional properties

The main reason I am interested in JMS (inside the application) is for the transactional properties. JMS allows you to start an asynchronous task and make sure that the task is only scheduled if the transaction in which the JMS message was sent succeeds.

Similarly, JMS is smart enough to retry receiving the message until it was processed successfully (potentially with a backout scenario to assure that retries don’t hog the system).

This sounds great, but configuring JMS to work like this is hard.

The generic solution is to use XA transactions. In that case the transactions of your data sources (like your database) and your JMS provider are synchronized.

If you are only using one database, then using that database for persistence of your JMS messages avoids the need for XA transactions. There is only one data source, so no transactions to synchronize.

Using Spring and ActiveMQ this can be done using a configuration like the following. This only uses the JMS inside the application, connection from the outside is not possible. This example persists in a PostgreSQL database.

<!--  Embedded ActiveMQ Broker -->
<amq:broker id="broker" useJmx="false" persistent="true">
        <amq:transportConnector uri="tcp://localhost:0" />
        <amq:jdbcPersistenceAdapter changeAutoCommitAllowed="false" createTablesOnStartup="false" useDatabaseLock="false">
            <amq:dataSource><ref bean="dataSource" /></amq:dataSource>
<!--  ActiveMQ Destinations  -->
<amq:queue id="zzzz" physicalName="" />
<!-- JMS ConnectionFactory to use, configuring the embedded broker using XML -->
<amq:connectionFactory id="jmsFactory" brokerURL="vm://localhost" />
<bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager">
    <property name="connectionFactory" ref="jmsFactory" />
<bean id="jmsConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"
      p:targetConnectionFactory-ref="jmsFactory" />
<bean id="jmsListenerContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
    <property name="connectionFactory" ref="jmsConnectionFactory" />
    <property name="sessionTransacted" value="true" />
    <property name="concurrency" value="3-10" />
<bean id="jmsProducerTemplate" class="org.springframework.jms.core.JmsTemplate"
      p:sessionTransacted="true" />

Your messages are now only delivered when your transaction in which the message was sent succeeds.
When the transaction in which the message is received rolls back, the message will be redelivered.

If needed, you can control how many times the message should be attempted to be processed. This can be done using code like the following:

@JmsListener(destination = "")
private static final int MAX_DELIVERY_ATTEMPTS = 3;
public void onMessage(Message message) {
    try {
        int count = message.getIntProperty("JMSXDeliveryCount");
        if (count > MAX_DELIVERY_ATTEMPTS) {
            LOG.warn("Processing JMS message {} failed {} times. It will not be retried.", message, MAX_DELIVERY_ATTEMPTS);
        // ..... normal handling of message
    } catch (JMSException e) {
        // ..... handle exception

Leave a Reply

Your email address will not be published. Required fields are marked *

question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen