1

I have a snipped as mentioned below for step processing.

@Bean
@JobScope
public Step readStep(StepBuilderFactory sbf, 
ItemReader reader,ItemWriter writer, TaskExecutor taskExecutor, 
RefFileReaderComponentFactory componentFactory, String file){
        final Step step = sbf.get(file)
                             .chunk(100)
                             .reader(reader)
                             .faultTolerant()
                             .skipPolicy(new SkipPolicy()) //dummy skip policy for representation purpose
                             .writer(writer)
                             .faultTolerant()
                             .skipPolicy(new SkipPolicy())
                             .listener(new NewStepExecutionListener()) //This dummy listener is implementation of the StepExecutionListener
                             //.listener(new NewItemWrtieListener) //This dummy listener is implementation of the ItemWriteListener
                             .taskExecutor(taskExecutor)
                             .build();
        return  step;
}

But the above code throws compile time error - The method taskExecutor(TaskExecutor) is undefined for the type StepBuilderHelper.

Interestingly, this error is only shown when I put NewStepExecutionListener listener but if I comment it out and uncomment NewItemWrtieListener then it works fine.

Post typecasting, code looks like this:

@Bean
@JobScope
public Step readStep(StepBuilderFactory sbf, 
ItemReader reader,ItemWriter writer, TaskExecutor taskExecutor, 
RefFileReaderComponentFactory componentFactory, String file){
        final Step step = ((AbstractTaskletStepBuilder<SimpleStepBuilder<Object, Object>>) sbf.get(file)
                             .chunk(100)
                             .reader(reader)
                             .faultTolerant()
                             .skipPolicy(new SkipPolicy()) //dummy skip policy for representation purpose
                             .writer(writer)
                             .faultTolerant()
                             .skipPolicy(new SkipPolicy())
                             .listener(new NewStepExecutionListener())) //This dummy listener is implementation of the StepExecutionListener
                             //.listener(new NewItemWrtieListener) //This dummy listener is implementation of the ItemWriteListener
                             .taskExecutor(taskExecutor)
                             .build();
        return  step;
}

I tried finding reason behind this but cannot understand why a change in Listener Type would require typecasting for step processing.

Can someone explain why ?

1 Answers1

0

The order of method calls matters because some methods may change the builder type. For example, when you call faultTolerant(), the builder type is changed from SimpleStepBuilder to FaultTolerantStepBuilder, adding new methods to configure fault tolerance (skip, retry, etc).

So you need to check the order and return type of each method and make sure the builder type returned defines the next method you want to call.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50