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

1

0

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...)

Slangy answered 2/2, 2018 at 20:51 Comment(2)
If you only need a writer, why are you not using a Tasklet?Butylene
@MichaelMinella I realized after writing this code that I could instead use a Tasklet. I am wondering, however, why this doesn't work to improve my understanding of spring batch. Based on previous stack overflow answers, I would think that using that noopitemreader would work...Slangy
B
3

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.

Butylene answered 2/2, 2018 at 21:35 Comment(1)
I see. I incorrectly assumed that the stack overflow solution I linked in the question would extend to work for Readers as well as Writers. That is an incorrect assumption, yes?Slangy

© 2022 - 2024 — McMap. All rights reserved.