CSGeek
CSGeek

Reputation: 3

Set property value during launch in spring batch

I am using spring batch 3.0. I have a scenario where I need to run approximately 1000s of jobs with 1 property value and same jobs with different property value. Is there a way to set property of job during Launch or scheduling time rather than in job configuration. Or any other way to achieve such functionality without replicating 1000s of job.

<batch:job id="job_A" parent="simpleJob">
        <batch:step id="A" parent="simpleStep">
            <batch:tasklet>
                <bean id="bA" class="ClassA" scope="step">
                        <property name="downloadFileA" value="false" />
                </bean>     
            </batch:tasklet>
         </batch:step>  
</batch:job>

And again same job with property value as true.

<batch:job id="job_A" parent="simpleJob">
        <batch:step id="A" parent="simpleStep">
            <batch:tasklet>
                <bean id="bA" class="ClassA" scope="step">
                        <property name="downloadFileA" value="true" />
                </bean>     
            </batch:tasklet>
         </batch:step>  
</batch:job>

Any help is highly appreciated.

Upvotes: 0

Views: 3036

Answers (2)

httPants
httPants

Reputation: 2133

You can use job parameters which get passed to the job launcher and are then accessible from the steps in your job.

When launching a job, you can do something like this...

JobParameters jobParameters = new JobParametersBuilder()
    .addString("downloadFileA", "true")
    .toJobParameters();

JobExecution jobExecution = jobLauncher.run(job, jobParameters);

You can then inject the jobParameter in your step...

@Autowired
@Value("#{jobParameters[downloadFileA]}")
String downloadFileA;

Upvotes: 2

Abdelghani Roussi
Abdelghani Roussi

Reputation: 2817

I think you should take a look at the Decider :

From spring batch documentation :

In some situations, more information than the ExitStatus may be required to decide which step to execute next. In this case, a JobExecutionDecider can be used to assist in the decision.

public class MyDecider implements JobExecutionDecider {
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
        if (someCondition) {
            return "FAILED";
        }
        else {
            return "COMPLETED";
        }
    }
}

In the job configuration, a "decision" tag will specify the decider to use as well as all of the transitions.

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

    <decision id="decision" decider="decider">
        <next on="FAILED" to="step2" />
        <next on="COMPLETED" to="step3" />
    </decision>

    <step id="step2" parent="s2" next="step3"/>
    <step id="step3" parent="s3" />
</job>

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

So in other words you should create a class that implements the JobExecutionDecider interface and then return a result based on your if-else test, and use this class in your spring batch xml configuration. Voilà!

Upvotes: 1

Related Questions