Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch configuration error in processor

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.

like image 421
boden Avatar asked May 15 '17 18:05

boden


People also ask

How do I enable batch processing in spring boot?

We need to add the @EnableBatchProcessing annotation in the configuration class file. The @EnableBatchProcessing annotation is used to enable the batch operations for your Spring Boot application. The reader() method is used to read the data from the CSV file and writer() method is used to write a data into the SQL.

Can we skip processor in Spring Batch?

Using skip and skipLimit. First of all, to enable skip functionality, we need to include a call to faultTolerant() during the step-building process. Within skip() and skipLimit(), we define the exceptions we want to skip and the maximum number of skipped items.

What is Spring Batch Processor?

An Item processor is a class which contains the processing code which processes the data read in to the spring batch. If the application reads n records the code in the processor will be executed on each record. A chunk is a child element of the tasklet. It is used to perform read, write, and processing operations.

How do you fail a Spring Batch?

1 Answer. Show activity on this post. public void myJobFailingMethod() { if(conditionsMatch()) { throw new CustomJobFailingException(); // create this exception class. // It will fail the job. } }


1 Answers

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;
}
like image 112
Brian Ecker Avatar answered Nov 04 '22 06:11

Brian Ecker