使用JTA和Atomikos配置Spring JMS

时间:2015-11-04 15:09:25

标签: spring-boot jta weblogic12c spring-jms atomikos

我想实现以下目标:

i)我们有一个weblogic队列,外部应用程序将写入队列

ii)我必须编写一个独立的应用程序,它可以在事务中读写这个队列。即来自队列的消息必须持久保存到DB中。因此,如果在任何阶段出现任何错误,事务应该回滚,并且消息应该再次在队列中可用。

为实现这一点,我做了一些自学,并遇到了以下网址: http://www.javaworld.com/article/2077963/open-source-tools/distributed-transactions-in-spring--with-and-without-xa.html?page=3 我已经明白我必须使用JTA和XA资源。我尝试使用Atomikos作为JTA事务管理器,遵循以下URL: http://www.atomikos.com/Documentation/SpringIntegration

在尝试了一整天之后,我得到了以下不完整的applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.provider.url">t3://somehost.corp.com:8011</prop>
                <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
            </props>
        </property>
    </bean>
<!--
This is an interface not a concreate class. Not sure what needs to go here for weblogic queue connection factory.
     <bean id="xaFactory" class="javax.jms.XAConnectionFactory">
        <property name="brokerURL" value="t3://somehost.corp.com:8011" />
    </bean> -->

    <bean id="ConnectionFactory" class="com.atomikos.jms.AtomikosConnectionFactoryBean"
        init-method="init" destroy-method="close">
        <property name="uniqueResourceName" value="jms/AuditCF" />
         <property name="xaConnectionFactory" ref="xaFactory" />
    </bean>

    <bean id="JtaTransactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager" ref="AtomikosTransactionManager" />
        <property name="userTransaction" ref="AtomikosUserTransaction" />
    </bean>

    <bean id="AtomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
        init-method="init" destroy-method="close">
        <property name="forceShutdown" value="false" />
    </bean>

    <bean id="AtomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout" value="300" />
    </bean>
    <bean id="myDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>jms/AuditQ</value>
        </property>
    </bean>
    <bean id="service" class="com.samples.AccountService">
        <property name="jmsTemplate">
            <ref bean="jmsTemplate" />
        </property>
    </bean>
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory">
            <ref bean="ConnectionFactory" />
        </property>
        <property name="defaultDestination">
            <ref bean="myDestination" />
        </property>
        <property name="destinationResolver" ref="jndiResolver" />
    </bean>

    <!-- a class that implements javax.jms.MessageListener -->
    <bean id="MessageListener" class="com.samples.Messages" />

    <bean id="MessageListenerContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="transactionManager" ref="JtaTransactionManager" />
        <property name="connectionFactory" ref="ConnectionFactory" />
        <property name="messageListener" ref="MessageListener" />
        <property name="destinationName" value="jms/AuditQ" />
        <property name="concurrentConsumers" value="1" />
        <property name="receiveTimeout" value="3000" />
        <property name="sessionTransacted" value="true" />
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>



    <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate">
            <ref bean="jndiTemplate" />
        </property>
        <property name="jndiName">
            <value>weblogic.jms.XAConnectionFactory</value>
        </property>
    </bean>
    <bean id="jndiResolver"
        class="org.springframework.jms.support.destination.JndiDestinationResolver">
        <property name="jndiTemplate">
            <ref bean="jndiTemplate" />
        </property>
        <property name="cache">
            <value>true</value>
        </property>
    </bean>
    <!-- <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="receiveTimeout">
            <value>0</value>
        </property>
        <property name="destinationResolver" ref="jndiResolver" />
    </bean> -->

    <bean id="datasource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
        init-method="init" destroy-method="close">
        <!-- set an arbitrary but unique name for the datasource -->
        <property name="uniqueResourceName">
            <value>XADBMS</value>
        </property>
        <!-- set the underlying driver class to use, in this example case we use
            Oracle -->
        <property name="xaDataSourceClassName">
            <value>oracle.jdbc.xa.client.OracleXADataSource</value>
        </property>
        <property name="xaProperties">
            <props>
                <prop key="user"><user></prop>
                <prop key="password"><pwd></prop>
                <prop key="URL">jdbc:oracle:thin:@host:3028/DB
                </prop>
            </props>
        </property>
        <!-- how many connections in the pool? -->
        <property name="poolSize" value="3" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="packagesToScan" value="com.samples">
        </property>

        <property name="dataSource">
            <ref bean="datasource" />
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.connection.isolation">3</prop>
                <prop key="hibernate.current_session_context_class">jta</prop>
                <prop key="hibernate.transaction.factory_class">
                    org.hibernate.transaction.JTATransactionFactory
                </prop>
                <prop key="hibernate.transaction.manager_lookup_class">
                    com.atomikos.icatch.jta.hibernate4.TransactionManagerLookup
                </prop>
            </props>
        </property>
    </bean>

</beans>

如Spring-Atomikos集成示例所示,XAConnectionFactory已被提及为:

<bean id="xaFactory"  
              class="org.apache.activemq.ActiveMQXAConnectionFactory"> 
      <property name="brokerURL" value="tcp://localhost:61616" /> 
   </bean>

我无法找到我需要为weblogic队列执行的相关配置。我曾尝试使用“weblogic.jms.XAConnectionFactory”作为类,但我得到了classnotfoundexception。

我没有使用JTA或XA的经验。请指导我。

0 个答案:

没有答案