Skip to content

Chunk oriented step builders must validate that an ItemWriter is provided [BATCH-2624] #979

Closed
@spring-projects-issues

Description

@spring-projects-issues

Marten Deinum opened BATCH-2624 and commented

Using Java based configuration in a project we accidentally created a chunk based step without a writer.

stepBuilder.chunk(50).reader(someReader).processor(someProcessor).build();

The SimpleChunkProcessor expects both the ItemProcessor and ItemWriter to be set (according to the validation done in the afterPropertiesSet method.

@Override
protected Tasklet createTasklet() {
     Assert.state(reader != null, "ItemReader must be provided");
     Assert.state(processor != null || writer != null, "ItemWriter or ItemProcessor must be provided");
     RepeatOperations repeatOperations = createChunkOperations();
     SimpleChunkProvider<I> chunkProvider = new SimpleChunkProvider<I>(reader, repeatOperations);
     SimpleChunkProcessor<I, O> chunkProcessor = new SimpleChunkProcessor<I, O>(processor, writer);
     chunkProvider.setListeners(new ArrayList<StepListener>(itemListeners));
     chunkProcessor.setListeners(new ArrayList<StepListener>(itemListeners));
     ChunkOrientedTasklet<I> tasklet = new ChunkOrientedTasklet<I>(chunkProvider, chunkProcessor);
     tasklet.setBuffering(!readerTransactionalQueue);
     return tasklet;
}

The createTasklet method in both the SimpleStepBuilder as well as the FaultTolerantStepBuilder accept a null ItemWriter as long as there is a processor. I would expect a ItemWriter next to an ItemReader to be mandatory (which is also what is expressed in the Spring Batch Documentation) but the java config isn't enforcing this, leading to unexpected behavior.

In the XML configuration an ItemReader and ItemWriter are required where an ItemProcessor is optional. (See the ChunkElementParser for that).

The change that lead to this was introduced in BATCH-1520 by @david_syer (so maybe there is a valid reason for this, but still it is weird that XML and Java config lead to different results).


Affects: 3.0.7

Referenced from: pull request #491

Backported to: 4.1.0.M1, 4.0.2, 3.0.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    has: backportsLegacy label from JIRA. Superseded by "for: backport-to-x.x.x"in: coretype: bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions