Xml Spring batch job to execute like if else

1k Views Asked by At

I made spring batch job but I stuck at somewhere. I'm trying to take argument from user in spring batch XML job, based on that argument I'll run different steps.

For eg. Argument= new or replace Based on "Argument" , different steps will execute If argument=new then step 1 Else If argument=replace then step 2 Else some error

Any lead or help for your side is highly appreciated.

1

There are 1 best solutions below

3
On

You can create a decider based on a system property to decide which step to use, something like:

class MyDecider implements JobExecutionDecider {

    @Override
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
        String operation = System.getProperty("operation");
        if (operation.equalsIgnoreCase("create"))
            return new FlowExecutionStatus("create");
        else {
            return new FlowExecutionStatus("update");
        }
    }
}

Then use this decider in your job definition:

<beans:bean id="decider" class="MyDecider"/>

<job id="job">
    <step id="step1" next="decision" />

    <decision id="decision" decider="decider">
        <next on="create" to="createStep" />
        <next on="update" to="updateStep" />
    </decision>

    <step id="createStep"/>
    <step id="updateStep"/>
</job>