问题描述
我正在尝试为配置有Spring Boot的Spring Batch/Spring Cloud Task应用程序配置XA/分布式事务.
I am trying to configure XA/distributed transactions for a spring batch / spring cloud task application configured with spring boot.
我添加了以下依赖项,希望依赖于Spring Boot自动配置:
I have added the following dependency hoping to rely on spring boot auto configuration:
compile("org.springframework.boot:spring-boot-starter-jta-atomikos")
但是,以下两个类导致配置了两个事务管理器:
However the following two classes cause two transaction managers to be configured:
-
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]
,然后因为配置了名为 transactionManager
的PlatformTransactionManager
,所以没有获取我的atomikos自动配置:
and then because a PlatformTransactionManager
named transactionManager
is configured, my atomikos auto-configuration is not picked up:
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
豆过分强迫吗?
Can someone please help me prevent this unduly forcing of the transactionManager
beans caused by the two classes above?
推荐答案
我遇到了同样的问题,我的解决方案是实现BatchConfigurer(保留@EnableBatchProcessing)并手动添加atomikos bean.
I had the same issue and my solution was to implement BatchConfigurer (keeping @EnableBatchProcessing) and to add atomikos beans manually.
JobConfig:
JobConfig:
@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:
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();
}
这篇关于Spring Cloud Task的SimpleTaskConfiguration和Spring Batch的SimpleBatchConfiguration阻止XA事务的Spring Boot自动配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!