Skip to content

Type safe alternative for PropertyExtractingDelegatingItemWriter #4735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
scordio opened this issue Dec 17, 2024 · 2 comments · May be fixed by #4890
Open

Type safe alternative for PropertyExtractingDelegatingItemWriter #4735

scordio opened this issue Dec 17, 2024 · 2 comments · May be fixed by #4890
Labels
status: waiting-for-triage Issues that we did not analyse yet type: feature

Comments

@scordio
Copy link
Contributor

scordio commented Dec 17, 2024

I've recently encountered the need for an adapter to use an existing ItemWriter<SimpleType> in a step that works with ComplexType items, where a ComplexType instance encapsulates a SimpleType one (imagine a ComplexType::getSimpleType getter available).

While PropertyExtractingDelegatingItemWriter seems to be a solution, I didn't find it type-safe due to its underlying dependency on the MethodInvoker. I also looked at #4672, but they don't seem to help my use case.

Taking inspiration from java.util.stream.Collectors.mapping(Function, Collector), I came up with a custom ItemWriter adapter for this purpose, statically imported as mapping(Function, ItemWriter), which allows writing a step definition like the following:

factory.get("step")
  .<ComplexType, ComplexType>chunk(10)
  .reader(/* an ItemReader<ComplexType> */)
  .writer(mapping(ComplexType::getSimpleType, new SimpleTypeItemWriter()))
  .build()

I heavily simplified the example to give a feeling about the core idea. Arguably, that example can also be solved with a processor:

factory.get("step")
  .<ComplexType, SimpleType>chunk(10)
  .reader(/* an ItemReader<ComplexType> */)
  .processor(/* an ItemProcess<ComplexType, SimpleType> */)
  .writer(new SimpleTypeItemWriter())
  .build()

However, my real-life use case also involves a CompositeItemWriter where each delegate expects items of a different type, all encapsulated by ComplexType. Something like the following:

factory.get("step")
  .<ComplexType, ComplexType>chunk(10)
  .reader(/* an ItemReader<ComplexType> */)
  .writer(new CompositeItemWriterBuilder<>()
    .delegates(
      mapping(ComplexType::getSimpleType, new SimpleTypeItemWriter())),
      mapping(ComplexType::getAnotherType, new AnotherTypeItemWriter())),
      mapping(ComplexType::getOneMoreType, new OneMoreTypeItemWriter())),
      /* etc */
    )
    .build())
  .build()

Would Spring Batch be interested in introducing such an adapter, maybe as a static factory method under ItemWriter for better discoverability?

If yes, I would be happy to provide a PR for it.

@scordio scordio added status: waiting-for-triage Issues that we did not analyse yet type: feature labels Dec 17, 2024
@scordio
Copy link
Contributor Author

scordio commented Jun 13, 2025

This can also be relevant for ItemReader and ItemProcessor.

@scordio scordio linked a pull request Jun 14, 2025 that will close this issue
@scordio
Copy link
Contributor Author

scordio commented Jun 15, 2025

While PropertyExtractingDelegatingItemWriter seems to be a solution

After some experimentation, PropertyExtractingDelegatingItemWriter is not a suitable solution for my use case, as it attempts to invoke the delegate method (in this case, write(Chunk)) with each mapped value, rather than building up a new chunk with the mapped values.

This is just to confirm that I was unable to find an out-of-the-box solution for my use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage Issues that we did not analyse yet type: feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant