Quartz perform tasks on a timed basis and configure Web. xml

Source: Internet
Author: User

0. Nonsense:

Separation of four months, after graduation, June 13 back to work, has been the idea of their own learning things to record and share to make, has not moved, today began the first article, this is the project needs to do a scheduled task, temporary learning, quartz function is very powerful and convenient to use it. The demo here is only implemented once a day, other functions can be based on further study, haha sleep, tomorrow continue.

One, MAVEN relies on:

<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>

Second, Doem:

Timingtaskschedule need to implement Servletcontextlistener interface, boot class when starting project after listening

 PackageCom.thinkgem.jeesite.modules.sys.listener;Importjavax.servlet.ServletContextEvent;ImportJavax.servlet.ServletContextListener; Public classTimingtaskscheduleImplementsservletcontextlistener{//The event is executed when the server starts@Override Public voidcontextinitialized (servletcontextevent arg0) {Try{quartzload.run (); } Catch(Exception e) {e.printstacktrace (); }    }    //The event is executed when the server is stopped@Override Public voidcontextdestroyed (servletcontextevent arg0) {Try{quartzload.stop (); } Catch(Exception e) {e.printstacktrace (); }    }}

Here's 0 0 0? * * indicates one 00:00:00 per day

From left to right, respectively, the anniversary of the second time

? Show no concern * indicates that it can be ignored every year

 PackageCom.thinkgem.jeesite.modules.sys.listener;ImportOrg.quartz.CronScheduleBuilder;ImportOrg.quartz.CronTrigger;ImportOrg.quartz.Job;ImportOrg.quartz.JobBuilder;ImportOrg.quartz.JobDetail;ImportOrg.quartz.Scheduler;Importorg.quartz.SchedulerFactory;ImportOrg.quartz.TriggerBuilder;Importorg.quartz.impl.StdSchedulerFactory;ImportCom.thinkgem.jeesite.modules.sys.listener.job; Public classQuartzload {Private StaticScheduler sched;  Public Static voidRun ()throwsException {System.out.println ("Timed task Start"); Jobdetail Jobdetail= Jobbuilder.newjob (class<?extendsjob>) job.class). Withidentity ("Myjob", "group1"). Build ();Crontrigger Trigger=(Crontrigger) Triggerbuilder.newtrigger (). Withidentity ("Trigger", "group1"). Withschedule (Cronschedulebuilder.cronschedule ("0 0 0?" * *") . Build (); Schedulerfactory sfact=Newstdschedulerfactory (); Scheduler Schedule=Sfact.getscheduler ();        Schedule.start ();    Schedule.schedulejob (Jobdetail, Trigger); }    //Stop     Public Static voidStop ()throwsexception{Sched.shutdown (); }  }

The job is your own business process.

 PackageCom.thinkgem.jeesite.modules.sys.listener;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importorg.apache.shiro.authz.annotation.RequiresPermissions;ImportOrg.quartz.Job;ImportOrg.quartz.JobExecutionContext;Importorg.quartz.JobExecutionException; Public classJobImplementsjob{ Public voidExecute (jobexecutioncontext arg0)throwsjobexecutionexception {Date Date=NewDate (); SimpleDateFormat SF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); System.out.println ("Time:" +Sf.format (date)); System.out.println ("Hello");            }        }    }}

Third, Web. XML monitoring:

Com.thinkgem.jeesite.modules.sys.listener.TimingTaskSchedule

< Listener >    < Listener-class >com.thinkgem.jeesite.modules.sys.listener.TimingTaskSchedule</listener-class >  </ Listener >

Quartz perform tasks on a timed basis and configure Web. xml

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.