• September 4, 2014
  • Quartz Scheduler
  • Comments Off on Quartz multiple jobs example

Quartz multiple jobs example

Quartz scheduler:

Quartz is an open source job scheduling framework that can be integrated into a wide variety of Java applications to run task on a predefine date and time.

Example Explanation:

1. Create multiple jobs by implementing Job interface.
2. Perform your business logic in the execute method.
3. Create a class for executing multiple quartz jobs.
4. Get JobDetail object from JobBuilder and set job detail like name and job class for each job.
5. Create Trigger object from TriggerBuilder and set the scheduler details for each job.
6. Get Scheduler object from StdSchedulerFactory.
7. Use Scheduler object to connect quartz job and quartz trigger together and execute the job.

Example:

CronTriggerTest.java

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
 
/**
 * This class is used for executing multiple 
 * quartz job using CronTrigger(Quartz 2.1.5).
 * @author javawithease
 */
public class CronTriggerTest {
	public static void main(String args[]){
    	try{
    		//Set job details.
    		JobDetail job1 = JobBuilder.newJob(Job1.class)
    			.withIdentity("job1", "group1").build();
 
    		JobDetail job2 = JobBuilder.newJob(Job2.class)
    			.withIdentity("job2", "group1").build();
 
    		JobDetail job3 = JobBuilder.newJob(Job3.class)
    			.withIdentity("job3", "group1").build();
 
        	//Set the scheduler timings.
    		Trigger trigger1 = TriggerBuilder.newTrigger()
    			.withIdentity("cronTrigger1", "group1")
    			.withSchedule(CronScheduleBuilder
    			  .cronSchedule("0/10 * * * * ?")).build();
 
    		Trigger trigger2 = TriggerBuilder.newTrigger()
    			.withIdentity("cronTrigger2", "group1")
    			.withSchedule(CronScheduleBuilder
    			  .cronSchedule("0/10 * * * * ?")).build();
 
    		Trigger trigger3 = TriggerBuilder.newTrigger()
    			.withIdentity("cronTrigger3", "group1")
    			.withSchedule(CronScheduleBuilder
    			  .cronSchedule("0/10 * * * * ?")).build();
 
        	//Execute the job.
    		Scheduler scheduler = 
    			new StdSchedulerFactory().getScheduler();
        	scheduler.start();
        	scheduler.scheduleJob(job1, trigger1);
        	scheduler.scheduleJob(job2, trigger2);
        	scheduler.scheduleJob(job3, trigger3);
    	}catch(Exception e){
    		e.printStackTrace();
    	}    	
	}
}

Job1.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
/**
 * This class defines a quartz job.
 * @author javawithease
 */
public class Job1 implements Job{
	public void execute(JobExecutionContext context) 
			throws JobExecutionException {	 
		System.out.println("Job1 is running.");		 
	}
}

Job2.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
/**
 * This class defines a quartz job.
 * @author javawithease
 */
public class Job2 implements Job{
	public void execute(JobExecutionContext context) 
			throws JobExecutionException {	 
		System.out.println("Job2 is running.");		 
	}
}

CronTriggerTest.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
/**
 * This class defines a quartz job.
 * @author javawithease
 */
public class Job3 implements Job{
	public void execute(JobExecutionContext context) 
			throws JobExecutionException {	 
		System.out.println("Job3 is running.");		 
	}
}

Output:

Job1 is running.
Job2 is running.
Job3 is running.
Job1 is running.
Job2 is running.
Job3 is running.
...

Download this example.
 
Next Topic: Quartz listing all jobs example.
Previous Topic: Quartz 2 JobListener example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 


Comments are closed.