首页 > 解决方案 > 如果我们在 Spring Batch 中执行特定方法,如何在 tasklet 中获取作业参数

问题描述

我为执行 3 个不同的作业指定了 3 个步骤。让我们说a,b和c。我在我的spring批处理xml中配置了它。应该这样配置,如果我执行 a,那么只有在成功时才会执行 b,否则会中断,对于 b 也是如此。我还想根据作业参数输入之一执行特定步骤。(如果我得到 b 的输入,那么它将直接执行 b,而不是 a)。我的春季批处理xml:

<batch:job id ="MyJob">
<batch:step id = "a">
<batch:tasklet ref="myJavaClassService" method="callingA"/>
<batch:next on="*" next="b"/>
<batch:next on="FAILED" next="exit"/>
</batch:step>
<batch:step id = "b">
<batch:tasklet ref="myJavaClassService" method="callingB"/>
<batch:next on="*" next="c"/>
<batch:next on="FAILED" next="exit"/>
</batch:step>
<batch:step id = "c">
<batch:tasklet ref="myJavaClassService" method="callingC"/>
<batch:next on="FAILED" next="exit"/>
</batch:step>
<batch:step id = "exit">
<batch:tasklet ref="myJavaClassService" method="exit"/>
</batch:step>
</batch:job>

我的java服务类如下:

@Service("myJavaClassService")
class MyJavaClassService
{
    public void callingA() {  /* Some Operation --- */ }
    public void callingB() {  /* Some Operation --- */ }
    public void callingC() {  /* Some Operation --- */ }
    public void exit() {  System.exit(1); }
}

我的Java主类如下:

class MyClassMain
{
/*---- some configuration related code ---*/

JobLauncher jobLauncher = (JobLauncher) ctx.getBean("JobLauncher");

Job job = (Job) ctx.getBean("MyJob");

JobParametersBuilder jpd = new JobParametersBuilder();
jpd.addString("firstParam");
jpd.addString("secondParam");
jpd.addString("thirdParam");

JobParameters jp = jpd.toParameters();

JobExecution result = jobLauncher.run(job, jp);

}

我坚持以下几点:

如何获取服务类中的所有作业参数?我尝试实施Tasklet, ItemPrcoess,StepListener等,但没有一个适合我。

如何根据作业参数执行特定步骤,假设我想直接执行“步骤 id = b”?

如何从任何调用方法中给出返回类型,例如我需要从中获取一个输出值callingA以及我需要传递给callingB方法的输出值,我该怎么做。

我试图在我的调用方法中使用@BeforeStep方法获取作业参数,StepExecutionListenerbeforeMethod没有执行,不知道为什么。例如:我 String first param = executionContext.getStepExecution.getString("firstparam")beforeStep方法中写了一行,但我无法在我的callingA方法中获取第一个参数值。因此,我意识到,没有一个听众在我的情况下工作。

请注意,我在我的方法中接到了一个电话,callingA但我无法获得作业参数。

另外,根据我的要求,如果您有任何其他可行的解决方案,我将不胜感激。我对块或任何读写器处理器工作没有任何要求。简单调用 3 种方法,其中包含执行不同 python 作业的代码。

标签: javaspringjobs

解决方案


推荐阅读