I want to configure Spring Batch job, but I receive the following error, how can I solve it?
Error:
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.
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.
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.
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.
1 Answer. Show activity on this post. public void myJobFailingMethod() { if(conditionsMatch()) { throw new CustomJobFailingException(); // create this exception class. // It will fail the job. } }
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With