17

I want to configure Spring Batch job, but I receive the following error, how can I solve it?

Error: enter image description here

Reader:

import org.springframework.batch.item.ItemReader;

public class MoviesReader implements ItemReader<SearchResponseRO>, StepExecutionListener {

    @Override
    public SearchResponseRO read() throws Exception {
        return new SearchResponseRO();
    }
}

Processor:

import org.springframework.batch.item.ItemProcessor;

public class MoviesProcessor implements ItemProcessor<SearchResponseRO, Movie> {
    @Override
    public Movie process(SearchResponseRO searchResponseRO) throws Exception {
        return new Movie();
    }
}

What do I need to change in order to fix the issue?

Thanks.

1 Answer 1

41

You need to specify a type for the chunk operation. In your case that would be <SearchResponseRO, Movie>.

return stepBuilderFactory.get("downloadStep").<SearchResponseRO, Movie>chunk(10)
  .reader(reader)
  .processor(processor)
  .....

Without the type, it defaults to <Object, Object>:

stepBuilderFactory.get("test").chunk(10)
        .reader(new ItemReader<Object>() {
            @Override
            public Object read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
                return null;
            }
        })
        .processor(new ItemProcessor<Object, Object>() {
            @Override
            public Object process(Object o) throws Exception {
                return null;
            }
        })
        .writer(new ItemWriter<Object>() {
            @Override
            public void write(List<?> list) throws Exception {

            }
        })
        .build();

If you look at the definition of the chunk method, it accepts an int, but returns SimpleStepBuilder<I, O>. Because there is no way to actually provide the types for I and O, you have to essentially cast them to the values that you want. I believe that the .<Type> syntax is just convenience for the direct cast when chaining calls, so the following two things should be the same:

public void castGenericReturnType() {
    System.out.println(this.<Integer>genericReturn(1));
    System.out.println((Integer) genericReturn(1));
}

public <I> I genericReturn(Object objectToCast) {
    return (I) objectToCast;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had refactored my configs and did not pay attention to <~>.chunk (It was obfuscated). Thanks to you I realized it has the wrong <I,O>.chunk types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.