Quartz periodically executes tasks and configures web. xml and quartzweb. xml
Zero nonsense:
I have been leaving my company for more than four months. After graduation, I went back to work in June 13. I have been thinking about sorting out and sharing my learning records. I have never moved on. I started my first article today, this is a scheduled task for today's project. It is a temporary task, and the quartz function is very powerful and easy to use. The demo here only implements regular execution every day, other functions can be further studied on this basis.
1. maven dependency:
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.3</version> </dependency>
Ii. Doem:
TimingTaskSchedule needs to implement the ServletContextListener interface, which is the startup class when the project is started after listening
Package com. thinkgem. jeesite. modules. sys. listener; import javax. servlet. servletContextEvent; import javax. servlet. servletContextListener; public class TimingTaskSchedule implements ServletContextListener {// This event is executed when the server is started @ Override public void contextInitialized (ServletContextEvent arg0) {try {QuartzLoad. run ();} catch (Exception e) {e. printStackTrace () ;}// execute this event when the server is stopped @ Override public void contextDestroyed (ServletContextEvent arg0) {try {QuartzLoad. stop ();} catch (Exception e) {e. printStackTrace ();}}}
0 0 here? ** Execution is performed at 00:00:00 every day.
From left to right, it indicates second minute hour day month week Year
? Indicates that you do not care * indicates that you can ignore or not write data every year.
Package com. thinkgem. jeesite. modules. sys. listener; import org. quartz. cronScheduleBuilder; import org. quartz. cronTrigger; import org. quartz. job; import org. quartz. jobBuilder; import org. quartz. jobDetail; import org. quartz. scheduler; import org. quartz. schedulerFactory; import org. quartz. triggerBuilder; import org. quartz. impl. stdSchedulerFactory; import com. thinkgem. jeesite. modules. sys. listener. job; public class QuartzLoad {private static Scheduler sched; public static void run () throws Exception {System. out. println ("scheduled task start"); JobDetail jobDetail = JobBuilder. newJob (Class <? Extends Job>) job. class ). withIdentity ("myjob", "group1 "). build (); CronTrigger trigger = (CronTrigger) TriggerBuilder. newTrigger (). withIdentity ("trigger", "group1 "). withSchedule (CronScheduleBuilder. cronSchedule ("0 0 0? **")). Build (); SchedulerFactory sfact = new StdSchedulerFactory (); Scheduler schedule = sfact. getScheduler (); schedule. start (); schedule. scheduleJob (jobDetail, trigger);} // stop public static void stop () throws Exception {sched. shutdown ();}}
Job is your own business processing
package com.thinkgem.jeesite.modules.sys.listener;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.shiro.authz.annotation.RequiresPermissions;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;public class job implements Job{public void execute(JobExecutionContext arg0) throws JobExecutionException { Date date=new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Time:"+sf.format(date)); System.out.println("Hello"); } } }}
Iii. web. xml listening:
Com. thinkgem. jeesite. modules. sys. listener. TimingTaskSchedule
<listener> <listener-class>com.thinkgem.jeesite.modules.sys.listener.TimingTaskSchedule</listener-class> </listener>