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

huangapple go评论65阅读模式
英文:

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&lt;MyMetadata&gt; sampleStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
    MyMetadata myMetaData  = ...;
	return new StepBuilder&lt;M&gt;(&quot;mySampleStep&quot;, jobRepository)
                .withCustomMetadata(myMetaData);
				.&lt;String, String&gt;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&lt;MyMetadata&gt; myStep = (Step&lt;MyMetadata&gt;)simpleJob.getStep(&quot;myStep&quot;)
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.

huangapple
  • 本文由 发表于 2023年7月18日 01:14:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定