Yosefarr
Yosefarr

Reputation: 787

spring batch: How To call my RollBack implementation

I Have my own Spring Batch Job (MyTaskletJob) that implements my interface IJobBase that implements Tasklet,

What I need that my job to implement another method called rollBack

public interface IJobBase extends Tasklet{

 void rollBack();

}

public class MyTaskletJob IJobBase{

    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1){

            …}
    public void rollBack(){

            …}

}

The Spring configuration:

<bean id="jobTaskletStep1" class="com....job.MyTaskletJob ">
    <property name="message" value="Hello..." />
</bean>
<batch:job id="backgroundJob">
<batch:step id="step0">
        <batch:tasklet ref="jobTaskletStep1"/>
        </batch:step>
</batch:job>

This is the way I run my Job:

// create the job according to job name

Job job = (Job) applicationContext.getBean(“backgroundJob”);

// run the job – the jobLauncher will run the MyTaskletJob *execute* method

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

// check the ExitStatus

If(myJobExecution.getExitStatus().equals(ExitStatus.FAILED)){

// Need to run rollback() method on MyTaskletJob

My question is how to invoke the method rollback() method when JobExecution FAILED

Upvotes: 1

Views: 983

Answers (1)

Michael Minella
Michael Minella

Reputation: 21453

The easy way would be to use a JobExecutionListener#afterJob(JobExecution). You can inject the reference to your jobTaskletStep1 into the listener. From there you can check the status set in the JobExecution and call the method as needed.

Upvotes: 0

Related Questions