执行Spring Batch中基于属性文件的Step



我必须运行一个Spring批处理作业,包括步骤a,B,C基于属性文件。我发现我们可以在春季批中使用JobExecutionDecider。但是给出的大多数例子都是使用单一条件。例如

public class NumberInfoDecider implements JobExecutionDecider {
private boolean shouldNotify() {
return true;
}
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
if (shouldNotify()) {
return new FlowExecutionStatus(NOTIFY);
} else {
return new FlowExecutionStatus(QUIET);
}
}

上面的示例只使用shouldNotify()。但在我的情况下,我需要使用相同的JobExecutionDecider来检查三个不同的属性,并动态返回状态。我需要下面的功能

//Properties file
StepA=true
StepB=false
StepC=false
//Program Functionality
if(stepA)
execute StepA
if(Step B)
execute Step B
if(Step C)
execute Step C

只需添加变量并在bean初始化时设置它们

public class NumberInfoDecider implements JobExecutionDecider {
private String stepA;
private String stepB;
private String stepC;
//getters and setters
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
if (stepA.equals("true")) {
return new FlowExecutionStatus(A);
} else if(stepB.equals("true")) {
return new FlowExecutionStatus(B);
} else if(stepC.equals("true")) {
return new FlowExecutionStatus(C);
}
}
}

XML配置

<bean id="decider" class="com.example.NumberInfoDecider">
<property name="stepA" value="${stepA}"/>
<property name="stepB" value="${stepB}"/>
<property name="stepC" value="${stepC}"/>
</bean>

或Java config

@Value(${stepA})
String stepA;
@Value(${stepB})
String stepB;
@Value(${stepC})
String stepC;
@Bean
public NumberInfoDecider decider() {
NumberInfoDecider stepDecider = new NumberInfoDecider();
stepDecider.setStepA = stepA;
stepDecider.setStepB = stepB;
stepDecider.setStepC = stepC;
return stepDecider;
}

相关内容

  • 没有找到相关文章

最新更新