Introduction
Quarzt is an open source project that executes tasks regularly in a project. Quartz is another open source project in the field of Job scheduling. It can be combined with J2EE and J2SE applications or used separately. Here we introduce examples of integration with spring.
Because Spring has integrated Quarzt, we only need to configure it.
Download jar package
You can directly download the jar package at http://www.quartz-scheduler.org/ Quarzt's official website
It can be built through Maven, remember to introduce the jar required for Spring
<!-- Introducing the corresponding dependencies of quartz--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.3</version> </dependency>
concept
Task class: that is, a class that requires code execution at regular intervals.
JobDetail: The details of configuring the task class, that is, the method of injecting the task class and specifying the task class, is an executable job that may be stateful in itself.
MyTrigger: The trigger represents the configuration of a scheduling parameter, configuring the call time.
Scheduler: It is a scheduler container, which can hold many JobDetails and triggers. When the container is started, each JobDetail in it will be automatically executed step by step according to the trigger.
Configuration method
First write a task class
public class MyJob { public void job() { System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ": Task execution"); }}Then spring to configure the bean of the task class
<bean id="myJob" / >
Configure JobDetail, inject task classes and task classes methods
<bean id="jobDetail"> <!-- Inject target object--> <property name="targetObject" ref="myJob"/> <!-- Inject target method--> <property name="targetMethod" value="job"/></bean>
Configure triggers
<!-- Configure trigger--><bean id="myTrigger"> <!-- Inject task details object--> <property name="jobDetail" ref="jobDetail"/> <!-- Inject cron expression, specify the triggering point--> <property name="cronExpression"> <value>/5 * * * * ?</value> // Indicates that it executes every five seconds. Corn will survive in the following</property> </bean>
Finally configure the dispatch factory and inject the configured trigger
<!-- Configure the dispatch factory-><bean id="schedulerFactoryBean"> <!-- Inject trigger--> <property name="triggers"> <list> <ref bean="myTrigger"/> </list> </property></bean>
At this point, the configuration is completed and the timing tasks can be performed.
Execution results
cron expression
corn is used to control the scheduling time of the task and is configured in Trigger. The following is the basic syntax of the corn expression. If it looks too complicated, the corn syntax generator is provided on the Internet http://cron.qqe2.com/. It can automatically generate corn expressions by specifying conditions.
Below are the meanings represented by seven *
Asterisk: * represents any time, which indicates that you want to include all legal values on this field.
* * * * * * means it will trigger every moment
0 * 17 * * ? : Inspire triggers every minute from 5 pm to 5:59 pm every day. It stops at 5:59 pm because the value is 17 in the hour domain, at 6 pm, the hour becomes 18, so I ignore this trigger until 5 pm the next day.
Question mark (?): The ? sign can only be used in the daily and weekly domains, but cannot be used simultaneously on these two domains. Can you think? The character is "I don't care what value is on the field." This is different from an asterisk, which indicates every value on the field. ? means that you do not specify a value for this field. If you specify a value for one of these two fields, you must put a ? on the other word value.
0 10,44 14 ? 3 WEB : is triggered at 2:10 pm and 2:44 pm every Wednesday in March.
Comma (,): is used to specify a list of values to a field. For example, using values 0,15,30,45 in the second field means that a trigger is triggered every 15 seconds.
0 0,15,30,45 * * * ? : Trigger is triggered every quarter of an hour.
Backslash (/): (/) is used for incremental timetable. We just used commas to represent increments every 15 minutes, but we can also write them as 0/15.
0/15 0/30 * * * ? : Trigger is triggered every 15 seconds at both the hour and half points.
Medium Score (-): Medium Score (-) is used to specify a range. For example, 3-8 on the hour domain means "3, 4, 5, 6, 7 and 8 points." The values of the domain do not allow rewinding, so values like 50-10 are not allowed.
0 45 3-8 ? * * :: Triggered from 3 am to 45 am at 8 am.
Letter (L): L indicates the last value allowed on a field. It is only supported by the daily and weekly domains. When used in the day domain, it represents the last day of the month specified in the month domain. When the letter L is used in the week domain, indicating the last day of the week, which is Saturday (or the number 7), you can use a number to connect L to represent the last week of the month X.
0 0 8 L * ? : Triggered at 8:00 am on the last day of each month
0 59 23 ? * L : Triggered at 11:59 on the last Saturday of each month
0 0 12 ? * 2L : The last Monday of each month triggers (the number is connected to L to represent the last week of the month X)
Letter (W): Weekday (Mon-Fri), and can only be used in the daily domain. It is used to specify the closest weekday to the specified day
Hack sign (#): The # character can only be used in the peripheral domain. It is used to specify which day of the week in a specified month. For example, if you specify the value of the weekly field to be 6#3, it means the third Friday of a certain month (6=Friday, #3 means the third week of the month).
Example
"0012**?" Triggered at 12 noon every day "01510?**" Triggered at 10:15 am every day "01510**?" Triggered at 10:15 am every day "01510**?*" Triggered at 10:15 am every day "01510**? 2005" Triggered at 10:15 am every day "0*14**?" Triggered at 10:15 am every day in 2005 from 2:59 pm every day "00/514**?" Triggered at 2:00 pm every day from 2:00 pm to 2:55 pm every day "00/514,18**?" Triggered at 2:00 pm every day from 2:00 pm to 2:55 pm and every 5 minutes every day from 6:55 pm and every 6:55 pm to 6:55 pm every day Every 1 minute triggered by "010,4414?3WED" on Wednesdays of March each year at 2:10 and 2:44 pm on Wednesdays of March each year at 10:15 AM on Monday to Fridays "0151015*?" "015 AM on 15th of the month" "01510L*?" "015 AM on the last day of the month" "01510?*6L" on the last Fridays of the month at 10:15 AM on the last Fridays of the month "01510?*6L2002-2005" on the last Fridays of the month from 2002 to 2005 at 10:15 AM on the last Fridays of the month "01510?*6#3" on the third Fridays of the month
Summarize
The above is all about the quarzt scheduled tasks in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!