Dynamic change of quartz scheduling time

Source: Internet
Author: User
http://blog.csdn.net/cuker919/article/details/6425032     The first is the direct use of the Reschedulejob method.
The second is that I delete the job reload first.
In recent days, the timer in the project often occurs abnormally, such as:

1, to modify the Linux system time, the timing of the task is all hung off.

2, in the page dynamic modification task time, often do not execute.

Below is some netizen's information, the temporary collection puts, has the time to verify and modifies own system the question.

recently met a friend raised a question, how to dynamically change the quartz scheduling job time. For example, the execution of every 10 minutes is performed every 5 minutes. The individual thinks that this kind of demand should be evaded by some way, or choose other technical framework, because dynamic change quartz schedule time completely loses the meaning that uses quartz. I'm using quartz to configure it based on spring, and the spring framework is not used in friends ' projects, which requires direct programming based on quartz. After work, I wrote an example:
The management class of quartz
Java code   public class Quartzmanage {      private static schedulerfactory SF =new STDSCHEDULERFAC Tory ();      private static String Job_group_name = "GROUP";      private static String trigger_group_name = "TRIGGER";        public staticvoid startjob (string jobName, Job Job , String time)               throws Schedulerexception, ParseException {          Scheduler sched = Sf.getscheduler ();    & nbsp;       jobdetail jobdetail = new Jobdetail ();           jobdetail.setname (jobName);          Jobdetail.setgroup (job_group_name);          Jobdetail.setjobclass ( Job.getclass ());            Crontrigger Trigger = new Crontrigger (JobName, trigger_group_name);          Trigger.setcronexpression (time);          sched.schedulejob (Jobdetail, Trigger);            if (!sched.isshutdown ()) {               Sched.start ();          }     }       /**      * Remove the current job from scheduler Modify Trigger      *      * @param jobdetail      * @param time & nbsp;    * @throws schedulerexception      * @throws parseexception       */      public staticvoid modifyjobtime (Jobdetail jobdetail, String time)                throws Schedulerexception, ParseeXception {          Scheduler sched = Sf.getscheduler ();           Trigger Trigger = Sched.gettrigger (Jobdetail.getname (),                   trigger_group_name);           if (trigger!= null) {               Crontrigger ct = (crontrigger) trigger;              //Removing job              of current process Sched.deletejob (Jobdetail.getname (), Jobdetail.getgroup ());              //Modify trigger              Ct.setcronexpression (time);              System.out.prinTLN ("Crontrigger getName" + ct.getjobname ());              /re-Dispatch jobdetail              Sched.schedulejob (Jobdetail, CT);         }     }   } 


Job tasks: Java code   public class jobtestimplements Job {      static int a =0;     &NB sp;  @Override       public void execute (jobexecutioncontext context)                throws Jobexecutionexception {           a = 1;          System.out.println ("Test +++++++++ +++++++++++++a= "+ a";            if (a = = 4) {     & nbsp;        try {                   Quartzmanage.modifyjobtime (Context.getjobdetail (),                            "0/10 * * * *");           &Nbsp;  } catch (Schedulerexception e) {                   e.printstacktrace ();               catch (parseexception e) {                   e.printstacktrace ();              }         }     }   } 


Start thread execution scheduling: Java code   public class Quartztest {      public staticvoid main (string[] args) th Rows schedulerexception,              parseexception {          /*          * This process is the main process, Triggers a quartz job dispatch so after the job is started, modifying the schedule in the process is ineffective          */           jobtest job = new Jobtest ();          Quartzmanage.startjob ("Ming", Job, "0/2 * * * *");      } } 



A lot of people's thinking is in the start of the main line agenda to change the scheduling time, simple analysis can be found, the main thread after the start will be scheduled to run the job, will not return to the main thread to load scheduling time, just played a trigger scheduling operation. Therefore, in order to dynamically modify the scheduling time, it is necessary to dynamically change the scheduling schedule of the current thread in the job task.

Test the code to start by printing a once every 2 seconds, when a is printed 4 times, followed every 10 seconds. Although the code test success, I still have doubts. Java code//removal of Job sched.deletejob (Jobdetail.getname (), Jobdetail.getgroup ()) of the current process;


Remove the current job task and add the new job according to the new scheduling time, although dynamic changes can be made, and there are other problems that are not known.
You are welcome to criticize the common test and verification.

http://blog.csdn.net/cuker919/article/details/6425032

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.