繁体   English   中英

Spring 批处理步骤未执行

[英]Spring Batch step not executing

我正在研究一个 spring 批处理项目,在该项目中我正在阅读学生名单,对其进行处理和编写。

现在我保持简单,处理只是返回学生,写只是打印它。

我期待每次步骤运行时我都会看到 output 但当步骤第一次运行时我只看到一次。 下面是output

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();
  }
}

作业运行器文件

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);

  }
}

memory学生读卡器中

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。因此,下次您的作业运行时,您的阅读器将无法再阅读。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM