首页 文章

Spring Batch使用@JobScope中的步骤定义基于java的条件流

提问于
浏览
2

我在Spring Batch中定义包含 conditional flow using steps in @JobScope 的基于java的作业配置时遇到问题,即在作业执行时将作业参数注入步骤 .

我的设置:

  • Spring Batch 3.0.4

  • Spring Batch Admin 2.0.0.M1(在运行时提供jobRepository,transactionManager,jobScope,stepScope bean)

假设我有一个包含两个步骤step1和step2的简单作业,那么在没有条件流的情况下定义和执行作业配置是有效的:

@Configuration
public class SimpleJobConfiguration {

@Bean
@JobScope
Step1Tasklet step1Tasklet(@Value("#{jobParameters['condition']}") String condition) {
    Step1Tasklet tasklet = new Step1Tasklet(condition);
    return tasklet;
}

// reader, writer ommitted

@Bean
@JobScope
public Step step1(@Value("#{jobParameters['condition']}") String condition) {
    TaskletStep step = stepBuilderFactory().get(STEP_NAME_1)
            .tasklet(step1Tasklet(condition))
            .build();
    return step;
}

@Bean
@JobScope
public Step step2() {
    TaskletStep step = stepBuilderFactory().get(STEP_NAME_2)
            .<String, String>chunk(10)
            .reader(reader(null))
            .writer(writer())
            .allowStartIfComplete(true)
            .build();
    return step;
}

@Bean
public Job simpleJob() {
    Job job = jobBuilderFactory().get(JOB_NAME)
            .start(step1(null))
            .next(step2())
            .build();
    return job;
}
  • 部署作业配置时,Spring Batch正在初始化我的Job bean without any exceptions . 它没有初始化我的步骤 - 这是有道理的,因为它们在Scope "Job"中 .

  • 启动此作业配置的执行时,Spring Batch正在初始化这两个步骤,按预期注入作业参数 .

  • 作业按预期执行,首先是步骤1,然后是步骤2 .

现在我想为我的Job添加一个简单的条件逻辑:

"IF (step1.exitStatus == "OK") THEN execute step2 ELSE finish job"

为了实现这一点,我已经定义了一个decider bean(实现JobExecutionDecider)并修改了我的Job定义:

@Bean
public SimpleStepDecider decider() {
    SimpleStepDecider decider = new SimpleStepDecider(); 
    return decider;
}

@Bean
// DOES NOT WORK; NEEDS TO BE FIXED!
public Job simpleJob() {
    Job job = jobBuilderFactory().get(JOB_NAME)
            .start(step1(null))
            .next(decider())
            .on("OK")
            .to(step2())
            .end()
            .build();
    return job;
}

使用此方法,我在作业配置的部署(!)期间遇到以下异常:

14:28:07,299 ERROR batch.local-startStop-1 context.ContextLoader:331 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleJob' defined in com.foo.bar.SimpleJobConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Job]: Factory method 'simpleJob' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.step1': Scope 'job' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for job scope
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
...
        at org.springframework.batch.core.configuration.support.GenericApplicationContextFactory$ResourceXmlApplicationContext.<init>(GenericApplicationContextFactory.java:161)
...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Job]: Factory method 'simpleJob' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.step1': Scope 'job' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for job scope
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
        ... 39 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.step1': Scope 'job' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for job scope
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:352)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:187)
        at com.sun.proxy.$Proxy33.getName(Unknown Source)
        at org.springframework.batch.core.job.builder.FlowBuilder.createState(FlowBuilder.java:282)
        at org.springframework.batch.core.job.builder.FlowBuilder.doStart(FlowBuilder.java:265)
        at org.springframework.batch.core.job.builder.FlowBuilder.start(FlowBuilder.java:122)
        at org.springframework.batch.core.job.builder.JobFlowBuilder.<init>(JobFlowBuilder.java:39)
        at org.springframework.batch.core.job.builder.SimpleJobBuilder.next(SimpleJobBuilder.java:133)
        at com.foo.bar.SimpleJobConfiguration.simpleJob(SimpleJobConfiguration.java:145)
        ... 40 more
Caused by: java.lang.IllegalStateException: No context holder available for job scope
        at org.springframework.batch.core.scope.JobScope.getContext(JobScope.java:153)
        at org.springframework.batch.core.scope.JobScope.get(JobScope.java:92)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337)
        ... 60 more

我试过了:

  • 将步 beans 移动到"normal"范围

  • 使用步骤执行侦听器而不是决策程序

但是根本没有运气......

有趣的是,我能够使用基于xml的配置定义相同的工作,并且它按预期工作(决策者使用作业参数'条件'的值作为退出状态代码;我测试了“OK”和“NOTOK”并且他们按预期工作):

<bean id="reader" class="com.foo.bar.MyReader" scope="job">
</bean>

<bean id="writer" class="com.foo.bar.MyWriter" />

<bean id="decider" class="com.foo.bar.SimpleStepDecider" />

<bean id="step1Tasklet" class="com.foo.bar.Step1Tasklet" scope="job">
    <constructor-arg value="#{jobParameters['condition']}" />
</bean>

<batch:job id="simpleJob" restartable="true">
    <batch:step id="step1" next="decision" allow-start-if-complete="true">
        <batch:tasklet ref="step1Tasklet" />
    </batch:step>
    <batch:decision id="decision" decider="decider">
        <batch:end on="NOTOK" />
        <batch:next on="OK" to="step2"/>
        <batch:fail on="*"/>
    </batch:decision>
    <batch:step id="step2" allow-start-if-complete="true">
        <batch:tasklet allow-start-if-complete="true">
            <batch:chunk reader="reader" writer="writer" commit-interval="1000" />
        </batch:tasklet>
    </batch:step>
</batch:job>

任何人都可以给我一个暗示我如何让我的java配置工作,即步骤bean在作业执行期间实例化?

2 回答

相关问题