2

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.

DBreaker
  • 319
  • 1
  • 4
  • 24
  • 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 May 30 '18 at 11:50
  • 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 May 30 '18 at 17:27
  • Not yet, probably will try xml configuration, instead of java configuration and see how it works. Thanks for the suggestion though. – DBreaker Jun 02 '18 at 03:45
  • Have you tried using `@JobScope` instead? – Philip Wrage May 01 '19 at 05:16

1 Answers1

0

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();
}
Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
  • Well as mentioned in my original post ( see link at the beginning of this post), I do get exception "Scope is not active in current thread", if I set StepScope annotation on the step itself. But it is working fine, if I use the stepscope on readers/writers. I'm not sure, if StepScope is not a valid annotation on step itself. – DBreaker Jun 02 '18 at 03:44
  • @DBreaker Did you solve this issue? – Hasan Can Saral Sep 06 '22 at 06:37