The Trigger__quartz of Quartz

Source: Internet
Author: User

Let's introduce the common properties of trigger:

Jobkey marks the ID of the job that should be invoked when the trigger is triggered. StartTime represents the time when the trigger schedule should be in effect for the first time. Value is a Java.util.Date object.
Endtime represents an event that the trigger schedule no longer takes effect. Priority represents the trigger priority of the trigger, and when resources are not available there is a principle that the trigger before the test is triggered and then the priority for the same time is triggered first, and the default priority is 5.
Misfire Instruction misfire occurs because a persistent trigger misses its triggering event, possibly because the scheduler is shutting down or because there are not enough threads in the quartz thread pool to execute the job. Here's a description of Quartz's Calendar interface, not Java.util.Calendar.

Package Org.quartz;

Public interface Calendar {public

  boolean istimeincluded (long TimeStamp);

  Public long Getnextincludedtime (long timeStamp);

It's easy to see what Calendar does from the interface definition:

Quartz Calendar objects can be associated with trigger and stored in the scheduler. The calendar is used to exclude the time that some trigger should be triggered. For example you define 9:30 but you have to exclude holidays.

Package Domyself.dusk.quartz;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;

Import Java.util.Date;
Import Org.quartz.JobBuilder;
Import Org.quartz.JobDetail;
Import Org.quartz.JobKey;
Import Org.quartz.Scheduler;
Import org.quartz.SchedulerException;
Import Org.quartz.SchedulerFactory;
Import Org.quartz.SimpleScheduleBuilder;
Import Org.quartz.Trigger;
Import Org.quartz.TriggerBuilder;
Import Org.quartz.impl.StdSchedulerFactory;

Import Org.quartz.impl.calendar.HolidayCalendar; public class Client {public static void main (string[] args) throws Schedulerexception {final String configfile = "dom
		Yself/dusk/quartz/ ";
		Schedulerfactory factory = new Stdschedulerfactory (configfile);
		Scheduler Scheduler = Factory.getscheduler ();
		<strong>holidaycalendar cal = new Holidaycalendar (); Scheduler.addcalendar ("Myholidays", Cal, False,false);</strong>//10 seconds call Hellojob, always repeat Trigger Trigger = Tri
				Ggerbuilder.newtrigger (). Startnow (). withidentity ("Hello"). Withpriority (6). Modifiedbycalendar ("Myholidays"). Withschedule (Simpleschedulebuilder.

		Repeatsecondlyforever ()). build ();
				Jobdetail job = Jobbuilder.newjob (Dumbjob.class). Withidentity ("Myjob", "group1")//Name "Myjob", Group "group1"
				. Usingjobdata ("Jobsays", "Hello world!")
		. Usingjobdata ("Myfloatvalue", 10f). Build ();
		Scheduler.schedulejob (Job, trigger);
		Scheduler.start ();
	Scheduler.shutdown (TRUE); }
The task will not be invoked because today is 2016-6-19 Sunday and is excluded by Holidaycalendar.

Simpletrigger If you have a job that needs to be executed at a specific time or if you need to repeat at a specified interval at a given time, Simpletrigger meet your needs. Static import is required:

Import static org.quartz.triggerbuilder.*;
Import static org.quartz.simpleschedulebuilder.*;
Import static org.quartz.datebuilder.*:
Creates a trigger that triggers a repeat at the specified event:
Simpletrigger trigger = (Simpletrigger) Newtrigger ()
    . Withidentity ("Trigger1", "group1")
    . StartAt ( Mystarttime)//some Date
    . Forjob ("Job1", "group1")//Identify job with name, group strings
    . Build ();
Creates a trigger that triggers the recurrence of a specified event, repeating 10 times every 10 seconds:
Trigger = Newtrigger ()
    . Withidentity ("Trigger3", "group1")
    . StartAt (mytimetostartfiring)  //If a start The time is isn't given (if this line were omitted), the ' Now ' is implied
    . Withschedule (Simpleschedule ()
        . withintervalinsec Onds (Withrepeatcount)
        //note repeats would give a total of firings forjob
    (myjob)//Identify Job with handle to it jobdetail itself                   
    . Build ();
Create a trigger that fires once in the next 5 minutes:
  Trigger = (Simpletrigger) Newtrigger ()
    . Withidentity ("Trigger5", "group1")
    . StartAt (Futuredate (5, Intervalunit.minute)//Use Datebuilder to create a date in the future
    . Forjob (Myjobkey)//Identify job with its Jo Bkey
    . Build ();
Create an immediately triggered trigger, repeat every 5 minutes until 22:00:
Trigger = Newtrigger ().
    withidentity ("Trigger7", "group1").
    withschedule (Simpleschedule ()
        . Withintervalinminutes (5)
        . RepeatForever ())
    . Endat (dateof (0, 0)). Build
Create a trigger to be triggered in the next one hours, repeated every 2 hours:
  Trigger = Newtrigger ()
    . Withidentity ("Trigger8")//Because group is not specified, "Trigger8" would be in the default Group
    . startAt (Evenhourdate (NULL))//Get the next even-hour (minutes and seconds zero ("00:00"))
    . Withschedule ( Simpleschedule ()
        . Withintervalinhours (2)
        . RepeatForever ())
    //Note So in this example, ' Forjob (..) ' Is isn't called
    //  -which is valid if the trigger are passed to the scheduler along with the job  
    . Build (); 
  scheduler.schedulejob (trigger, job);
Misfire instruction constants for Simpletrigger:
misfire_instruction_reschedule_ Now_with_existing_repeat_count
MISFIRE_ Instruction_reschedule_next_with_remaining_count
Use examples:
  Trigger = Newtrigger ().
    withidentity ("Trigger7", "group1").
    withschedule (Simpleschedule ()
    . Withintervalinminutes (5)
    . RepeatForever ()
    . Withmisfirehandlinginstructionnextwithexistingcount ())
    . Build ();

CrontriggerA word that works. Describes the following corn expression: Seconds Minutes Hours day-of-month Month day-of-week year (optional) Corn expression consists of the top seven parts, separated by a space in the middle. Wildcard characters for corn expressions:
Field Name Mandatory allowed Values allowed Special Characters
Seconds YES 0-59 , - * /
Minutes YES 0-59 , - * /
Hours YES 0-23 , - * /
Day of month YES 1-31 , - * ? /L W
Month YES 1-12 or Jan-dec , - * /
Day of week YES 1-7 or Sun-sat , - * ? L
Year NO Empty, 1970-2099 , - * /

* ("All Values")-for example, "*" is represented per minute in the Minutes field.

? ("No Value specified")-useful when you need to specify something in two fields where one of the fields is allowed and another field is not allowed. For example: You want my trigger to be triggered on a special day in one months (for example: Day 10th), but don't care what the day is, then I'll put 10 in the Day-of-month field and put "?" In the Day-of-week field.

--for specifying a range. For example, "10-12" represents a 10,11,12 point in the Hour field.

,-for specifying additional values. For example, "Mon,wed,fri" represents "Monday, Tuesday, and Friday" in the Day-of-week field.

/-for specifying an increment. For example, "0/15" in the Seconds field represents "0, 15, 30, and 45 seconds". The ' 1/3 ' in the Day-of-month field indicates ' start at 1th every three days ' from the beginning of the month.

L ("Last")-has different meanings in the two fields allowed. For example, "L" in the Day-of-month field represents "the last day of the month"-31st in January, February 28 in the non-Swiss year. If used in the Day-of-week field, it simply means "7" or "SAT." However, after another value in the Day-of-week, it means "the last Friday of the month"-for example, "6L" means "this month's final Fridays". You can also specify an offset to the end of this month, such as "L-3", which represents the third day of the week. When using the "L" option, the most important thing is not to specify a list, or a range of values, otherwise you will get a confusing or unexpected value.

W ("weekday")-lets you specify a working day that is closest to the specified date (Monday to Friday). For example, if you specify "15W" as the value of the Day-of-month field, T means: "The closest working day from 15th." If 15th is Saturday, trigger will be triggered in number 14th-Friday. If 15th is a Sunday, trigger will be triggered at number 16th. If number 15th is Tuesday, trigger will be triggered at number 15th.  However if you specify "1W" as the value of the Day-of-month and number 1th is a Saturday, trigger will be triggered in 3rd, because it will not skip the one-month range. The ' W ' character can be used only when the Day-of-month is a single day, not a range or a list.

Some simple and useful examples:

"0 0/5 * * *?" Triggers every 5 minutes.
"10 0/5 * * *?" Executes every 5 minutes, and the number of seconds is 10, for example: 10:00:10 and 10:05:10
"0 30 10-13?" * Wed,fri " Wednesday, Friday, 10:30, 11:30, 12:30, 13:30 triggers
"0 0/30 8-9 5,20 *?" It triggers every 30 minutes between 8:00 and 9:00 every month, 20th.

	Trigger Trigger = Triggerbuilder.newtrigger ()
				    . Withidentity ("Trigger3", "group1")
				    . Withschedule ( Cronschedulebuilder.cronschedule ("0 0/2 8-17 * *"))
				    . Forjob ("Myjob", "group1")
				    . Build ();

Misfire instruction constants for Crontrigger:
Trigger = Newtrigger ()
    . Withidentity ("Trigger3", "group1")
    . Withschedule (Cronschedule ("0 0/2 8-17 * *")
        . Withmisfirehandlinginstructionfireandproceed ())
    . Forjob ("Myjob", "group1")
    . Build ();

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