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:transportConnectors> <amq:transportConnector uri="tcp://localhost:0" /> </amq:transportConnectors> <amq:persistenceAdapter> <amq:jdbcPersistenceAdapter changeAutoCommitAllowed="false" createTablesOnStartup="false" useDatabaseLock="false"> <amq:adapter><amq:postgresql-jdbc-adapter/></amq:adapter> <amq:dataSource><ref bean="dataSource" /></amq:dataSource> </amq:jdbcPersistenceAdapter> </amq:persistenceAdapter> </amq:broker> <!-- ActiveMQ Destinations --> <amq:queue id="zzzz" physicalName="your.queues.physical.name.ZzzzQueue" /> <!-- 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> <bean id="jmsConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory" depends-on="broker" p:targetConnectionFactory-ref="jmsFactory" /> <jms:annotation-driven/> <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> <bean id="jmsProducerTemplate" class="org.springframework.jms.core.JmsTemplate" p:connectionFactory-ref="jmsConnectionFactory" 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:
@Transactional @JmsListener(destination = "your.queues.physical.name.ZzzzQueue") 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); return; } // ..... normal handling of message } catch (JMSException e) { // ..... handle exception } } |
Leave a Reply