我的要求:创建一个调度程序来定期调用批处理,在批处理内部,我需要两个步骤。 StepA将开始运行并相应地结果,StepB将调用。 如果您查看控制台日志,您会看到stepA已被多次调用,比方说,完全相同的时刻。根据@Scheduled(fixedRate = 60000),每分钟应该只有一个电话。 Iguess认为这个问题与Scheduller没有任何关系,因为我删除了schedulling并且我遇到了类似的问题。其他相关问题是从不调用StepB。我尝试创建customDecider(“... next(customDecider()”)并创建一个私有方法来返回JobExecutionDecider。似乎两者都没有生效。 我最好的猜测是,池线程在StepA中引起了一些“无限循环”,因为我没有正确设置,但我没有找到它可能是什么。
BatchConfig.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 ...相同只是在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());
}
}
}
答案 0 :(得分:2)
问题在于您的CustomItemReaderA
。您需要告诉读者何时停止“读取”数据。如果您按照下面的说明修改阅读器,则执行将按预期进行。
public class CustomItemReaderA implements ItemReader<String> {
private static final Logger log = LoggerFactory
.getLogger(CustomItemReaderA.class);
private int readCount = 0;
@Override
public String read() throws Exception, UnexpectedInputException,
ParseException, NonTransientResourceException {
log.info("Inside CustomItemReaderA");
if (readCount == 1) {
return null;
}
readCount++;
return "Read CustomItemReaderA";
}
}