Spring 启动 + spring 没有数据源的批处理
Spring boot + spring batch without DataSource
我正在尝试在 spring 启动项目中配置 spring 批处理,我想在没有数据源的情况下使用它。我发现 ResourcelessTransactionManager
是要走的路,但我无法让它工作。问题是我已经定义了另外 3 个数据源,但我不想在 springBatch.
中使用它们中的任何一个
我已经检查了默认实现 DefaultBatchConfigurer
,如果它无法找到数据源,它将完全按照我的要求执行。问题是我有 3 个,不想使用任何一个。
请不要建议在内存数据库中使用 hsql 或其他,因为我不希望那样。
你可以尝试去掉@SpringBootApplication中的DataSourceAutoConfiguration。请参阅下面的示例代码。
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableBatchProcessing
public class SampleBatchApplication {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
protected Tasklet tasklet() {
return new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext context) {
return RepeatStatus.FINISHED;
}
};
}
@Bean
public Job job() throws Exception {
return this.jobs.get("job").start(step1()).build();
}
@Bean
protected Step step1() throws Exception {
return this.steps.get("step1").tasklet(tasklet()).build();
}
public static void main(String[] args) throws Exception {
System.exit(SpringApplication.exit(SpringApplication.run(SampleBatchApplication.class, args)));
}
}
和样本测试class
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.rule.OutputCapture;
import static org.assertj.core.api.Assertions.assertThat;
public class SampleBatchApplicationTests {
@Rule
public OutputCapture outputCapture = new OutputCapture();
@Test
public void testDefaultSettings() throws Exception {
assertThat(SpringApplication.exit(SpringApplication.run(SampleBatchApplication.class))).isEqualTo(0);
String output = this.outputCapture.toString();
assertThat(output).contains("completed with the following parameters");
}
}
如果您的配置中有多个 DataSource
(无论您是否要使用它们),您需要定义自己的 BatchConfigurer
。这是框架知道在这种情况下该做什么的唯一方法。
您可以在此处的文档中阅读有关 BatchConfigurer
的更多信息:http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/configuration/annotation/BatchConfigurer.html
我通过扩展 DefaultBatchConfigurer class 解决了这个问题,因此它会忽略任何数据源,因此它将配置一个基于映射的 JobRepository。
示例:
@Configuration
@EnableBatchProcessing
public class BatchConfig extends DefaultBatchConfigurer {
@Override
public void setDataSource(DataSource dataSource) {
//This BatchConfigurer ignores any DataSource
}
}
我们有类似的问题,我们正在使用 spring 启动 JDBC 并且我们不想在数据库中存储 spring 批处理表,但我们仍然想使用 spring 对我们数据源的事务管理。
我们最终实现了自己的 BatchConfigurer。
@Component
public class TablelessBatchConfigurer implements BatchConfigurer {
private final PlatformTransactionManager transactionManager;
private final JobRepository jobRepository;
private final JobLauncher jobLauncher;
private final JobExplorer jobExplorer;
private final DataSource dataSource;
@Autowired
public TablelessBatchConfigurer(DataSource dataSource) {
this.dataSource = dataSource;
this.transactionManager = new DataSourceTransactionManager(this.dataSource);
try {
final MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean(this.transactionManager);
jobRepositoryFactory.afterPropertiesSet();
this.jobRepository = jobRepositoryFactory.getObject();
final MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory);
jobExplorerFactory.afterPropertiesSet();
this.jobExplorer = jobExplorerFactory.getObject();
final SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setJobRepository(this.jobRepository);
simpleJobLauncher.afterPropertiesSet();
this.jobLauncher = simpleJobLauncher;
} catch (Exception e) {
throw new BatchConfigurationException(e);
}
}
// ... override getters
}
并将初始化程序设置为 false
spring.batch.initializer.enabled=false
在我的例子中,我将数据保存到 Cassandra。如果您使用的是 spring-boot-starter-batch,则预计会提供一个尚未实现的数据源,但您可以按照以下步骤欺骗配置:
第一步:
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class SampleSpringBatchApplication{
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "true");
SpringApplication.run(SampleSpringBatchApplication.class, args);
}
}
第 2 步:
@Configuration
@EnableBatchProcessing
public class SampleBatchJob extends DefaultBatchConfigurer {
//..
@Override
public void setDataSource(DataSource dataSource) {
}
//..
}
我正在尝试在 spring 启动项目中配置 spring 批处理,我想在没有数据源的情况下使用它。我发现 ResourcelessTransactionManager
是要走的路,但我无法让它工作。问题是我已经定义了另外 3 个数据源,但我不想在 springBatch.
我已经检查了默认实现 DefaultBatchConfigurer
,如果它无法找到数据源,它将完全按照我的要求执行。问题是我有 3 个,不想使用任何一个。
请不要建议在内存数据库中使用 hsql 或其他,因为我不希望那样。
你可以尝试去掉@SpringBootApplication中的DataSourceAutoConfiguration。请参阅下面的示例代码。
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableBatchProcessing
public class SampleBatchApplication {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
protected Tasklet tasklet() {
return new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext context) {
return RepeatStatus.FINISHED;
}
};
}
@Bean
public Job job() throws Exception {
return this.jobs.get("job").start(step1()).build();
}
@Bean
protected Step step1() throws Exception {
return this.steps.get("step1").tasklet(tasklet()).build();
}
public static void main(String[] args) throws Exception {
System.exit(SpringApplication.exit(SpringApplication.run(SampleBatchApplication.class, args)));
}
}
和样本测试class
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.rule.OutputCapture;
import static org.assertj.core.api.Assertions.assertThat;
public class SampleBatchApplicationTests {
@Rule
public OutputCapture outputCapture = new OutputCapture();
@Test
public void testDefaultSettings() throws Exception {
assertThat(SpringApplication.exit(SpringApplication.run(SampleBatchApplication.class))).isEqualTo(0);
String output = this.outputCapture.toString();
assertThat(output).contains("completed with the following parameters");
}
}
如果您的配置中有多个 DataSource
(无论您是否要使用它们),您需要定义自己的 BatchConfigurer
。这是框架知道在这种情况下该做什么的唯一方法。
您可以在此处的文档中阅读有关 BatchConfigurer
的更多信息:http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/configuration/annotation/BatchConfigurer.html
我通过扩展 DefaultBatchConfigurer class 解决了这个问题,因此它会忽略任何数据源,因此它将配置一个基于映射的 JobRepository。
示例:
@Configuration
@EnableBatchProcessing
public class BatchConfig extends DefaultBatchConfigurer {
@Override
public void setDataSource(DataSource dataSource) {
//This BatchConfigurer ignores any DataSource
}
}
我们有类似的问题,我们正在使用 spring 启动 JDBC 并且我们不想在数据库中存储 spring 批处理表,但我们仍然想使用 spring 对我们数据源的事务管理。
我们最终实现了自己的 BatchConfigurer。
@Component
public class TablelessBatchConfigurer implements BatchConfigurer {
private final PlatformTransactionManager transactionManager;
private final JobRepository jobRepository;
private final JobLauncher jobLauncher;
private final JobExplorer jobExplorer;
private final DataSource dataSource;
@Autowired
public TablelessBatchConfigurer(DataSource dataSource) {
this.dataSource = dataSource;
this.transactionManager = new DataSourceTransactionManager(this.dataSource);
try {
final MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean(this.transactionManager);
jobRepositoryFactory.afterPropertiesSet();
this.jobRepository = jobRepositoryFactory.getObject();
final MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory);
jobExplorerFactory.afterPropertiesSet();
this.jobExplorer = jobExplorerFactory.getObject();
final SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setJobRepository(this.jobRepository);
simpleJobLauncher.afterPropertiesSet();
this.jobLauncher = simpleJobLauncher;
} catch (Exception e) {
throw new BatchConfigurationException(e);
}
}
// ... override getters
}
并将初始化程序设置为 false
spring.batch.initializer.enabled=false
在我的例子中,我将数据保存到 Cassandra。如果您使用的是 spring-boot-starter-batch,则预计会提供一个尚未实现的数据源,但您可以按照以下步骤欺骗配置:
第一步:
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class SampleSpringBatchApplication{
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "true");
SpringApplication.run(SampleSpringBatchApplication.class, args);
}
}
第 2 步:
@Configuration
@EnableBatchProcessing
public class SampleBatchJob extends DefaultBatchConfigurer {
//..
@Override
public void setDataSource(DataSource dataSource) {
}
//..
}