java Spring Boot + Spring Batch 无数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39913918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot + spring batch without DataSource
提问by Majky
I'm trying to configure spring batch inside spring boot project and I want to use it without data source. I've found that ResourcelessTransactionManager
is the way to go but I cannot make it work. Problem is I already have 3 another dataSources defined, but I don't want to use any of them in springBatch.
我正在尝试在 spring boot 项目中配置 spring 批处理,我想在没有数据源的情况下使用它。我发现这ResourcelessTransactionManager
是要走的路,但我无法让它发挥作用。问题是我已经定义了另外 3 个数据源,但我不想在 springBatch 中使用它们中的任何一个。
I've checked default implementation DefaultBatchConfigurer
and if it is not able to find dataSource it will do exactly what I want. Problem is I've 3 of them and dont want to use any.
我已经检查了默认实现DefaultBatchConfigurer
,如果它无法找到 dataSource,它将完全按照我的意愿行事。问题是我有 3 个并且不想使用任何一个。
Please dont suggest to use hsql or other in memory DB as I dont want that.
请不要建议在内存数据库中使用 hsql 或其他,因为我不想要那样。
回答by Nandish
I got around this problem by extending the DefaultBatchConfigurer class so that it ignores any DataSource, as a consequence it will configure a map-based JobRepository.
我通过扩展 DefaultBatchConfigurer 类来解决这个问题,以便它忽略任何数据源,因此它将配置基于映射的 JobRepository。
Example:
例子:
@Configuration
@EnableBatchProcessing
public class BatchConfig extends DefaultBatchConfigurer {
@Override
public void setDataSource(DataSource dataSource) {
//This BatchConfigurer ignores any DataSource
}
}
回答by Rzv Razvan
In my case I persist data to Cassandra. If you are using spring-boot-starter-batchit is expected to provide a DataSource which is not yet implemented but you can trick the configuration like in the following steps:
就我而言,我将数据保存到 Cassandra。如果您使用的是 spring-boot-starter-batch,预计会提供一个尚未实现的数据源,但您可以通过以下步骤来欺骗配置:
Step1:
步骤1:
@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);
}
}
Step2:
第2步:
@Configuration
@EnableBatchProcessing
public class SampleBatchJob extends DefaultBatchConfigurer {
//..
@Override
public void setDataSource(DataSource dataSource) {
}
//..
}
回答by Michael Minella
If you have more than one DataSource
in your configuration (regardless of if you want to use them or not) you need to define your own BatchConfigurer
. It's the only way the framework knows what to do in situations like that.
如果您DataSource
的配置中有多个(无论您是否想使用它们),您都需要定义自己的BatchConfigurer
. 这是框架知道在这种情况下该做什么的唯一方式。
You can read more about the BatchConfigurer
in the documentation here: http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/configuration/annotation/BatchConfigurer.html
您可以BatchConfigurer
在此处阅读有关文档的更多信息:http: //docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/configuration/annotation/BatchConfigurer.html
回答by abaghel
You can try excluding the DataSourceAutoConfiguration in @SpringBootApplication. See the sample code below.
您可以尝试在@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)));
}
}
And sample test 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");
}
}
回答by Richard Va?ek
We had the similar problem, we were using spring boot JDBC and we did not want to store spring batch tables in the DB, but we still wanted to use spring's transaction management for our DataSource.
我们遇到了类似的问题,我们使用的是 spring boot JDBC,我们不想在数据库中存储 spring 批处理表,但我们仍然想对我们的数据源使用 spring 的事务管理。
We ended up implementing own BatchConfigurer.
我们最终实现了自己的 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
}
and setting up initializer to false
并将初始值设定项设置为 false
spring.batch.initializer.enabled=false