2
votes

I originally asked a question at [Getting "Scope 'step' is not active for the current thread" while creating spring batch beans, and based on the suggestion provided at Spring batch scope issue while using spring boot, I tried to replace @StepScope annotation and instead defined StepScope bean in configuration as below

@Bean
@Qualifier("stepScope")
public org.springframework.batch.core.scope.StepScope stepScope() {

    final org.springframework.batch.core.scope.StepScope stepScope = new org.springframework.batch.core.scope.StepScope();
    stepScope.setAutoProxy(true);
    return stepScope;
}

with this change, I'm not able to pass job parameters while creating beans as, it is throwing

'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'

My configuration is like

@Configuration
@EnableBatchProcessing
public class MyConfig{

@Bean
@Qualifier("partitionJob")
public Job partitionJob() throws Exception {

    return jobBuilderFactory
            .get("partitionJob")
            .incrementer(new RunIdIncrementer())
            .start(partitionStep(null))
             .build();
}

@Bean
@StepScope
public Step partitionStep(
        @Value("#{jobParameters[gridSize]}") String gridSize)
        throws Exception {

    return stepBuilderFactory
            .get("partitionStep")
            .partitioner("slaveStep", partitioner())
            .gridSize(gridZize)
            .step(slaveStep(chunkSize))
            .taskExecutor(threadPoolTaskExecutor()).build();
}

@Bean
@StepScope
public Step slaveStep(int chunkSize) throws Exception {

    return stepBuilderFactory
            .get("slaveStep")
            ......
            ........
}

I read that the bean should be annotated with @StepScope,if job Parameters needs to be accessed like my example. But I'm getting exceptions as explained above.

1
I tried a workaround by passing gridsize as java system property -DgridSize=10 and injected that using @Value annotation. May be at stepScope annotation and JobParameter injection work together only when used on readers/writers/processors. Not on steps themselves. - DBreaker
Have you tried to imitate this in xml config for testing purposes? I think you should not get this error in xml. I understand that you might need to make this work on spring boot but just in case this is worth giving a try. - ANKIT
Not yet, probably will try xml configuration, instead of java configuration and see how it works. Thanks for the suggestion though. - DBreaker
Have you tried using @JobScope instead? - Philip Wrage

1 Answers

0
votes

This is how you can pass job parameters

  GenericApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
    JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");     
        JobParameters jobParameters = new JobParametersBuilder()
                                                               .addString("paramter1", "Test")  
                                                               .toJobParameters();

        Job job = (Job) context.getBean("myJob");
        JobExecution execution = jobLauncher.run(job, jobParameters);

Access Job parameters

    Bean
@StepScope
public Step partitionStep(
        @Value("#{jobParameters['paramter1']}") String gridSize)
        throws Exception {

    return stepBuilderFactory
            .get("partitionStep")
            .partitioner("slaveStep", partitioner())
            .gridSize(gridZize)
            .step(slaveStep(chunkSize))
            .taskExecutor(threadPoolTaskExecutor()).build();
}