paso Spring Batch no ejecutar

Legendary_Hunter:

Estoy trabajando en un proyecto de lotes de primavera en el que estoy leyendo una lista de los estudiantes, procesarla y escribirlo.

Por ahora lo he guardado sencilla y procesar sólo devuelve el estudiante y escribir simplemente lo imprime.

Yo estaba esperando cada vez corre el paso voy a ver la salida pero no veo una sola vez cuando se ejecuta el paso por primera vez. A continuación se muestra la salida

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

También noto que por primera vez no hay parámetros de trabajo y después de que no son parámetros. Mientras que yo soy el suministro de los parámetros del trabajo cada vez que corro trabajo.

Archivo de configuración

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

archivo corredor de empleo

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

  }
}

En la memoria lector de estudiante

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;
  }
}
El problema:

Porque se está llamando initialize()en el InMemoryStudentReaderconstructor. Primavera solamente inicializar InMemoryStudentReader una vez y el alambre a su trabajo. Después de la primera carrera, nextStudentIndexno se restablece a 0. Así que la próxima vez que sus carreras de trabajo, el lector no puede leer más.

Si quieres que funcione, debe restablecer el nextStudentIndexa 0 cada vez que inicie su trabajo.

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=403137&siteId=1
Recomendado
Clasificación