Selvakumar
Selvakumar

Reputation: 309

How to join Spring JMS transactions from two different connection factories?

I am using different connection factories for sending and receiving messages, having trouble with partial commit issues incase of delivey failures. jms:message-driven-channel-adapter uses the receiveConnectionFactory ro receive the messages from the queue. jms:outbound-channel-adapter uses the deliverConnectionFactory to send the messages multiple to downstream queues. We have only one JmsTransactionManager which uses the receiveConnectionFactory and the jms:outbound-channel-adapter configured with session-transacted="true".

<beans>
    <bean id="transactionManager"
        class="org.springframework.jms.connection.JmsTransactionManager">
        <property name="connectionFactory" ref="receiveConnectionFactory" />
    </bean>
    <bean id="receiveConnectionFactory"
        class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory">
            <bean class="com.ibm.mq.jms.MQQueueConnectionFactory">
                <property name="hostName" value="${mq.host}" />
                <property name="channel" value="${mq.channel}" />
                <property name="port" value="${mq.port}" />
            </bean>
        </property>
        <property name="sessionCacheSize" value="${receive.factory.cachesize}" />
        <property name="cacheProducers" value="${receive.cache.producers.enabled}" />
        <property name="cacheConsumers" value="${receive.cache.consumers.enabled}" />
    </bean>

    <bean id="deliverConnectionFactory"
        class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory">
            <bean class="com.ibm.mq.jms.MQQueueConnectionFactory">
                <property name="hostName" value="${mq.host}" />
                <property name="channel" value="${mq.channel}" />
                <property name="port" value="${mq.port}" />
            </bean>
        </property>
        <property name="sessionCacheSize" value="${send.factory.cachesize}" />
        <property name="cacheProducers" value="${send.cache.producers.enabled}" />
        <property name="cacheConsumers" value="${send.cache.consumers.enabled}" />
    </bean>

    <tx:advice id="txAdviceNew" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="send" propagation="REQUIRES_NEW" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:advisor advice-ref="txAdviceNew" pointcut="bean(inputChannel)" />
        <aop:advisor advice-ref="txAdviceNew" pointcut="bean(errorChannel)" />
    </aop:config>

    <jms:message-driven-channel-adapter
        id="mdchanneladapter" channel="inputChannel" task-executor="myTaskExecutor"
        connection-factory="receiveConnectionFactory" destination="inputQueue"
        error-channel="errorChannel" concurrent-consumers="${num.consumers}"
        max-concurrent-consumers="${max.num.consumers}" max-messages-per-task="${max.messagesPerTask}"
        transaction-manager="transactionManager" />

    <jms:outbound-channel-adapter
        connection-factory="deliverConnectionFactory" session-transacted="true"
        destination-expression="headers.get('Deliver')" explicit-qos-enabled="true" />
</beans>

When there is MQ exception on any one destination, the partial commit occurs and then the failure queue commit happens. I am looking to see if I am missing some configuration to join the transactions so that the partial commit never happens.

I tried with only one connection factory for both send and receive (receiveConnectionFactory) and the parital commit is not happening, everything works as expected.

Upvotes: 1

Views: 994

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121272

I tried with only one connection factory for both send and receive (receiveConnectionFactory) and the parital commit is not happening, everything works as expected.

That's the right way to go in your case.

I see that your two ConnectionFactories are only different by their objects. Everything rest looks like the same target MQ server.

If you definitely can't live with only one ConnectionFactory, you should consider to use JtaTransactionManager or configure org.springframework.data.transaction.ChainedTransactionManager for two JmsTransactionManagers - one per connection factory.

See Dave Syer's article on the matter: https://www.javaworld.com/article/2077963/open-source-tools/distributed-transactions-in-spring--with-and-without-xa.html

Upvotes: 2

Related Questions