Quartz Scheduling in Java is an entry-level application of some timed tasks (job)

Source: Internet
Author: User
Tags bind copy

Quartz implementation: http://quartz-scheduler.org/to download the corresponding jar package in Maven, you can copy the dependent copy directly to the POM.

Specific rules can be queried for quartz documents

The following is a very detailed example:

1. First write the tasks that need to be performed to execute and implement the job

Package job;    
       
Import Java.util.Date;    
Import Org.quartz.Job;    
Import Org.quartz.JobExecutionContext;    
Import org.quartz.JobExecutionException; /** * Tasks to be performed * @author CLQ * * */public class Myjob implements JOB {@Override//Take action to be performed , written in the Execute method public void execute (jobexecutioncontext arg0) throws Jobexecutionexception {SYSTEM.OUT.PR    
   Intln ("Test Quartz" +new Date ());  
} 2. The class package job that the calling task needs to implement;  
Import Org.quartz.Scheduler;  
Import Org.quartz.SchedulerFactory;  
Import Org.quartz.impl.JobDetailImpl;  
Import Org.quartz.impl.StdSchedulerFactory;  
       
Import Org.quartz.impl.triggers.CronTriggerImpl; public class CronTriggerTest2 {public static void main (string[] args) {//Gets a scheduler through Schedulerfactory    
    Chedulerfactory schedulerfactory=new stdschedulerfactory ();    
       Scheduler Scheduler=null; try{//Get Scheduler Instance Scheduler=schedulerfactory.getschedulER (); Indicates the name of the job, the name of the group, and the bind job class to create the Jobdetail instance, the bind job implementation class Jobdetailimpl Jobdetail=new Jobdetailimpl ("Job1", "Jgroup  
      
       1 ", myjob.class);  
      
       Define the dispatch departure rule to perform a crontriggerimpl corntrigger=new crontriggerimpl ("JobName", "Jobgroup") every 2 seconds; The * number in parentheses is based on this for seconds, minutes, days, weeks, months, years//inside there are specific rules of use, very flexible corntrigger.setcronexpression (0/2 * * * *?  
      
       *");  
       Registration of jobs and triggers in task scheduling scheduler.schedulejob (Jobdetail, Corntrigger);    
       Start dispatch Scheduler.start ();  
       }catch (Exception e) {e.printstacktrace (); }  
}  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

CronTriggerTest2 This class because many of the methods inside have been abandoned, this is a newly written implementation class, and its function is identical to the above

Package job;  
Import Org.quartz.JobBuilder;    
Import Org.quartz.JobDetail;    
Import Org.quartz.Scheduler;    
Import Org.quartz.SchedulerFactory;  
Import Org.quartz.SimpleScheduleBuilder;  
Import Org.quartz.Trigger;  
Import Org.quartz.TriggerBuilder;  
Import Org.quartz.TriggerKey;    
Import Org.quartz.impl.StdSchedulerFactory;    
         
      /** * Calling the task's class * @author CLQ * * */public class Crontriggertest {public static void main (string[] args) {    
      Schedulerfactory schedulerfactory=new stdschedulerfactory ();    
      Scheduler Scheduler=null;    
          try{Scheduler=schedulerfactory.getscheduler ();  
      
          Jobdetail job = Jobbuilder.newjob (Myjob.class). Withidentity ("Myjob"). Build (); Trigger Trigger = Triggerbuilder.newtrigger (). Withidentity (Triggerkey.triggerkey ("Mytrigger", "Mytriggergro  
          Up ")). Withschedule (Simpleschedulebuilder.simpleschedule ()    . Withintervalinmilliseconds. RepeatForever ()). Startnow (). build ();    
Scheduler.schedulejob (Job, trigger);    
      Start dispatch Scheduler.start ();    
      }catch (Exception e) {e.printstacktrace (); }    
}    
}

In a sense to understand a job refers to a single thread, this in the development of the use of more common, we can seriously learn quartz use.

After performing the above method, you will find that the program will be very stable to execute the myjob in the way you need to do, every 2s execution

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.