Scheduled tasks based on quartz (detailed explanation)
brief introduction
Quarzt is an open source project that regularly executes tasks in the project. Quartz is another open source project in the job scheduling field of OpenSymphony open source organization. It can be combined with J2EE and J2SE applications or used alone. Here we introduce an example of integration with spring
Because spring has integrated quarzt, we only need to configure it.
Download jar package
1. You can go directly http://www.quartz-scheduler.org/ Download the jar package from quarzt's official website
2. It can be built through Maven. Remember to introduce the jar required by spring
concept
Task class: that is, the class that needs to execute code regularly.
JobDetail: configuring the details of the task class, that is, injecting the task class and specifying the method of the task class, is an executable work, which may be stateful.
Trigger (mytrigger): trigger represents the configuration of a scheduling parameter and the time of call.
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
• write the task class first
• then spring configures the bean of the task class
• configure JobDetail and inject methods of task class and task class
• configure triggers
• finally, configure the scheduling factory and inject the configured trigger
This completes the configuration, and the scheduled tasks can be executed.
results of enforcement
cron expressions
Corn is used to control the scheduling time of tasks. It is configured in trigger. The following is the basic syntax of corn expression. If it looks too complex, a corn syntax generator is provided on the Internet http://cron.qqe2.com/ You can automatically generate a corn expression by specifying conditions.
Here are the meanings of the seven * s
Asterisk: * represents any time, which indicates that you want to include all legal values in this field,
** * * * * means that it will be triggered all the time
0 * 17 * * ? : Trigger every minute from 5 p.m. to 5:59 p.m. every day. It stops at 5:59 p.m. because the value of 17 is in the hour field. At 6 p.m., the hour becomes 18, so it ignores the trigger until 5 p.m. the next day.
Question mark (?):? Can only be used in the day and week fields, but can not be used in both fields. You can think that the? Character is "I don't care what value is in this field." This is different from the asterisk, which indicates each value in the field.? If you specify a value for one of these two fields, you must put a value on the other word value?.
0 10,44 14 ? 3 Web: triggered at 2:10 p.m. and 2:44 p.m. on every Wednesday in mid March.
Comma (,): used to specify a list of values for a field. For example, using the values 0,15,30,45 in the second field means that a trigger is triggered every 15 seconds.
0 0,45 * * * ? : Trigger every quarter of an hour.
Backslash (/): (/) is used to increment the schedule. We just used commas to represent increments every 15 minutes, but we can also write 0 / 15 like this.
0/15 0/30 * * * ? : Trigger every 15 seconds at the hour and half hour.
Dash (-): dash (-) is used to specify a range. For example, 3-8 in the hour field means "3,4,5,6,7 and 8 points." The value of the field does not allow rollback, so values like 50-10 are not allowed.
0 45 3-8 ? * * :: Triggered from 3 a.m. to 45 a.m.
Letter (L): l indicates the last value allowed in a field. It is only supported by the day and week fields. When used in the day field, it indicates the last day of the month specified in the month field. When l letter is used in the week field, it indicates the last day of the week, which is Saturday (or number 7). You can connect a number with L to indicate the last week X of the month.
0 0 8 L * ?: Triggered at 8:00 a.m. on the last day of each month
0 59 23 ? * 50: Triggered at 11:59 p.m. on the last Saturday of each month
0 0 12 ? * 2L: triggered on the last Monday of each month (the number connected with L indicates the last week X of the month)
Letter (W): mon Fri, and can only be used in the day domain. It is used to specify the nearest weekday from the specified day
The pound sign (#): # character can only be used in the week field. It is used to specify which day of the week in the month. For example, if you specify the value of the week field as 6#3, it means the third Friday of a month (6 = Friday, #3 means the third week in the month).
Example
The above quartz based scheduled scheduling task (detailed explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.