Search code examples
spring-batchjobsrestart

how to restart spring batch failed job with old job paramters


I am passing time in my job parameters .After running my job , i can see in console my job started with time parameters :-

Job: [FlowJob: [name=DB2-SQL-LOAD56]] launched with the following parameters: [{Time=1590853755581}]

I am wondering how can i restart the job if my job fails because everytime when my job fails , it runs a new instance since it picks up the current time (passed as job parameter)

How do i restart my failed job with old time parameter(mentioned above) ?

Do i need to add logic to accept time parameter in my code ? if yes , how do I handle that ?

I have attached my code. I would really appreciate if someone can help me what am i missing here in order to restart my failed job.

@SpringBootApplication
public class Db2MySqlLoadApplication implements CommandLineRunner {

@Autowired
private JobLauncher jobLauncher;

@Autowired
private Job job;


public static void main(String args[]) {
    SpringApplication.run(Db2MySqlLoadApplication.class, args);
}


@Override
public void run(String... args) throws Exception {


    JobParameters params = new JobParametersBuilder()
                    .addLong("Time" ,System.currentTimeMillis())
            .toJobParameters();
    jobLauncher.run(job, params);
}
}

Solution

  • Since you use Spring Boot, you don't really need to implement CommandLineRunner. Spring Boot already provides the JobLauncherCommandLineRunner that does that for you. What you can do is remove this run method (note that the args parameter is not used in your case) and launch your job from the command line with something like:

    java -jar myjob.jar Time=$(date +%s)
    

    This example uses the date +%s command on unix/linux which gives the current date as a timestamp since the Unix epoch, you would need to look for an equivalent if you are on MS windows.

    With that, if you want to restart a specific job instance with a previous Time parameter, you can pass the previous value as follows:

    java -jar myjob.jar Time=1590853755581