CodesJava

Easy learning with example program codes

Quartz 1.6 hello world example using CronTrigger


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 a job by implementing Job interface.
2. Perform your business logic in the execute method.
3. Create a class for executing quartz job.
4. Create a JobDetail object and set job detail like name and job class.
5. Create CronTrigger object and set the scheduler timing and other details.
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.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
 
/**
 * This class is used for executing quartz 
 * job using CronTrigger(Quartz 1.6.3).
 * @author codesjava
 */
public class CronTriggerTest {
	public static void main(String args[]){
    	try{
    		//Set job details.
    		JobDetail job = new JobDetail();
        	job.setName("helloJob");
        	job.setJobClass(HelloJob.class);
 
        	//Set the scheduler timings.
        	CronTrigger trigger = new CronTrigger();
        	trigger.setName("cronTrigger");
        	//Execute the job after every 10 seconds.
        	trigger.setCronExpression("0/10 * * * * ?");
 
        	//Execute the job.
    		Scheduler scheduler = 
    				new StdSchedulerFactory().getScheduler();
        	scheduler.start();
        	scheduler.scheduleJob(job, trigger);
    	}catch(Exception e){
    		e.printStackTrace();
    	}    	
	}
}

HelloJob.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
/**
 * This class defines a quartz job.
 * @author codesjava
 */
public class HelloJob implements Job{
	public void execute(JobExecutionContext context)
			throws JobExecutionException {	 
		System.out.println("Hello World.");		 
	}
}

Output:

Hello World.
Hello World.
Hello World.
Hello World.
Hello World.
...

Download this example.
 
Next Topic: Quartz 2.1.5 example using SimpleTrigger with example.
Previous Topic: Quartz 1.6 example using SimpleTrigger with 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.

 

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki