Environment: Introducing Package Quartz-1.6.1.jar
Timer Application Example:
Class Timertest.java:
Import Java.util.Date;
Import Java.util.Timer;
public class timertest{
public static void Main (string[] args) {
Timer Testtimer = new timer ();
Testtimer.schedule (New Mytimertask (), New Date (), 5*1000);
}
}
Class Mytimertask.java:
Import Java.util.Date;
Import Java.util.TimerTask;
Class Mytimertask extends timertask{
public void Run () {
System.out.println ("= =" +new Date ());
}
}
Quartz Application Example:
Class Main.java:
Import Java.util.logging.Level;
Import Java.util.logging.Logger;
Import org.quartz.SchedulerException;
public class Main {
public static void Main (string[] args) {
try {
New Excutetask (). Start ();
catch (Schedulerexception ex) {
Logger.getlogger (Main.class.getName ()). log (Level.severe, NULL, ex);
}
}
}
Class Excutetask.java:
Import org.quartz.CronExpression;
Import Org.quartz.CronTrigger;
Import Org.quartz.JobDetail;
Import Org.quartz.Scheduler;
Import org.quartz.SchedulerException;
Import Org.quartz.SchedulerFactory;
Import Org.quartz.impl.StdSchedulerFactory;
public class Excutetask {
public void Start () throws Schedulerexception
{
Schedulerfactory schedulerfactory = new Stdschedulerfactory ();
Scheduler Scheduler = Schedulerfactory.getscheduler ();
Jobdetail jobdetail = new Jobdetail ("JobName", "Jobgroupname", Excutejob1.class);
Crontrigger Crontrigger = new Crontrigger ("Triggername", "triggergroupname");
Jobdetail jobDetail2 = new Jobdetail ("JobName2", "Jobgroupname", Excutejob2.class);
Crontrigger cronTrigger2 = new Crontrigger ("TriggerName2", "triggergroupname");
Try
{
Executed every 2 minutes
Cronexpression cexp = new Cronexpression ("0 */2 * * *"? *");
Crontrigger.setcronexpression (CEXP);
Performed every 1 hours
Cronexpression CEXP2 = new Cronexpression ("0 0 */1 * *? *");
Crontrigger2.setcronexpression (CEXP2);
catch (Exception e)
{
E.printstacktrace ();
}
Planning tasks
Scheduler.schedulejob (Jobdetail, Crontrigger);
Scheduler.schedulejob (JOBDETAIL2, CronTrigger2);
Start a task
Scheduler.start ();
}
public void Close () throws Schedulerexception
{
Start a task
Scheduler
}
}
Class Excutejob1.java:
Import Java.util.Date;
Import Org.quartz.Job;
Import Org.quartz.JobExecutionContext;
Import org.quartz.JobExecutionException;
public class ExcuteJob1 implements job{
public void execute (jobexecutioncontext arg0) throws Jobexecutionexception
{
System.out.println ("==job1==" +new Date ());
}
}
Class Excutejob2.java:
Import Java.util.Date;
Import Org.quartz.Job;
Import Org.quartz.JobExecutionContext;
Import org.quartz.JobExecutionException;
public class EXCUTEJOB2 implements job{
public void execute (jobexecutioncontext arg0) throws Jobexecutionexception
{
System.out.println ("==job2==" +new Date ());
}
}