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

  • JMS inside JTA transaction

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

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
    
        <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
            <property name="environment">
                <props>
                    <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>
                </props>
            </property>
        </bean>
    
        <!--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>
            <property name="jndiName">
                <!--<value>XAConnectionFactory</value>-->            
                  <value>ConnectionFactory</value>
            </property>
        </bean>
         
        <!--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"/>    
         </bean>
    
        <!--declar destination for sending emails-->
        <bean id="sendEmailDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiTemplate" ref="jndiTemplate"/>
            <property name="jndiName" value="queue/SendMailQueue"/>
        </bean>
    
        <!--MDP listener bean, change to SimpleMessageListenerContainer if no transaction manager required !!! -->
        <bean id="emailMessageListenerMDP" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
            <constructor-arg>
                <bean class="com.supportspace.stargate.jms.email.MailMDP">
                    <property name="mailEngine" ref="mailEngine"/>
                    <property name="mailManager" ref="mailManager"/>
                </bean>
            </constructor-arg>
        </bean>
    
        <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"/>    
         </bean>
    
    
    </beans>

    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:

    Code:
    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.
Working...
X