我使用Spring Batch和Spring Boot,这是我的主要课程。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这是我的配置类
@Configuration
public class AppConfig {
@Bean
public MyObject getObject() {
return new MyObject();
}
}
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
private static final String OVERRIDDEN_BY_EXPRESSION = null;
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
public Job job() {
return jobs.get(Constants.FULL_JOB_NAME)
.start(stepProcessDocument())
.build();
}
@Bean
protected Step stepProcessDocument() {
return steps.get(Constants.STEP_PROCESS_DOCUMENT_NAME)
.<Document,Document>chunk(10)
.reader(buildItemReader(OVERRIDDEN_BY_EXPRESSION))
.processor(buildItemProcessor())
.writer(buildItemWriter(OVERRIDDEN_BY_EXPRESSION))
.build();
}
@Bean
@StepScope
protected ItemReader<Document> buildItemReader(@Value("#{jobParameters[" + Constants.PARAM_JOB_PARAMETER + "]}") String param) {
ItemStreamReader<Document> reader = new CustomItemReader(param);
reader.open(new ExecutionContext());
return reader;
}
@Bean
protected ItemProcessor<Document, Document> buildItemProcessor() {
return new CustomItemProcessor();
}
@Bean
@StepScope
protected ItemWriter<Document> buildItemWriter(@Value("#{jobParameters[" + Constants.PARAM_JOB_PARAMETER + "]}") String param) {
ItemStreamWriter<Document> writer = new CustomItemWriter(param);
writer.open(new ExecutionContext());
return writer;
}
@Bean
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) {
JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
return jobRegistryBeanPostProcessor;
}
}
这是我在我的应用程序中使用的自定义文件阅读器。
public class CustomItemReader implements ItemStreamReader<Document> {
@Autowired
private MyObject myObject;
private int count = 0;
private String param;
public CustomItemReader(String param) {
this.param = param;
}
@Override
public void open(ExecutionContext executionContext)
throws ItemStreamException {
myObject.open(); //myObject is null
}
@Override
public void update(ExecutionContext executionContext)
throws ItemStreamException {
// TODO Auto-generated method stub
}
@Override
public void close() throws ItemStreamException {
myObject.close();
}
@Override
public Document read() throws Exception, UnexpectedInputException,
ParseException, NonTransientResourceException {
myObject.doStuff();
count++;
if(count == 5) {
return null;
}
return new Document();
}
我在myObject上遇到了一个Java空指针异常。 为什么我无法在ItemStreamReader打开方法中自动装配java对象?
答案 0 :(得分:1)
我找到了答案。在我的itemReader的定义方法中:
@Bean
@StepScope
protected ItemReader<Document> buildItemReader(@Value("#{jobParameters[" + Constants.PARAM_JOB_PARAMETER + "]}") String param) {
ItemStreamReader<Document> reader = new CustomItemReader(param);
reader.open(new ExecutionContext());
return reader;
}
我执行方法:
reader.open(new ExecutionContext());
所以每当我重新启动使用此itemReader的失败作业时,我会破坏失败作业步骤的执行上下文。我不能从我离开的地方恢复。
要解决此问题,我必须删除此行代码并返回ItemStreamReader。因此框架可以访问open方法。
@Bean
@StepScope
protected ItemStreamReader<Document> buildItemReader(@Value("#{jobParameters[" + Constants.PARAM_JOB_PARAMETER + "]}") String param) {
ItemStreamReader<Document> reader = new CustomItemReader(param);
return reader;
}
此外,这个解决方案也解决了我原来的问题。但不幸的是,我不知道为什么,因为我是Spring Framework的初学者。
我希望有人能帮助我理解。
答案 1 :(得分:0)
您需要加载Beans作为上下文的一部分,因为您使用的是Spring Boot,您可以使用以下内容:
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) throws Exception {
// System.exit is common for Batch applications since the exit code can be used to
// drive a workflow
System.exit(SpringApplication.exit(SpringApplication.run(
Application.class, args)));
}
}
@ComponentScan
将注册所有Spring组件,例如@Component, @Repository, @Service, @Controller
,包括@Configuration
类。如果您需要查看完整的示例,请查看此项目:http://www.codingpedia.org/ama/spring-batch-tutorial-with-spring-boot-and-java-configuration/
我希望这可以有所帮助。