Open kzander91 opened 3 years ago
Thank you for opening this issue and for providing a minimal example.
The problem here is the scoped step. A step should not be scoped (with step scope or job scope). what should be scoped instead is the component of the step (tasklet, reader, writer, etc). In your case, it is the item processor that should be scoped to use late-binding, not the step itself. Here is an example of how your sample should be changed:
@Bean
Job job() {
return jobs.get("job") //
.start(step()) //
.build();
}
@Bean
Step step() {
return steps.get("step") //
.<String, String>chunk(1) //
.reader(() -> String.valueOf(Math.random())) //
.processor(itemProcessor(null)) //
.writer(new ListItemWriter<>()) //
.build();
}
@Bean
@StepScope
public ItemProcessor<String, String> itemProcessor(@Value("#{jobParameters['param']}") String param ) {
return item -> {
log.info("Processing item: '{}' Param: '{}'", item, param);
TimeUnit.SECONDS.sleep(1L);
return item;
};
}
With those changes, the sample works as expected.
There is a mention about not scoping Step
beans in the documentation here, but it only mentions step-scope while it should include job scope as well.
I will change this into a documentation issue and update the docs accordingly.
Hi @fmbenhassine ,
if steps should have scope step or job, how can i dynamically set the chunkSize from a job parameter? In https://stackoverflow.com/a/67365622/4073333 your advice was to use @JobScope for the step, but that is not righy anymore, is it?
edit:
Is this a good solution?
@Bean
@StepScope
public SimpleCompletionPolicy chunkCompletionPolicy(@Value("#{jobParameters['chunkSize']}") int chunkSize) {
return new SimpleCompletionPolicy(chunkSize);
}
@Bean
public Step step(PlatformTransactionManager transactionManager,
SimpleCompletionPolicy simpleCompletionPolicy) {
return steps
.get("exampleStep")
.chunk(simpleCompletionPolicy, transactionManager)
.reader(reader1())
.processor(processor())
.writer(dbResultWriter())
.build();
}
Bug description I have a
Job
that contains aStep
that is annotated with@JobScope
(to be able to inject job parameters). UsingJobOperator.stop()
from another thread to stop that job fails with the following exception:Environment Spring Batch: 4.3.2
Steps to reproduce
JobExecution
).JobOperator.stop()
to stop that job.Expected behavior Stopping the job works, regardless of the scope of its steps.
Minimal Complete Reproducible example Sample project: demo.zip
The project contains a job-scoped step. A
CommandLineRunner
creates aJobLauncher
that launches jobs asynchronously. Then, the main thread waits two seconds before it callsJobOperator.stop()
../mvnw spring-boot:run
JobOperator.stop()
is called and fails with the exception above.