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

自闭症网瘾萝莉.ら 提交于 2019-12-11 14:09:54

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/48590704/spring-step-does-not-run-properly-when-i-fib-the-reader-must-i-use-a-tasklet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!