Quartz's Hello (Java)

Source: Internet
Author: User

Quartz Task Scheduling Framework

Simply put: it is at a specific time, to do the specified event, and then specific to an object to do

The first experience of quartz:

1.pom.xml file (Import jar package)

<dependencies><dependency><groupId>org.quartz-scheduler</groupId><artifactId> Quartz</artifactid><version>2.2.1</version></dependency><dependency><groupid >org.quartz-scheduler</groupid><artifactid>quartz-jobs</artifactid><version>2.2.1 </version></dependency><dependency><groupid>org.slf4j</groupid><artifactid >slf4j-log4j12</artifactid><version>1.7.12</version></dependency></dependencies >

2. Hello to create a task scheduler

public static void Main (string[] args) throws Exception {//Create timer Object Scheduler Scheduler = Stdschedulerfactory.getdefaultscheduler ();//Start Timer object Scheduler.start (); SYSTEM.OUT.PRINTLN ("1234");//Off Timer scheduler.shutdown ();}

Add triggers and work objects based on the above

Create a class to implement the Job method

public class Hellojob implements JOB {public void execute (jobexecutioncontext context) throws Jobexecutionexception { System.out.println ("test01");}}

Invoking a work object by using a trigger

public static void Main (string[] args) throws Exception {//Create timer Object Scheduler Scheduler = STDSCHEDULERFACTORY.GETDEFAULTSC Heduler ();//define a Work object Jobdetail job = Jobbuilder.newjob (Hellojob.class). Withidentity ("Job1", "group1"). Build ();// Define a trigger trigger Withschedule = Triggerbuilder.newtrigger (). Withidentity ("Trigger1", "group1"). Startnow (). Withschedule (Simpleschedulebuilder.repeatsecondlyforever (1)). build (); Scheduler.schedulejob (Job, withSchedule); /Turn on Timer object Scheduler.start ();//off Timer//Scheduler.shutdown ();}

  

Object Triggerbuilder Start task time Startnow immediately start startAt (date) Specify time start object Simpleschedulebuilder perform simple task recurrence repeatsecondly ... () How many seconds to repeat after repeatminutely ... () How many minutes after the repeathourly is repeatedly executed ... () Number of hours after repeated execution

The use of the above will result in the processing of time does not achieve the desired requirements

public static void Main (string[] args) throws Exception {//Create timer Object Scheduler Scheduler = STDSCHEDULERFACTORY.GETDEFAULTSC Heduler ();//define a Work object Jobdetail job = Jobbuilder.newjob (Hellojob.class). Withidentity ("Job1", "group1"). Build ();// Defines a trigger that  needs to be triggered every second trigger Withschedule = Triggerbuilder.newtrigger (). Withidentity ("Trigger1", "group1"). Startnow (). Withschedule (cronschedulebuilder.cronschedule ("** * * * *?")). Build (); Scheduler.schedulejob (Job, withschedule);//Turn on Timer object Scheduler.start ();//Turn off Timer//Scheduler.shutdown ();}

  

Use the calendar mode for processing so it will be. We can achieve most of our needs for timed time.

What do those characters mean, from the number of digits?

1. Seconds seconds 2. Minutes min 3. Hours hours 4. Day-of-month the day of the month 5. Month 6. Day-of-week Days of the Week 7. Year (optional field) years (optional field)

Where wildcard characters are used to set (*,/,? , l,w,#)

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

Represents 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 part of the domain, it means" every 20 minutes in the hour, from the 3rd minute
"3,23,43" or another form of the same.

‘?‘ Character

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

' L ' character

Can be used in Day-of-month and Day-of-week, this character is the "last" shorthand,
However, the meanings are different 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 of 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, it means "the most
After the week XXX. " For example: "6L" or "Fril" represents the last Friday of this month. When you use the ' L ' option,
The most important thing is not to specify a list or range of values, which can cause confusion.

' W ' character

Used to specify the closest number of weeks (specified in the Day-of-week field) for a distance to a given day. For example:
If you specify "15W" for the Day-of-month field, it means "the nearest week from the month 15th"

' # ' character

Represents the first few weeks of the month. For example: "6#3" or "fri#3" in the Day-of-week domain
Represents "The third Friday of the month".

Quartz's Hello (Java)

Related Article

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.