Timer: Timer and Quartz application example

Source: Internet
Author: User

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 ());
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.