我在使用@JobScope 中的步骤在Spring Batch中定义包含条件流的基于java的作业配置时遇到问题,即在作业执行时将作业参数注入到步骤中。
我的设置:
假设我有一个包含两个步骤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;
}
现在我想为我的Job添加一个简单的条件逻辑:
&#34; IF(step1.exitStatus ==&#34; OK&#34;)然后执行step2 ELSE完成工作&#34;
为了实现这一点,我已经定义了一个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
我试过了:
但是没有运气......
有趣的是,我能够使用基于xml的配置定义相同的作业,并且它按预期工作(决策者使用作业参数的值&#39;条件&#39;作为退出状态代码;我测试了两者& #34;好的&#34;和#34; NOTOK&#34;他们按预期工作):
<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在作业执行期间是否会实现?
答案 0 :(得分:1)
可能是您遇到了目前尚未解决的BATCH-2229: Unable to use Job Scope beans in a multi-threaded or partitioned step。
这是为了响应从分区和/或多线程步骤访问Job Scoped bean而引发的:
答案 1 :(得分:0)
我认为问题是“step1(null)”,尝试改变如下:
public Job simpleJob(Step step1) {
Job job = jobBuilderFactory().get(JOB_NAME)
.start(step1)
.next(decider())
.on("OK")
.to(step2())
.end()
.build();
return job;}