Uploaded image for project: 'Spring Batch'
  1. Spring Batch
  2. BATCH-2624

Chunk oriented step builders must validate that an ItemWriter is provided

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.0.7
    • Fix Version/s: 4.1.0, 4.0.2, 3.0.10, 4.1.0.M1
    • Component/s: Core
    • Labels:
      None

      Description

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

        Attachments

          Activity

            People

            • Assignee:
              mbenhassine Mahmoud Ben Hassine
              Reporter:
              mdeinum Marten Deinum
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: