Today on the project, you need to do a timing task. If you learn temporarily, the function of Quartz is still very convenient to use. The DEMO here is only implemented once a day. Other functions can continue to study on this basis. Haha sleeps on this. Haha sleeps. , continue tomorrow.
I have always had the idea to record, organize and share what I have learned. I have never done it. Today I will start the first article. This is a scheduled task that needs to be done on today’s project. I learned it temporarily. The function of quartz is still It is very powerful and easy to use. The demo here only implements scheduled execution once a day. Other functions can be further studied on this basis. Haha, sleep and continue tomorrow.
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>
2. Doem:
TimingTaskSchedule needs to implement the ServletContextListener interface to start the project after listening Class
package com.thinkgem.jeesite.modules.sys.listener;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class TimingTaskSchedule implements ServletContextListener{// 服务器启动时执行该事件 @Overridepublic void contextInitialized(ServletContextEvent arg0) {try { QuartzLoad.run(); } catch (Exception e) { e.printStackTrace(); } }// 服务器停止时执行该事件 @Overridepublic void contextDestroyed(ServletContextEvent arg0) {try { QuartzLoad.stop(); } catch (Exception e) { e.printStackTrace(); } } }
The 0 0 0 ? * * means execution once every day at 00:00:00
represents seconds from left to right Time-sharing day-month anniversary
? Indicates that you don’t care* means you can ignore it and don’t write it 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("定时任务启动"); 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); }//停止 public static void stop() throws Exception{ sched.shutdown(); } }
Job is your own business processing
job execute(JobExecutionContext arg0) == SimpleDateFormat("yyyy-MM-dd HH:mm:ss""Time:"+"Hello"
三, web.xml listening:
com.thinkgem.jeesite.modules.sys.listener.TimingTaskSchedule
<listener> <listener-class>com.thinkgem.jeesite.modules.sys.listener.TimingTaskSchedule </listener-class> </listener>
The above is the detailed content of Example tutorial for configuring web.xml. For more information, please follow other related articles on the PHP Chinese website!