英文:
How to add custom properties to a Spring Batch Step, Job, Flow?
问题
我想将自定义(只读)元数据关联到Spring Batch作业、流程和步骤。这与将任意数据存储到执行上下文不同!如果我能像这样做就好了:
@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();
}
...这样,以便以后我可以获取我的自定义元数据(比如说,我想构建UI控制台来可视化作业、步骤和流程):
...
SimpleJob simpleJob = ...;
Step<MyMetadata> myStep = (Step<MyMetadata>)simpleJob.getStep("myStep");
MyMetadata myMetadata = myStep.getCustomMetadata();
不幸的是,Spring Batch API提供了一堆构建器,创建具体的类,没有办法以上述方式扩展作业、步骤或流程。Spring Batch文档中没有任何显示这是可能的内容。
有没有人有建议如何实现这一点?
英文:
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?
答案1
得分: 0
似乎您希望在步骤/作业之前和之后执行一些工作。您是否考虑使用StepExecutionListener或JobExecutionListener?这些监听器允许在作业之后和之前执行一些工作。例如,您可以将一些数据存储在数据库、缓存或执行上下文(步骤/作业级别)。如果我理解您的需求正确的话。
英文:
It looks like you would like to do some work before step/job and after step/job. Did you consider to use StepExecutionListener or JobExecutionListener? Those listeners allow to do some work after and berofe job. For instance you can store some data in db, or cache, or executionContext(step/job level). If I understand your needs rightly.
答案2
得分: 0
这是不可能的。您可以扩展Spring Batch提供的作业/步骤(以及它们各自的构建器,如果您想使用构建器),然后添加该功能。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论