Search code examples
springspring-batch

How to add custom properties to a Spring Batch Step, Job, Flow?


I would like to associate custom (read-only) metadata to Spring Batch jobs, flows, steps. (This is not same as storing arbitrary data to execution context, at the step execution level!). It would have been nice, if I could do something like this:

@Bean
public Step<MyMetadata> sampleStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
    MyMetadata myMetaData  = ...;
    return new StepBuilder<M>("mySampleStep", jobRepository)
                .withCustomMetadata(myMetaData);
                .<String, String>chunk(10, transactionManager)
                .reader(itemReader())
                .writer(itemWriter())
                .build();
}

.. so that later, I can get back my custom metadata (say I want to build UI console to visualize jobs, steps, flows):

...
SimpleJob simpleJob = ...;
Step<MyMetadata> myStep = (Step<MyMetadata>)simpleJob.getStep("myStep")
MyMetadata myMetadata = myStep.getCustomMetadata();

Unfortunately, Spring Batch API provides a maze of builders creating concrete classes with no way to extend jobs, steps or flows in the above manner. Nothing in the Spring Batch documentations shows this is even possible.

Does anyone have a suggestion how to achieve this?


Solution

  • This is not possible. You can extend the job/step provided by Spring Batch (along with their respective builders if you want to use builders) and add that.