'Execute a Step based on properties file in Spring Batch [duplicate]
I have to run a Spring Batch Job comprising of steps A,B,C based on properties file . I found out that we can use JobExecutionDecider in spring batch . But most of the examples given are using single condition . For example
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);
}
}
The above example uses only shouldNotify() . But in my case I need to use the same JobExecutionDecider to check three different properties and return the status dynamically . I need the functionality like below
//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
Solution 1:[1]
Just add variables and set them during bean initialization
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 config
<bean id="decider" class="com.example.NumberInfoDecider">
<property name="stepA" value="${stepA}"/>
<property name="stepB" value="${stepB}"/>
<property name="stepC" value="${stepC}"/>
</bean>
Or 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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ACH |
