springspring-batchspring-bean

Issue in creating instance of peeking reader in spring batch job


I have Master step

@Bean
public Step employeeMasterProfilePhoneStep() throws UnexpectedInputException, ParseException {
    return stepBuilderFactory.get(Constant.MASTER_PHONE_STEP)
            .partitioner(Constant.PARTITIONER_STEPNAME_WORKERSTEP, employeeProfilePhonePartitioner())
            .listener(employeeProfileStepListener())
            .step(employeeProfilePhoneStep())
            .taskExecutor(employeeProfileTaskExecutor())
            .gridSize(20)
            .build();

}

Worker step

@Bean
public Step userProfilePhoneStep() {
    return stepBuilderFactory.get("userProfilePhoneStep")
            .<UserProfile, UserProfile>chunk(2000)
            .reader(custPhonePeekingReader())
            .processor(employeeProfileProcessor())
            .writer(employeeProfileItemWriter)
            .listener(ItemReaderListener)
            .listener(ChunkListener)
            .build();
}

Stepscope on method which creates objects based on number of execution context in Partitioner (In my case I have 3 execution contect)

@Bean
@StepScope
public CustPhonePeekingReader custPhonePeekingReader() {
 return  new CustPhonePeekingReader();

}

My custPhonePeekingReader() is never invoked from the worker step even though it should have created 3 objects since I have 3 execution context created in partitioner.

But if I change the method name to customerPhonePeekingReader or anything other than "custPhonePeekingReader" worker step able to invoke and create 3 instances of CustPhonePeekingReader. Any issue or conflict in bean name ? Any help is much appreciated


Solution

  • In your master step you have:

    .step(employeeProfilePhoneStep())
    

    but the worker step is called userProfilePhoneStep(). This is incorrect.

    For the reader, since you are using a method call to set the bean here .reader(custPhonePeekingReader()), then what you are describing should not happen. You did not share all the code so the issue should be elsewhere.

    You can try to inject the reader like this:

    @Bean
    public Step userProfilePhoneStep(CustPhonePeekingReader custPhonePeekingReader) {
        return stepBuilderFactory.get("userProfilePhoneStep")
                .<UserProfile, UserProfile>chunk(2000)
                .reader(custPhonePeekingReader)
                .processor(employeeProfileProcessor())
                .writer(employeeProfileItemWriter)
                .listener(ItemReaderListener)
                .listener(ChunkListener)
                .build();
    }
    

    I would do the same for the processor and writer.