Announcement Announcement Module
No announcement yet.
JMS inside JTA transaction Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • JMS inside JTA transaction

    following is Spring ApplicationContext-jms.xml for using jms queue use in our application

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
        <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
            <property name="environment">
                    <prop key="java.naming.provider.url">localhost:1099</prop>
                    <prop key="java.naming.factory.url.pkgs">org.jnp.interfaces:org.jboss.naming</prop>
                    <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
        <!--reference to jboss jms connection factory , can use httpConnection factory-->
        <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiTemplate">
                <ref bean="jndiTemplate"/>
            <property name="jndiName">
        <!--JMSTemplate bean used to manage all connection and session, it's Spring way to overcome
        the JMS unnecessary repetitives code, set to non persistent mode-->
        <bean id="jmsEmailTemplate" class="org.springframework.jms.core.JmsTemplate">
            <property name="connectionFactory" ref="connectionFactory"/>
            <property name="defaultDestination" ref="sendEmailDestination"/>
            <property name="deliveryPersistent" value="false"/>
            <property name="sessionTransacted" value="true"/>    
        <!--declar destination for sending emails-->
        <bean id="sendEmailDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiTemplate" ref="jndiTemplate"/>
            <property name="jndiName" value="queue/SendMailQueue"/>
        <!--MDP listener bean, change to SimpleMessageListenerContainer if no transaction manager required !!! -->
        <bean id="emailMessageListenerMDP" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
                <bean class="">
                    <property name="mailEngine" ref="mailEngine"/>
                    <property name="mailManager" ref="mailManager"/>
        <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
            <property name="connectionFactory" ref="connectionFactory"/>
            <property name="destination" ref="sendEmailDestination"/>
            <property name="messageListener" ref="emailMessageListenerMDP"/>
            <property name="transactionManager" ref="transactionManager"/>    

    We use the Jboss transaction manager and I tried to add this queue into the global transaction which includes insert new object to DB and send the message,
    In case of failure need to rollback so the MDP will not receive the message,
    As it seems now althought the insert of new object failed the DB is rolled back while the queue not,
    When adding the sessionTransacted property to the JMSTemplate it seems to work fine, is this the right solution?
    cause in the documentation I found the following:

    Note that within a JTA transaction, the parameters passed to
    	 * <code>create(Queue/Topic)Session(boolean transacted, int acknowledgeMode)</code>
    	 * method are not taken into account. Depending on the J2EE transaction context,
    	 * the container makes its own decisions on these values.