Reputation: 172
I'm new in Spring Batch and I'm trying to execute two steps of a job. Both must initialize something before execute the read, process and write methods. But I'm not getting how can I do it. Every time I launch the job, the two steps initialize at same time. I want they initialize in the sequence of the job.
To put it simply, I did something like that:
public Job job() {
return jobBuilderFactory.get("job")
.incrementer(new RunIdIncrementer())
.start(step1())
.next(step2())
.build();
}
public Step step1() {
return stepBuilderFactory.get("step1")
.<Model1, Model1>chunk(2)
.reader(reader1())
.processor(processor1())
.writer(writer1())
.build();
}
public Step step2() {
return stepBuilderFactory.get("step2")
.<Model2, Model2>chunk(2)
.reader(reader2())
.processor(processor2())
.writer(writer2())
.build();
}
@StepScope
public Reader1 reader1() {
return new Reader1();
}
@StepScope
public Processor1 processor1() {
return new Processor1();
}
@StepScope
public Writer1 writer1() {
return new Writer1();
}
@StepScope
public Reader2 reader2() {
return new Reader2();
}
@StepScope
public Processor2 processor2() {
return new Processor2();
}
@StepScope
public Writer2 writer2() {
return new Writer2();
}
That's my Reader2 class that I want to initialize after the first step. The Reader1 is the same thing. Both "test" are printed and then steps begin to run.
public class Reader2 implements ItemReader<Model2>{
public Reader2() {
initialize();
}
public void initialize() {
System.out.println("test");
}
@Override
public Model2 read() throws Exeption {
.
.
.
}
}
Upvotes: 0
Views: 787
Reputation: 31600
I would use a StepExecutionListener#beforeStep for that matter. It is more appropriate for step initialization rather than doing the initialization in the reader's constructor.
Upvotes: 1