Java Trigger Crontrigger

Source: Internet
Author: User

Summary: Crontriggers is usually more useful than simpletrigger if you need to trigger tasks on a schedule like a calendar instead of every specific interval like simpletrigger.

I. web. XML:

<servlet><servlet-name>boot</servlet-name><servletclass>xxx.xx.x.bootservlet</ Servlet-class><load-on-startup>1</load-on-startup></servlet>

Ii. creation of Bootservlet.java

  

public class Bootservlet extends HttpServlet {public  void init (servletconfig config) throws servletexception {    s Uper.init (config);    Initializes the database    new Srvinitdb (). Initdb ();    Start Alert trigger    Startexpenseremain ();  }    /***/public    void Startexpenseremain () {try {Scheduler Scheduler = Stdschedulerfactory.getdefaultscheduler ();  Scheduler.start ();  Once a week in Friday jobdetail expenseremindjob = new Jobdetail ("Expenseremindjob", Null,expenseremindjob.class);  Crontrigger expenseremindjobtrigger=new Crontrigger ("Expenseremindjobtrigger");  Expenseremindjobtrigger.setstarttime (New Date ()); Expenseremindjobtrigger.setcronexpression ("0 0 12?  * FRI "); Scheduler.schedulejob (Expenseremindjob, Expenseremindjobtrigger); } catch (Schedulerexception e) {e.printstacktrace ();} catch (ParseException e) {e.printstacktrace ()}}}

Iii. creation of Job:ExpenseRemindJob.java

public class Expenseremindjob implements job{public void execute (jobexecutioncontext arg0) throws Jobexecutionexception  {  sendexpensesubjectremind ();  } /***/public synchronized void Sendexpensesubjectremind () {}}

Note:

Where new Crontrigger ("xxx"). Setcronexpression ("0 0 12? * FRI ");
A total of 7 sub-expressions all describe a separate schedule detail. These sub-expressions are separated by spaces, respectively:
1. Seconds sec
2. Minutes minutes
3. Hours hours
4. Days of the Day-of-month month
5 month
6. Day-of-week Days of the Week ("Mon-fri", "MON, WED, FRI" or even "Mon-wed,sat")
7. Year (optional field) (optional field)
The example string for a cron expression is "0 0 12?" * WED ", which means" 12:00 noon every Wednesday ".

All of the values in the domain have a specific legal scope, the legal range of these values is quite obvious, for example: the legal value of the second and the domain is 0 to 59, the legal range of the hour is 0 to 23,day-of-month is worth legal where the range is 0 to 31, but you need to be aware of the different days in different months. The legal value for the month is 0 to 11.

or use the string Jan,feb MAR, APR, May, June, JUL,, SEP, OCT, NOV and Dec to indicate. Days-of-week can be expressed in 1 to 7来 (1= Sunday) or in string Sun, MON, TUE, WED, THU, FRI, and Sat.

The wildcard character (' * ') can be used to represent "each" possible value in a domain. Therefore, the * in the month field represents each month, while the * in the Day-of-week field represents "Every day of the week".

‘?‘ Characters can be used in day-of-month and Day-of-week domains, which are used to denote "no value specified".
This is useful when you need to specify a value for one or two fields without having to set up other domains.

The '/' character is used to denote the increment of the value, for example, if the minute field is placed in ' 0/15 ', it means "every 15 minutes, starting from 0",
If you use ' 3/20 ' in the minute field, it means "every 20 minutes in the hour, starting from the 3rd minute" or the other same form is ' 3,23,43 '.

The ' L ' character can be used in Day-of-month and Day-of-week, which is a shorthand for "last", but has a different meaning in two domains.

For example, the "L" in the Day-of-month field represents the last day of the month, that is, 31st of January, non-leap year February of 28th.
If it is used in day-of-week, it means "7" or "SAT". However, if the character is followed by a different value in the Day-of-week field, the last week of the month is xxx.
For example: "6L" or "Fril" represents the last Friday of this month. When using the ' L ' option, the most important thing is not to specify a list or range of values, which can cause confusion.

The ' W ' character is used to specify the closest week (specified in the Day-of-week field) to the nearest day of the day.
For example: If you specify "15W" for the Day-of-month field, it means "the nearest week of the month 15th".

' # ' represents the first few weeks of the month. For example: "6#3" or "fri#3" in the Day-of-week field means "third Friday of the month".

Java Trigger Crontrigger

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.