我将弹簧批量与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 Null指针异常。为什么我无法在tocketTreamreader打开方法中自动自动java对象?
我找到了答案。在我的项目阅读器的定义方法中:
@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());
因此,每次我重新启动使用此项目阅读器的失败作业时,我都会破坏失败的作业步骤的执行上下文。而且我无法恢复离开的地方。
要解决此问题,我必须删除此代码行并返回项目流程程序。因此,框架可以访问开放方法。
@Bean
@StepScope
protected ItemStreamReader<Document> buildItemReader(@Value("#{jobParameters[" + Constants.PARAM_JOB_PARAMETER + "]}") String param) {
ItemStreamReader<Document> reader = new CustomItemReader(param);
return reader;
}
此外,该决议也可以解决我的原始问题。但是不幸的是,我不知道为什么是因为我是春季框架的初学者。
我希望有人会帮助我理解。
您需要加载豆作为上下文的一部分,因为您使用的是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
将注册所有弹簧组件(例如@Component, @Repository, @Service, @Controller
),包括@Configuration
类。如果您需要看到一个完整的示例,请查看此项目:http://www.codingpedia.org/ama/spring-batch-tutorial-with-spring-boot-boot-boot-and-java-configuration/
我希望这可以有用。