我的要求:创建一个调度程序来定期调用批处理,并且在批处理内部,我需要两个步骤。StepA 将开始运行,根据结果,StepB 将调用。如果您查看控制台日志,您将看到 stepA 被多次调用,比如说,完全相同的时刻。根据@Scheduled(fixedRate=60000) 每分钟应该只有一个电话。我猜这个问题与调度程序没有关系,因为我删除了调度程序并且我遇到了类似的问题。其他相关问题是从未调用过 StepB。我尝试创建 customDecider ("...next(customDecider()") 并创建一个私有方法来返回 JobExecutionDecider。似乎两者都没有生效。我最好的猜测是池线程导致StepA 中的一些“无限循环”,因为我没有
批处理配置.java
/*@Bean
public JobExecutionDecider customDecider() throws Exception{
return new CustomDecider();
}*/
private JobExecutionDecider decider(boolean decision){
return(jobExecution, stepExecution) -> new FlowExecutionStatus(decision ? "OK" : "FAILED");
}
@Bean
public Job job(Step stepA, Step stepB) {
return jobBuilderFactory.get("job1")
//.flow(stepA).on("FAILED").to(stepB).end().build();
.start(stepA)
.next(decider(true))
.on("FAILED")
.stop()
.from(stepA)
.next(stepB)
.build()
.build();
}
@Bean
public Step stepA(ItemReader<String> readerA, ItemWriter<String> writerA) {
return stepBuilderFactory.get("stepA").<String, String> chunk(1)
.reader(readerA)
.writer(writerA)
.allowStartIfComplete(true)
.build();
}
@Bean
@StepScope
public ItemReader<String> readerA() {
return new CustomItemReaderA();
}
@Bean
public ItemWriter<String> writerA() {
return new CustomItemWriterA();
}
@Bean
public Step stepB(ItemReader<String> readerB, ItemWriter<String> writerB) {
return stepBuilderFactory.get("stepB").<String, String> chunk(1)
.reader(readerB)
.writer(writerB)
.allowStartIfComplete(true)
.build();
}
@Bean
@StepScope
public ItemReader<String> readerB() {
return new CustomItemReaderB();
}
@Bean
public ItemWriter<String> writerB() {
return new CustomItemWriterB();
}
CustomDecider.java
public class CustomDecider implements JobExecutionDecider {
@Override
public FlowExecutionStatus decide(JobExecution jobExecution,
StepExecution stepExecution) {
return new FlowExecutionStatus("OK");
}
}
CustomItemReaderA
public class CustomItemReaderA implements ItemReader<String>{
private static final Logger log = LoggerFactory.getLogger(CustomItemReaderA.class);
@Override
public String read() throws Exception, UnexpectedInputException,
ParseException, NonTransientResourceException {
log.info("rA");
return "A";
}
}
CustomItemWriterA
public class CustomItemWriterA implements ItemWriter<String> {
private static final Logger log = LoggerFactory.getLogger(CustomItemWriterA.class);
@Override
public void write(List<? extends String> items) throws Exception {
log.info("wA");
}
}
CustomItemReaderB 和 CustomItemReaderB 与 Custom...A 相同,只是在 log.info 期间从 A 变为 B 字母并返回
控制台日志
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.488 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.504 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemReaderA : rA
2016-01-18 21:40:37.519 INFO 7340 --- [pool-2-thread-1] com.example.CustomItemWriterA : wA
and so on ...
调度器
@Component
public class QueueScheduler {
private static final Logger log = LoggerFactory
.getLogger(QueueScheduler.class);
private Job job;
private JobLauncher jobLauncher;
@Autowired
public QueueScheduler(JobLauncher jobLauncher, @Qualifier("job") Job job){
this.job = job;
this.jobLauncher = jobLauncher;
}
@Scheduled(fixedRate=60000)
public void runJob(){
try{
jobLauncher.run(job, new JobParameters());
}catch(Exception ex){
log.info(ex.getMessage());
}
}
}