Project UI (7) Integration of Quartz with javaWeb for Dynamic Scheduling and persistence

Source: Internet
Author: User

Project UI (7) Integration of Quartz with javaWeb for Dynamic Scheduling and persistence

Original address: http://www.cnblogs.com/Alandre/ (sand tile pulp carpenter), need to reprint, keep the next!

Di Zigui sage training first filial piety I would like to believe In The wide love of The crowd and his benevolence has a letter Written In The Font

Required: WEB-INF/lib/quartz-2.2.1.jar

Basic steps:

  • Web. xml registration listener ScheduleStartListener
  • Listener class sedion. jeffli. wmuui. listener. ScheduleStartListener implementation
  • Test Case Step 1: Job interface implementation class JobTest
  • Step 2 of the test case: QuartzTest

 

Web. xml registration listener ScheduleStartListener

Register the quartz listener to monitor whether the project is started or restarted. Ensure that all tasks are rescheduled to the task scheduling when the project is started or restarted.

Add a Listener to web. xml:

<! -- Quartz listener --> <listener-class> sedion. jeffli. wmuui. listener. ScheduleStartListener </listener-class> </listener>

 

Listener class sedion. jeffli. wmuui. listener. ScheduleStartListener implementation

Listener class is mainly used to implement various recovery tasks, re-Restore all triggers in the triggerGroups group, and re-set job execution according to the new trigger. by the way, this exception is customized (you do not need to delete it): sedion. jeffli. wmuui. exception. quartzException;

Package sedion. jeffli. wmuui. listener; import java. util. list; import javax. servlet. servletContextEvent; import javax. servlet. servletContextListener; import org. quartz. scheduler; import org. quartz. schedulerFactory; import org. quartz. trigger; import org. quartz. triggerKey; import org. quartz. impl. stdSchedulerFactory; import sedion. jeffli. wmuui. exception. quartzException; public class ScheduleStartListener implem Ents ServletContextListener {public void contextDestroyed (ServletContextEvent sce) {} public void contextInitialized (ServletContextEvent sce) {try {recovery ();} catch (Exception e) {throw new QuartzException ("ScheduleStartListener contextInitialized ERROR !! ", E) ;}} public void recovery () {Scheduler scheduler = null; try {SchedulerFactory schedulerFactory = new StdSchedulerFactory (); scheduler = schedulerFactory. getScheduler (); // you can use SchedulerFactory to create a List of schedulinstances <String> triggerGroups = schedups. getTriggerGroupNames (); // obtain all trigger groups in the scheduler System. out. println ("all trigger group size ():" + triggerGroups. size (); if (triggerGroups! = Null & triggerGroups. size ()! = 0) // restore all triggers in the triggerGroups Group {for (int I = 0; I <triggerGroups. size (); I ++) {TriggerKey triggerKey = TriggerKey. triggerKey (triggerGroups. get (I), triggerGroups. get (I); System. out. println ("triggerKey:" + triggerKey); Trigger tg = scheduler. getTrigger (triggerKey); // get the trigger System. out. println (triggerKey + "-> execution time:" + tg. getNextFireTime (); scheduler. rescheduleJob (triggerKey, tg); // reset according to the new trigger Job execution} scheduler. start ();} catch (Exception e) {throw new QuartzException ("ScheduleStartListener recovery () Error! ", E );}}}

 

Test Case Step 1: Job interface implementation class JobTest

As the name suggests, it is used to customize tasks and implement methods. you can write in it anything you want to do on that point (operate the database, display at the front end, etc ). write what you want to write in the following place: System. out. println ("add specific operations to add tasks ");. by the way, this exception is customized (you do not need to delete it ):

Package test. quartz; import org. quartz. job; import org. quartz. jobDataMap; import org. quartz. jobExecutionContext; import org. quartz. jobExecutionException; import org. quartz. jobKey; import org. quartz. scheduler; import org. quartz. schedulerException; import org. quartz. schedulerFactory; import org. quartz. triggerKey; import org. quartz. impl. stdSchedulerFactory; import sedion. jeffli. wmuui. exception. quartzException; p Ublic class JobTest implements Job {public JobTest () {} public void execute (JobExecutionContext context) throws JobExecutionException {JobDataMap data = context. getJobDetail (). getJobDataMap (); System. out. println ("data. testId: "+ data. getInt ("testId"); // you can delete try {System. out. println ("add specific operations to add tasks");} catch (Exception e) {throw new QuartzException ("JobTest execute () ERROR !! ", E) ;}} public static void removeJob (JobKey jobKey, TriggerKey tiKey) throws SchedulerException {SchedulerFactory sf = new StdSchedulerFactory (); sched1_sched = sf. getScheduler (); sched. pauseTrigger (tiKey); // stop the trigger sched. unscheduleJob (tiKey); // remove the trigger sched. deleteJob (jobKey); // Delete task }}

 

Step 2 of the test case: QuartzTest

As the name suggests, it is used for implementation and test. you can use SchedulerFactory to create a Scheduler instance. The group named by the trigger on the cluster node instance is only used to distinguish (logging) from where the job is scheduled and re-executed, if it is being scheduled.

Package test. quartz; import static org. quartz. jobBuilder. newJob; import static org. quartz. triggerBuilder. newTrigger; import java. util. date; import org. quartz. jobDetail; import org. quartz. scheduler; import org. quartz. schedulerFactory; import org. quartz. simpleTrigger; import org. quartz. impl. stdSchedulerFactory; public class QuartzTest {public void run (String date, int id) throws Exception {SchedulerFactory schedulerFactory = new StdSchedulerFactory (); Scheduler scheduler = schedulerFactory. getScheduler (); // you can create a schedulinstance through SchedulerFactory // set job details JobDetail job = newJob (JobTest. class ). withIdentity ("job _" + id, "test" + id) // (String name, String group) name the group of the trigger on the cluster node instance to distinguish (logging) where can I determine where to schedule and re-execute this job? If it is being scheduled .... requestRecovery (). build (); job. getJobDataMap (). put ("testId", id); // you can specify the storage parameter (you do not need to delete it) Date startDate = FormatDate. stringToDateAll (date); // convert Date to String // set the trigger SimpleTrigger trigger = (SimpleTrigger) newTrigger (). withIdentity ("overdue" + id, "overdue" + id) // withIdentity ("trigger", "group "). startAt (startDate ). build (); scheduler. scheduleJob (job, trigger); schedjob. start (); System. out. println ("------- Start Scheduler --------------");} public static void main (String [] args) throws Exception {QuartzTest quartzOverdue = new QuartzTest (); quartzOverdue. run ("00:30:00", 666); // 666, lucky number }}

 

Here, the project tomcat starts. Here, my host time is:

 

Then we run:

Public static void main (String [] args) throws Exception {QuartzTest quartzOverdue = new QuartzTest (); quartzOverdue. run ("00:30:00", 666); // 666, lucky number}

 

View the console:

Output first

------- Start Scheduler ----------------

Then it's time

Add specific operations to the task

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.