作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为使用 spring 引导配置的 spring 批处理/spring 云任务应用程序配置 XA/分布式事务。
我添加了以下依赖项,希望依赖于 spring boot 自动配置:
compile("org.springframework.boot:spring-boot-starter-jta-atomikos")
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
已配置,我的 atomikos 自动配置没有被拾取:
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
上面两个类引起的beans?
最佳答案
我遇到了同样的问题,我的解决方案是实现 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();
}
@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-boot - Spring 云任务的 SimpleTaskConfiguration 和 Spring Batch 的 SimpleBatchConfiguration 防止 XA 事务的 Spring Boot 自动配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38445529/
我正在尝试为使用 spring 引导配置的 spring 批处理/spring 云任务应用程序配置 XA/分布式事务。 我添加了以下依赖项,希望依赖于 spring boot 自动配置: compil
我是一名优秀的程序员,十分优秀!