0

I´m trying to inject parameters from an outside context to an ItemReader using spring batch.

Below I have my code that trigger the job:

    Date date = Date.from(advanceSystemDateEvent.getReferenceDate().atStartOfDay(ZoneId.systemDefault()).toInstant());      
    JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
    jobParametersBuilder.addLong("uniqueness", System.nanoTime());
    jobParametersBuilder.addDate("date", date);     
    jobLauncher.run(remuneradorJob, jobParametersBuilder.toJobParameters());

My job:

@EnableBatchProcessing
@Configuration
public class JobsConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Bean
    public Job remuneradorJob(Step remuneradorStep) {
        return jobBuilderFactory
            .get("remuneradorJob")
            .start(remuneradorStep)
            .incrementer(new RunIdIncrementer())
            .build();
    }
}

My step:

@Configuration
public class StepsConfig {

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Bean
public Step remuneradorStep(ItemReader<Entity> remuneradorReader, ItemWriter<Entity> remuneradorWriter) {
    return stepBuilderFactory
            .get("remuneradorStep")
            .<Entity, Entity>chunk(1)
            .reader(remuneradorReader)
            .writer(remuneradorWriter)
            .build();
}
}

My itemreader:

@Configuration
public class RemuneradorReaderConfig {

@Autowired
private EntityManagerFactory entityManagerFactory;  

@Bean
@StepScope
public JpaPagingItemReader<Entity> remuneradorReader(@Value("#{jobParameters}") Map jobParameters) {

    //LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    LocalDate localDate = LocalDate.of(2011,5,16);
    JpaPagingItemReader<Entity> databaseReader = new JpaPagingItemReader<>();
    databaseReader.setEntityManagerFactory(entityManagerFactory);
    databaseReader.setQueryString("...");
    databaseReader.setPageSize(1000);
    databaseReader.setParameterValues(Collections.<String, Object>singletonMap("limit", localDate));
    return databaseReader;
}
}

I got the error:

Error creating bean with name 'scopedTarget.remuneradorReader': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope

I tried replacing @StepScope for @JobScope, but I got the same error.

I saw this issue:

Spring batch scope issue while using spring boot

And finally, the application runs.

But now I´m facing another problem, according the code below:

@Bean
@StepScope
public JpaPagingItemReader<Entity> remuneradorReader(@Value("#{jobParameters}") Map jobParameters) {

JpaPagingItemReader<Entity> databaseReader = new JpaPagingItemReader<>();
databaseReader.setEntityManagerFactory(entityManagerFactory);
databaseReader.setQueryString("select o from Object o");
databaseReader.setPageSize(1000);
return databaseReader;
}

When I execute this reader, it gives me:

Deposit{Deposit_ID='null', Legacy_ID ='null', Valor_Depósito='10000', Saldo='10000'}

The idDeposit and idLegacy comes null.

But when I remove @StepScope and @Value("#{jobParameters}") Map jobParameters from ItemReader, like code below:

@Bean
public JpaPagingItemReader<Entity> remuneradorReader() {

JpaPagingItemReader<Entity> databaseReader = new JpaPagingItemReader<>();
databaseReader.setEntityManagerFactory(entityManagerFactory);
databaseReader.setQueryString("select o from Object o");
databaseReader.setPageSize(1000);
return databaseReader;
}

The reader gives me the correct response:

Deposit{Deposit_ID='98', Legacy_ID ='333', Valor_Depósito='10000', Saldo='10000'}

I think it´s missing something else.

Can anyone help me?

Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
  • Does this answer your question? [Spring batch scope issue while using spring boot](https://stackoverflow.com/questions/28457107/spring-batch-scope-issue-while-using-spring-boot) – Mahmoud Ben Hassine Mar 02 '21 at 09:49
  • I saw this issue above, but I still have doubts about the problem. For example, where I put this xml: Another doubt: I tried this piece of code: @Bean public StepScope stepScope() { final StepScope stepScope = new StepScope(); stepScope.setAutoProxy(true); return stepScope; } But the IDE doesn´t allow me instantiate StepScope class, because it´s an abstract class. – user2424634 Mar 02 '21 at 10:03
  • `StepScope` is not an abstract class: https://github.com/spring-projects/spring-batch/blob/d8fc58338d3b059b67b5f777adc132d2564d7402/spring-batch-core/src/main/java/org/springframework/batch/core/scope/StepScope.java#L65. – Mahmoud Ben Hassine Mar 02 '21 at 10:07
  • I made a wrong import. The correct is this one: import org.springframework.batch.core.scope.StepScope; – user2424634 Mar 02 '21 at 12:53
  • Yes, with this one, it should work, as mentioned in the duplicate question. – Mahmoud Ben Hassine Mar 02 '21 at 13:03
  • I edited the question above. – user2424634 Mar 02 '21 at 18:09
  • Please provide a minimal complete example that reproduces the issue to be able to help you: https://stackoverflow.com/help/minimal-reproducible-example. – Mahmoud Ben Hassine Mar 03 '21 at 16:03

0 Answers0