Search code examples
spring-batchspring-mybatis

Spring step does not run properly when I "fib" the reader, must I use a tasklet?


I'm aware that all spring steps need to have a reader, a writer, and optionally a processor. So even though my step only needs a writer, I am also fibbing a reader that does nothing but make spring happy.

This is based on the solution found here. Is it outdated, or am I missing something?

I have a spring batch job that has two chunked steps. My first step, deleteCount, is just deleting all rows from the table so that the second step has a clean slate. This means my first step doesn't need a reader, so I followed the above linked stackoverflow solution and created a NoOpItemReader, and added it to my stepbuilder object (code at the bottom).

My writer is mapped to a simple SQL statement that deletes all the rows from the table (code is at the bottom).

My table is not being cleared by the deleteCounts step. I suspect it's because I'm fibbing the reader.

I am expecting that deleteCounts will delete all rows from the table, yet it is not - and I suspect it's because of my "fibbed" reader but am not sure what I'm doing wrong.

My delete statement:

<delete id="delete">
    DELETE FROM ${schemaname}.DERP
</delete>

My deleteCounts Step:

@Bean
@JobScope
public Step deleteCounts() {
    StepBuilder sb = stepBuilderFactory.get("deleteCounts");
    SimpleStepBuilder<ProcessedCountData, ProcessedCountData> ssb = sb.<ProcessedCountData, ProcessedCountData>chunk(10);
    ssb.reader(noOpItemReader());
    ssb.writer(writerFactory.myBatisBatchWriter(COUNT_DATA_DELETE));
    ssb.startLimit(1);
    ssb.allowStartIfComplete(true);
    return ssb.build();
}

My NoOpItemReader, based on the previously linked solution on stackoverflow:

public NoOpItemReader<? extends ProcessedCountData> noOpItemReader() {
    return new NoOpItemReader<>();
}

// for steps that do not need to read anything
public class NoOpItemReader<T> implements ItemReader<T> {

    @Override
    public T read() throws Exception {
        return null;
    }
}

I left out some mybatis plumbing, since I know that is working (step 2 is much more involved with the mybatis stuff, and step 2 is inserting rows just fine. deleting is so simple, it must be something with my step config...)


Solution

  • Your NoOpItemReader returns null. An ItemReader returning null indicates that the input has been exhausted. Since, in your case, that's all it returns, the framework assumes that there was no input in the first place.