Quartz Crontrigger Most complete configuration instructions and write examples.

Source: Internet
Author: User
Tags month name stub
Crontrigger Tutorial

----------------------------------------------------------------------

Also introduce Jar pack Quartz-all-1.6.4.jar, the following example I'm using Quartz-all-1.6.4.jar

If the server is different,

You can use a different jar package

Quartz-jboss-1.6.4.jar
Quartz-oracle-1.6.4.jar
Quartz-weblogic-1.6.4.jar
Introduced
Cron is a tool that has been used for a long time on UNIX systems and has been proven to have strong task scheduling capabilities. The class Crontrigger is based on Cron's ability to schedule tasks.
Crontrigger use "cron expression". A cron expression can create a task-trigger schedule, such as "8 o'clock in the morning every Monday to Friday" or "1:30 P.M. of the last Friday of the month".
The cron expression is powerful, but it's also easy to confuse. The goal of this tutorial is to find the difficulty of creating a cron expression that allows users to have a prioritized resource before they can go to a forum or mailing list for help.

Format
A cron expression is made up of 6 to 7 fields. These fields are separated by spaces and can be any allowable values. See the following table in detail:

Field names must be valid values valid characters
Seconds are 0 to 59,--*/
Minutes are 0 to 59,--*/
Hours are 0 to 23,--*/
Day of Month is 1 to 31,--*? /L W
The month is 1 to 12 or to DEC,-*/
Day of week is 1 to 7 or SUN to SAT,-*? L
Year no empty, 1970 to 2099,-*/

So a cron expression can be simple (e.g. * * * * * *),
can also be very complex (such as: 0 0/5 14,18,3-39,52?) Jan,mar,sep Mon-fri 2002-2010).

Valid characters
"*" (all valid values)--represents all valid values for the field. For example, the "*" for a minute field means "every minute." “?” (No specific value)-can only appear in the day of month field and the day of week field, and the two can only be selected one. Indicates no specific value and can be ignored. For example, I would like to trigger a task number 10th a month, regardless of number 10th is the week. Then you can set "10" in the day of month field and the day of week field to set "?". See the example below. "-"--Specify a range. For example, the "10-12" of the hour field means "10 points, 11 points, 12 dots." ","--Specify an extra value. For example, the "Mon,wed,fri" of the week field of day means "Monday, Wednesday, Friday". '/'--the growth step of the specified value. For example, the "0/15" of the second field means "0 seconds, 15 seconds, 30 seconds, 45 seconds." The "5/15" of the second field means "5 seconds, 20 seconds, 35 seconds, 50 seconds." The day of the month field "1/3" means "starting at 1th per month, once every three days." "L" (last)--only appears in the day of month field with the day of week field and has a different meaning. For example, the "L" of the field day of month means "Last of the Month"-January 31, not a leap year February 28. If used in the day of week field, it means "7" or "SAT"-Saturday. However, if you use the back of a value x in the day's week field, it means "the last one weeks of the month X". For example, "6L" means "last month of Friday". It should be noted that "L" cannot be used when ",", "-". "W" (weekday)--Select the most recent weekday from the given date (Monday to Friday). For example, you specify "15W" as the value of the day of the month field, which means "the most recent weekday with 15th per month". So, if number 15th is Saturday, the trigger will be triggered in number 14th (Friday). If number 15th is Sunday, the trigger is triggered at number 16th (Monday). If number 15th is Tuesday, the trigger is triggered at number 15th (Tuesday). However, if you specify "1W" as the value of the day of month field and number 1th is Saturday, the trigger is triggered in number 3rd (Monday). Quartz will not "jump out" the boundaries of the month. Note that when you use "W", you cannot use ",", "-". "L" and "W" can be combined with "LW" in the day of month field, table to "last Weekday" "#"--Specify the first few weeks of each month X. For example, the "6#3" of the week field of day means "monthly3rd Friday "(day3= Friday, #3= third);" 2#1 "means" 1th Monday per month ";" 4#5 "means" 5th week of 3 per month. " Note that "#5" is not triggered if there is no 5th Wednesday in the month. Valid characters (such as the month name and the week name) are not case sensitive. Mon and Mon are the same.

Example
Here are some complete examples:

The meaning of cron expressions
0 0 12 * *? Every day, 12 o'clock, one trigger.
0 15 10? * * Every 10:15 Trigger
0 15 10 * *? Triggers once every day 10:15
0 15 10 * *? * Trigger once every 10:15
0 15 10 * *? 2005 in 2005 years, every 10:15.
0 * 14 * *? Every day 2 o'clock to 2:59, every minute is triggered once
0 0/5 14 * *? Every day 2 o'clock to 2:55, every 5 minutes trigger
0 0/5 14,18 * *? Daily 2 o'clock to 2:55 and 18 o'clock to 18:55, triggering every 5 minutes
0 0-5 14 * *? Every day 2 o'clock to 2:5, every minute is triggered once
0 10,44 14? 3 WED every Wednesday 2:10 and 2:44 each year in March.
0 15 10? * Mon-fri is triggered once every month in Monday, Tuesday, Wednesday, Thursday, Friday 10:15
0 15 10 15 *? 15 per month, one 10:15 trigger.
0 L *? The last day of the month is 10:15 trigger once
0 15 10? * 6L Monthly last Friday 10:15 trigger once
0 15 10? * 6L Monthly last Friday 10:15 trigger once
0 15 10? * 6L 2002-2005 from 2002 to 2005, the last Friday of each month, 10:15 trigger once
0 15 10? * 6#3 the third Friday 10-point 15 trigger once a month
0 0 12 1/5 *? Starting at number 1th per month, 12 o'clock every 5 days.

0 11 11 11 11? Every November 11 11:11 triggers one


------------------------------------------------------------------------------------

I wrote a class .....

/**
*
*/
Package com.budco.bmp.action.maintenance.temptest;

Import java.text.ParseException;
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Date;
Import java.util.List;

Import Org.quartz.CronTrigger;

Import Com.budco.bmp.core.util.DateUtil;

/**
* @author Ale
*
*/
public class Testscheduledate {

public static final String date_format_month_day_year_hour_min_am_pm = "mm/dd/yyyy Hh:mma";
Trigger
Private final static String Trigger_preview_schedule = "Triggerpreviewschedule";
Protected final static String Group_preview_schedule = "Grouppreviewschedule";

/**
*
*/
Public Testscheduledate () {
TODO auto-generated Constructor stub
}

Protected list<date> getnext10schedules (final String cron) throws ParseException {
Final list<date> results = new arraylist<date> ();

Final Crontrigger Crontrigger = new Crontrigger (Trigger_preview_schedule, Group_preview_schedule, cron);

Date firedate = new Date ();
for (int i = 0; i < i++) {
Firedate = Crontrigger.getfiretimeafter (firedate);
Results.add (firedate);
}
return collections.unmodifiablelist (results);
}

/**
* @param args
* @throws ParseException
*/
public static void Main (final string[] args) throws ParseException {
TODO auto-generated Method Stub
Final Testscheduledate TSD = new Testscheduledate ();
protected static String Default_cron = "0 * * * *";
Final String Default_cron = "0 0/5 16,17 * *";
Final String Default_cron = "0 15 10 * *? *";
Final String Default_cron = "0 10,30 14"? 3 WED ";
Final String Default_cron = "0 11 11 11 11?";

        /final String Default_cron = "0 15 10?" * Mon-fri ";  /means Monday to Friday every 10:15 execution job

        final String Default_cron = "0 15 10? * 6L 2011-2012 "; from 2011 to 2012, the last Friday of each month 10:15 triggers a
        final list<date> Datelist = Tsd.getnext10schedules (Default_cron);
        final list<string> results = new arraylist<string> ();
        for (final Date da:datelist) {
        & nbsp;   results.add (Dateutil.format (DA, date_format_month_day_year_hour_min_am_pm));
        }

        for (final String str:results) {
       & nbsp;    system.out.println (str);

        }
    

}
Print Results:

From 2011 to 2012, the last Friday of each month is triggered 10:15

07/29/2011 10:15am
08/26/2011 10:15am
09/30/2011 10:15am
10/28/2011 10:15am
11/25/2011 10:15am
12/30/2011 10:15am
01/27/2012 10:15am
02/24/2012 10:15am
03/30/2012 10:15am
04/27/2012 10:15am



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.