Spring 批处理步骤未执行

Spring Batch step not executing

我正在处理一个 spring 批处理项目,我在其中读取学生列表、处理它并编写它。

目前,我一直保持简单,只处理 returns 学生并编写打印出来。

我期待每次步骤 运行s 我都会看到输出,但我只在第一次步骤 运行s 时看到它一次。下面是输出

2020-04-03 01:33:16.153  INFO 14710 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [xxxx]
[Student{id=1, name='ABC'}]
as
[Student{id=2, name='DEF'}]
as
[Student{id=3, name='GHI'}]
as
2020-04-03 01:33:16.187  INFO 14710 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [xxxx] executed in 33ms
2020-04-03 01:33:16.190  INFO 14710 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=readStudents]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 52ms
job triggered
2020-04-03 01:33:17.011  INFO 14710 --- [   scheduling-1] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=readStudents]] launched with the following parameters: [{time=1585857797003}]
2020-04-03 01:33:17.017  INFO 14710 --- [   scheduling-1] o.s.batch.core.job.SimpleStepHandler     : Executing step: [xxxx]
2020-04-03 01:33:17.022  INFO 14710 --- [   scheduling-1] o.s.batch.core.step.AbstractStep         : Step: [xxxx] executed in 4ms
2020-04-03 01:33:17.024  INFO 14710 --- [   scheduling-1] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=readStudents]] completed with the following parameters: [{time=1585857797003}] and the following status: [COMPLETED] in 11ms

我还注意到作业中第一次没有参数,之后有参数。而我在 运行 作业时提供作业参数。

配置文件

@EnableBatchProcessing
public class Config {

  private JobRunner jobRunner;

  public Config(JobRunner jobRunner){
    this.jobRunner = jobRunner;
  }

  @Scheduled(cron = "* * * * * *")
  public void scheduleJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
    System.out.println("job triggered");
    jobRunner.runJob();
  }
}
@Configuration
public class JobConfig {
  @Bean
  public Job job(JobBuilderFactory jobBuilderFactory,
                 StepBuilderFactory stepBuilderFactory,
                 ItemReader<Student> reader,
                 ItemProcessor<Student, Student> processor,
                 ItemWriter<Student> writer) {

    Step step = stepBuilderFactory.get("xxxx")
        .<Student, Student>chunk(1)
        .reader(reader)
        .processor(processor)
        .writer(writer)
        .build();

    return jobBuilderFactory
        .get("readStudents")
        .start(step)
        .build();
  }

  @Bean
  public ItemReader<Student> reader() {
    return new InMemoryStudentReader();
  }
}

作业运行ner 文件

public class JobRunner {

  private Job job;
  private JobLauncher simpleJobLauncher;

  @Autowired
  public JobRunner(Job job, JobLauncher jobLauncher) {
    this.simpleJobLauncher = jobLauncher;
    this.job = job;
  }


  public void runJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
    JobParameters jobParameters =
        new JobParametersBuilder()
            .addLong("time",System.currentTimeMillis()).toJobParameters();
    simpleJobLauncher.run(job, jobParameters);

  }
}

记忆中同学reader

public class InMemoryStudentReader implements ItemReader<Student> {

  private int nextStudentIndex;
  private List<Student> studentData;

  public InMemoryStudentReader() {
    initialize();
  }

  private void initialize() {
    Student s1 = new Student(1, "ABC");
    Student s2 = new Student(2, "DEF");
    Student s3 = new Student(3, "GHI");

    studentData = Collections.unmodifiableList(Arrays.asList(s1, s2,s3));
    nextStudentIndex = 0;
  }

  @Override
  public Student read() throws Exception {
    Student nextStudent = null;

    if (nextStudentIndex < studentData.size()) {
      nextStudent = studentData.get(nextStudentIndex);
      nextStudentIndex++;
    }

    return nextStudent;
  }
}

因为您在 InMemoryStudentReader 构造函数中调用 initialize()。 Spring 只初始化一次 InMemoryStudentReader 并将其连接到您的作业。在第一个 运行 之后,nextStudentIndex 不会重置为 0。所以下一次你的作业 运行 时,你的 reader 无法再读取了。

如果你想让它工作,你应该在你开始你的工作时将 nextStudentIndex 重置为 0。