5

スプリング ブートで構成されたスプリング バッチ/スプリング クラウド タスク アプリケーションの XA/分散トランザクションを構成しようとしています。

春のブート自動構成に依存することを期待して、次の依存関係を追加しました。

compile("org.springframework.boot:spring-boot-starter-jta-atomikos")

ただし、次の 2 つのクラスにより、2 つのトランザクション マネージャーが構成されます。

  • org.springframework.cloud.task.configuration.SimpleTaskConfiguration

  • org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration

次のメッセージを参照してください。

2016-07-18 21:46:19.952  INFO 18995 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'transactionManager' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.cloud.task.configuration.SimpleTaskConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in org.springframework.cloud.task.configuration.SimpleTaskConfiguration]

PlatformTransactionManager名前付きが構成されているためtransactionManager、atomicos の自動構成が取得されません。

   AtomikosJtaConfiguration did not match
      - @ConditionalOnClass classes found: org.springframework.transaction.jta.JtaTransactionManager,com.atomikos.icatch.jta.UserTransactionManager (OnClassCondition)
      - @ConditionalOnMissingBean (types: org.springframework.transaction.PlatformTransactionManager; SearchStrategy: all) found the following [transactionManager] (OnBeanCondition)

transactionManager上記の2つのクラスによって引き起こされる豆の不当な強制を防ぐのを手伝ってもらえますか?

4

2 に答える 2

2

私は同じ問題を抱えていました。私の解決策は、BatchConfigurer (@EnableBatchProcessing を保持) を実装し、atomikos Bean を手動で追加することでした。

ジョブ構成:

@Configuration
@EnableBatchProcessing
public class JobConfig implements BatchConfigurer {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private JtaTransactionManager jtaTransactionManager;

    // ... skipping some code 

    @Override
    public JobRepository getJobRepository() throws Exception {
        JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTransactionManager(jtaTransactionManager);
        return factory.getObject();
    }

    @Override
    public PlatformTransactionManager getTransactionManager() throws Exception {
        return jtaTransactionManager;
    }

    @Override
    public JobLauncher getJobLauncher() throws Exception {
        SimpleJobLauncher launcher = new SimpleJobLauncher();
        launcher.setJobRepository(getJobRepository());
        launcher.setTaskExecutor(new SimpleAsyncTaskExecutor());
        return launcher;
    }

    @Override
    public JobExplorer getJobExplorer() throws Exception {
        JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
        jobExplorerFactoryBean.setDataSource(dataSource);
        jobExplorerFactoryBean.afterPropertiesSet();
        return jobExplorerFactoryBean.getObject();
    } 

AtomikosConfig:

@Configuration
public class AtomikosConfig extends AbstractJtaPlatform {

    @Bean(initMethod = "init", destroyMethod = "close")
    @DependsOn("atomikosUserTransactionService")
    public UserTransactionManager atomikosTransactionManager() {
            UserTransactionManager manager = new UserTransactionManager();
            manager.setForceShutdown(false);
            manager.setStartupTransactionService(false);
            return manager;
    }

    @Bean(initMethod = "init", destroyMethod = "shutdownForce")
    public UserTransactionServiceImp atomikosUserTransactionService() {
            Properties properties = new Properties();
            return new UserTransactionServiceImp(properties);
    }

    @Bean
    public UserTransactionImp atomikosUserTransaction() throws SystemException {
            UserTransactionImp transaction = new UserTransactionImp();
            transaction.setTransactionTimeout(300);
            return transaction;
    }

    @Primary
    @Bean
    public JtaTransactionManager jtaTransactionManager() throws Exception {
            JtaTransactionManager manager = new JtaTransactionManager();
            manager.setTransactionManager(atomikosTransactionManager());
            manager.setUserTransaction(atomikosUserTransaction());
            manager.setAllowCustomIsolationLevels(true);
            return manager;
    }

    @Bean
    public ActiveMQXAConnectionFactory xaFactory() {
            ActiveMQXAConnectionFactory factory = new ActiveMQXAConnectionFactory();
            factory.setBrokerURL("tcp://localhost:61616");
            factory.setUserName("admin");
            factory.setPassword("admin");
            //factory.setTrustAllPackages(true);
            factory.setTransactedIndividualAck(true);
            return factory;
    }

    @Bean(initMethod = "init", destroyMethod = "close")
    public AtomikosConnectionFactoryBean connectionFactory() {
            AtomikosConnectionFactoryBean factoryBean = new AtomikosConnectionFactoryBean();
            factoryBean.setUniqueResourceName("amq1");
            factoryBean.setXaConnectionFactory(xaFactory());
            factoryBean.setMaxPoolSize(10);
            return factoryBean;
    }

    @Bean
    public AtomikosJtaPlatform springJtaPlatformAdapter() throws Exception {
            AtomikosJtaPlatform platform = new AtomikosJtaPlatform();
            platform.setJtaTransactionManager(jtaTransactionManager());
            platform.setTransactionManager(atomikosTransactionManager());
            platform.setUserTransaction(atomikosUserTransaction());
            return platform;
    }

    @Override
    protected TransactionManager locateTransactionManager() {
            return atomikosTransactionManager();
    }

    @Override
    protected UserTransaction locateUserTransaction() {
            return atomikosTransactionManager();
    }
于 2016-08-26T09:25:22.213 に答える