我的主要工作只是读操作而另一个写了一些写但是 MyISAM engine
忽略了事务,所以我不需要必要的事务支持 . 如何配置 Spring Batch
以拥有自己的 JobRepository
数据源,与保存业务数据的数据源分开?最初的一个数据源配置如下所示:
@Configuration
public class StandaloneInfrastructureConfiguration {
@Autowired
Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "org.podcastpedia.batch.*" });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalJpaProperties());
return em;
}
Properties additionalJpaProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "none");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.setProperty("hibernate.show_sql", "true");
return properties;
}
@Bean
public DataSource dataSource(){
return DataSourceBuilder.create()
.url(env.getProperty("db.url"))
.driverClassName(env.getProperty("db.driver"))
.username(env.getProperty("db.username"))
.password(env.getProperty("db.password"))
.build();
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
然后将它导入 Job
的配置类中,其中 @EnableBatchProcessing
注释自动使用它 . 我最初的想法是尝试设置配置类扩展 DefaultBatchConfigurer
,但后来我得到了
BeanCurrentlyInCreationException(org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为jobBuilders的bean时出错:当前正在创建请求的bean:是否存在无法解析的循环引用?):
@Configuration
@EnableBatchProcessing
@Import({StandaloneInfrastructureConfiguration.class, NotifySubscribersServicesConfiguration.class})
public class NotifySubscribersJobConfiguration extends DefaultBatchConfigurer {
@Autowired
private JobBuilderFactory jobBuilders;
@Autowired
private StepBuilderFactory stepBuilders;
@Autowired
private DataSource dataSource;
@Autowired
Environment env;
@Override
@Autowired
public void setDataSource(javax.sql.DataSource dataSource) {
super.setDataSource(batchDataSource());
}
private DataSource batchDataSource(){
return DataSourceBuilder.create()
.url(env.getProperty("batchdb.url"))
.driverClassName(env.getProperty("batchdb.driver"))
.username(env.getProperty("batchdb.username"))
.password(env.getProperty("batchdb.password"))
.build();
}
@Bean
public ItemReader<User> notifySubscribersReader(){
JdbcCursorItemReader<User> reader = new JdbcCursorItemReader<User>();
String sql = "select * from users where is_email_subscriber is not null";
reader.setSql(sql);
reader.setDataSource(dataSource);
reader.setRowMapper(rowMapper());
return reader;
}
........
}
任何想法都受到欢迎 . 该项目可在GitHub上获取 - https://github.com/podcastpedia/podcastpedia-batch
谢谢一堆 .
5 回答
好的,这很奇怪,但它确实有效 . 将数据源移动到它自己的配置类工作正常,并且可以自动装配 .
该示例是Spring Batch Service Example的多数据源版本:
DataSourceConfiguration :
BatchConfiguration :
我将数据源放在一个单独的配置类中 . 在批处理配置中,我们扩展DefaultBatchConfigurer并覆盖setDataSource方法,传入特定数据库以与Spring Batch一起使用@Qualifier . 我无法使用构造函数版本来使用它,但setter方法对我有用 .
我的阅读器,处理器和编写器都在他们自己的自包含类中,以及步骤 .
这是使用Spring Boot 1.1.8和Spring Batch 3.0.1 . Note: 对于使用Spring Boot 1.1.5的项目,我们有一个不同的设置,它在新版本上的工作方式不同 .
你有没有试过这样的东西?
然后使用@Primary标记其他数据源,并在批处理配置中使用@Qualifier来指定您要对batchDataSource bean进行auotwire .
按https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources:
在应用程序属性中,您可以使用常规数据源属性:
假设您有2个数据源,一个用于弹出批处理元数据,例如作业详细信息[比如说CONFIGDB],另一个用于您的业务数据[比如说AppDB]:
将CONFIGDB注入jobRepository,如下所示:
现在你可以将AppDB dartasource注入到DAO的OR Writer中,如果有的话......
要么
你可以定义一个资源,并在类所需的类中使用jndi查找注入此AppDB,如:
}