当前位置: 首页 > 知识库问答 >
问题:

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

柯琛
2023-03-14
    null
@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;
}
    null

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

“如果(step1.exitstatus==”ok“),则执行step2else finish job”

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

@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
    null

有趣的是,我能够使用基于XML的配置定义相同的作业,并且它按照预期工作(decider使用作业参数'condition'的值作为退出状态代码;我测试了“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在作业执行期间被实例化?

共有1个答案

钱展
2023-03-14

您可能遇到了批处理-2229:无法在多线程或分区步骤中使用作业范围bean,这一问题目前尚未解决。

这是在响应从分区和/或多线程步骤访问作业范围bean时提出的:

用分区步骤访问spring批处理中的@JobScope bean

 类似资料:
  • 我们在整个测试套件中有200多个特性和2000多个场景。它工作得很好,但许多步骤定义没有使用,我们想删除它们。 目前,我们很难手动尝试删除步骤定义代码,看看这样做是否破坏了什么。是否有一个工具或实用程序可以识别Java-Cucumber代码库中的哪些步骤定义未被使用,以便将其删除?

  • 我用Cucumber特性文件和Java步骤定义文件进行了一个简单的设置。 feature.feature->StepDefinition.java->PageObject.java 如上所示,我在这里使用了三个步骤定义文件。并且cucumber可以识别这两个文件中的步骤定义。但是当AcceptPage.java文件中定义了“and I Accept”步骤时,它甚至不尝试运行该步骤。如果我将它移动到

  • 我试图按照这篇文章将cucumber规格与IntelliJ中的步骤定义相匹配。 当我按Alt Enter时,我看到检查未定义的步骤选项。但是,我应该看到意图操作创建步骤定义。 我想我已经安装了Cucumber IntelliJ插件,所以这应该不是问题。非常感谢任何帮助。

  • 我不能为一个项目用cucumber执行简单的测试。我在Intellij13社区,使用cucumber插件。 我在features目录中编写了我的features文件,我还通过插件实现了创建它们的步骤。intellij可以识别功能文件中的我的步骤,它可以导航并转到步骤实现。当我尝试运行我的场景时,if无法声明“未定义的步骤”。任何帮助都将不胜感激。 以下是我如何组织我的项目:

  • 我想在spring验证器中创建基于条件的验证。我有一个UserDTO类,因为有两个DTO类带有注释。 如果我传递isPrimary,那么它应该只验证primaryDTO bean,而忽略secondoryDTO验证。 请引导。 谢谢

  • 我在用任何逻辑建立我的模型。我创建了一个带有自定义发行版的代理群体,在其中我放入了一个选项列表。我提出了5个选项(类别):A、B、C、D和E,每个选项都有其各自的百分比。我想做的是代理去两个不同的服务的基础上的选项(类别)。我在模型中有一个selectOutput,但我不知道该把什么作为条件来实现这一点,也就是说,如果代理是a或B,那么我希望它转到service_1,如果它是C、D或E,我希望它们