Using Quratz timed Calls

Source: Internet
Author: User

Recently in the work need to call the function of the time, timed calls in the Web application or quite a lot of, such as the schedule reminder, every night to remind what class to go, or just like I need to complete the function, timed one database data import into another database.

And Quratz is an open-source job scheduling framework written entirely by Java. Although the quartz framework incorporates many additional features, in its simplest form, you will find that it is simply unbearable to use! It is really convenient and practical to use.

Nonsense not much to say, directly on!

First you need to quratz the relevant jar package, I am using Quartz-2.2.1.jar.

Next, you need a job execution class and a task scheduling class.

Task scheduling class, in which a corn expression is developed, which is implemented according to your requirements, such as running at a certain time of day.

Package Com.gisquest.djgx.modules.services;import Java.util.properties;import Org.quartz.CronScheduleBuilder;  Import Org.quartz.crontrigger;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 Org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.Component; Import Org.springframework.transaction.annotation.transactional;import Com.gisquest.djgx.modules.sys.entity.wsdefine;import com.gisquest.djgx.modules.sys.utils.dictutils;@    Component@transactional (readOnly = true) public class Invokeservice {schedulerfactory SF = null; Scheduler sched = null;//Click the Start button to invoke the method public void Scheduler (wsdefine wsdefine,string basepath,string id) {String deftime = Wsdefine.getdeftime (); String month = "*"; String year = "?"; String schedname = "schedu" +ID; String jobName = "Job" +ID; String groupName = "group" +ID; StrinG triggername = "trigger" +ID; String schedstring= ""; try {properties props = new Properties ();p rops.put ("Org.quartz.scheduler.instanceName", Schedname);p rops.put ("Org.quartz.threadPool.threadCount", "N"), SF = new stdschedulerfactory (props);  Sf.getscheduler (schedname)! = null) {sched = Sf.getscheduler (schedname);  Initialize Scheduler}else{sched = Sf.getscheduler (); Initialize Scheduler}jobdetail job = Jobbuilder.newjob (Quartzjob.class). Withidentity (JobName, GroupName). Build (); Set up the job, the specific operation in the Quartzjob class Job.getjobdatamap (). Put ("Wsurl", Basepath+wsdefine.geturl ());// Add the familiar Job.getjobdatamap () that the calling function requires. put ("Jobid", Wsdefine.getjobid ()), Job.getjobdatamap (). Put ("Wsname", Wsdefine.getname ()); string[] str = deftime.split (":"); schedstring = str[2] + "" + str[1] + "+str[0]+" "+" * "+" "+ month +" "+ year; System.out.println (schedstring); System.out.println (sched); Crontrigger trigger = (Crontrigger) Triggerbuilder.newtrigger (). Withidentity (Triggername, GroupName). Withschedule ( Cronschedulebuilder.cronschedule (schedString). build (); Set trigger Sched.schedulejob (Job, trigger); Set the scheduling job Sched.start (); Start dispatch task, execute job} catch (Exception ex) {Ex.printstacktrace ()}} Click the Stop button to invoke the method public void StopService (string id) {String schedname = "Schedu" +id;try {if (Sf.getscheduler (schedname)! = Nu ll) {Sf.getscheduler (schedname). Shutdown ();}} catch (Exception ex) {ex.printstacktrace ();}}}
There's only one way to use Crontrigger, and there's a much simpler way.Simpletrigger, there is no crontrigger way to be more precise. There is a configuration format for the Crontrigger expression below this article.

The job execution class, which needs to implement the Quratz job interface, implements the Execute method, which is called by the function that you want to call periodically in the Execute method.

<span style= "FONT-SIZE:18PX;" >package Com.gisquest.djgx.modules.services;import Org.quartz.job;import Org.quartz.jobdatamap;import Org.quartz.jobexecutioncontext;import Org.quartz.jobexecutionexception;import Org.slf4j.Logger;import Org.slf4j.loggerfactory;import Org.springframework.stereotype.component;import org.springframework.transaction.annotation.Transactional; @Component @transactional (readOnly = true) public class Quartzjob implements Job {private static final Logger log = Loggerfactory.getlogger (Quartzjob.class); @Overridepublic void execute (Jobexecutioncontext context) throws Jobexecutionexception {try {jobdatamap DataMap = Context.getjobdetail ( ). Getjobdatamap (); String Wsurl = datamap.getstring ("Wsurl"); String Jobid = String.valueof (Datamap.getint ("Jobid"));//The property that was passed in the Task Scheduler before, String wsname = Datamap.getstring ("Wsname"); System.out.println (Wsurl+jobid+wsname); Wsservice wsservice = new Wsservice (); System.out.println (Wsservice); Wsservice.callwebservice (Null,wsurl, "Pushdata", jobid,wsname);//The method of the timed call} catch (Exception e) {e.printstacktrace (); Log.error (E.getlocalizedmessage ());}}} </span>

Crontrigger Configuration format:
Format: [seconds] [min] [hour] [day] [month] [week] [year]

Serial number Description is required Allowed values to be filled in Allowed wildcard characters
1 Seconds Is 0-59 , - * /
2 Score of Is 0-59 , - * /
3 Hours Is 0-23 , - * /
4 Day Is 1-31 , - * ? /L W
5 Month Is 1-12 or Jan-dec , - * /
6 Week Is 1-7 or Sun-sat , - * ? L
7 Years Whether Empty or 1970-2099 , - * /

Common examples:

0 0 12 * *? 12-point trigger per day
0 15 10? * * Triggered 10:15 daily
0 15 10 * *? Triggered 10:15 daily
0 15 10 * *? * Triggered 10:15 daily
0 15 10 * *? 2005 2005 Daily 10:15 Trigger
0 * 14 * *? Every afternoon from 2 to 2:59 per minute.
0 0/5 14 * *? Every afternoon from 2 to 2:59 (the whole point starts, every 5 minutes trigger)
0 0/5 14,18 * *? Every afternoon from 2 to 2:59 (the whole point starts, every 5 minutes trigger) every afternoon from 18 to 18:59 (the hour starts, every 5 minutes trigger)
0 0-5 14 * *? Every afternoon from 2 to 2:05 per minute.
0 10,44 14? 3 WED March minutes 2:10 and 2:44 every Wednesday pm
0 15 10? * Mon-fri Triggered 10:15 every morning from Monday to Friday
0 15 10 15 *? 15th 10:15 A.M. per month, trigger
0 L *? 10:15 trigger on the last day of the month
0 15 10? * 6L Friday 10:15 in the last week of each month
0 15 10? * 6L 2002-2005 Triggered from 2002 to 2005, the last week of Friday, 10:15
0 15 10? * 6#3 Triggered in Friday of the third week of the month
0 0 12 1/5 *? The first noon of each month starts every 5 days
0 11 11 11 11? Every November 11 11:11 trigger (Singles Day)



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Using Quratz timed Calls

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.