Md Noorshid
Md Noorshid

Reputation: 107

Spring Boot Job Scheduler

I am trying to define multiple jobs in Spring Boot Batch Processing. I can schedule the jobs very easily , but getting problem when i need just schedule some particular jobs only. Here is my approach, defining schedule class where i am scheduling the job. Can I schedule particular jobs instead of scheduling all jobs together?? Thanks

import org.apache.log4j.Logger;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class JobScheduler {
static Logger logger=Logger.getLogger(JobScheduler.class);
@Autowired
JobLauncher jobLauncher;
@Autowired
private Job job;
   
@Scheduled(cron="10 * * * *  *") //Scheduling job at the interval of 10 seconds
public void scheduleJob(){
	JobParameters jobParameters=new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters();
	try {
		String jobName=job.getName();
		logger.info("JOB NAME===> "+jobName);
		JobExecution jobExecution=jobLauncher.run(job, jobParameters);
		logger.info("JOB'S STATUS===> "+jobExecution.getStatus());
		
	} catch (JobExecutionAlreadyRunningException e) {
	} catch (JobRestartException e) {
	} catch (JobInstanceAlreadyCompleteException e) {
	} catch (JobParametersInvalidException e) {
	}
}

}

Upvotes: 0

Views: 1734

Answers (2)

Bartek
Bartek

Reputation: 136

Look at this simple example:

https://spring.io/guides/gs/scheduling-tasks/

All you need to do is Enable Scheduling with @SpringBootApplication @EnableScheduling annotations.

Then you can use @Scheduled annotation for specific method in any @Component you want to create scheduled task and set different schedule time for every task. As many as you want.

You do not need to create JobScheduler, JobLuncher or Job.

Upvotes: 3

Harshit Gupta
Harshit Gupta

Reputation: 126

Yes you can do that. Make a separate method/class and implement it using switch-case (using java). Have separate method calls for every job(I am assuming that you want to schedule different-different jobs). Sample code would look something like this-

switch(job) {
    case 'job1' {
    executejob1(job1, jobParameters);
    break;
    }
    case 'job2' {
    executejob1(job2, jobParameters);
    break;
    }
    case 'job3' {
    executejob1(job3, jobParameters);
    break;
    }
    case 'job4' {
    executejob1(job4, jobParameters);
    break;
    }
}

Now you just need to call this with the 'job' which you want to schedule along with its job parameter. Hope this will be of some help.

Upvotes: 2

Related Questions