Click the link to join the group "Java EE (ssh+intellijide+maven)": http://jq.qq.com/?_wv=1027&k=L2rbHv
A: Quartz introduction
Opensymphony's Quartz provides a perfect solution for task scheduling.
Quartz is an open-source job scheduling framework, timing Scheduler, which provides a simple but powerful mechanism for job scheduling in Java applications.
There are two basic concepts in quartz: jobs and triggers. A job is an executable task that can be dispatched, and the trigger provides a schedule for the job
II: Quartz Spring configuration
1. The main reason for this is that it is inconvenient to apply, especially when setting a specific month-to-date time, while Quartz uses a cron configuration similar to Linux, and is easily configured to perform triggers every time.
2. Second performance reasons, the use of the JDK comes with the timer does not have multi-threading, and quartz using a thread pool, performance is much higher than the timer.
In spring, there are two main ways to use: the first, and currently the most used, Spring provides a Methodinvokingjobdetailfactorybean proxy class that directly invokes a function of the task class through the Reilly class, and the second, The Quartz interface is implemented in the program, and Quartz is dispatched through the interface.
The main explanation is the proxy class provided by spring Methodinvokingjobdetailfactorybean
1. Business logic class: Business logic is independent, itself and quartz decoupling, and did not go into, this is a good way for business.
1 |
public class TestJobTask{ |
2. Add a thread pool
1 |
<!-- 线程执行器配置,用于任务注册 --> |
2 |
< bean id = "executor" class = "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" > |
3 |
< property name = "corePoolSize" value = "10" /> |
4 |
< property name = "maxPoolSize" value = "100" /> |
5 |
< property name = "queueCapacity" value = "500" /> |
3. Defining Business logic Classes
2 |
< bean id = "testJobTask" class = "com.mike.scheduling.TestJobTask" /> |
4. Add Quartz call business logic
2 |
< bean id = "jobDetail" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > |
3 |
< property name = "targetObject" ref = "testJobTask" /> |
4 |
< property name = "targetMethod" value = "service" /> |
5. There are two ways to increase the trigger of the call, the time of the trigger:
The first trigger time, with Linux-like Cron, configures the time to emit a rich representation
1 |
< bean id = "cronTrigger" class = "org.springframework.scheduling.quartz.CronTriggerBean" > |
2 |
< property name = "jobDetail" ref = "jobDetail" /> |
3 |
< property name = "cronExpression" value = "10 0/1 * * * ?" /> |
Cron expression "10 */1 * * *?" Starts at 10 seconds, executes every 1 minutes
Second, the use of a relatively simple way to affirm the delay time and time interval
1 |
< bean id = "taskTrigger" class = "org.springframework.scheduling.quartz.SimpleTriggerBean" > |
2 |
< property name = "jobDetail" ref = "jobDetail" /> |
3 |
< property name = "startDelay" value = "10000" /> |
4 |
< property name = "repeatInterval" value = "60000" /> |
Delay 10 seconds to start, then execute every 1 minutes
6. Start calling
2 |
< bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" > |
3 |
< property name = "triggers" > |
5 |
< ref bean = "cronTrigger" /> |
8 |
< property name = "taskExecutor" ref = "executor" /> |
7. End: Start the container and the spring and quartz have been combined.
common expressions for cron
"0 0 12 * *?" trigger 12 o'clock noon every day.
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Daily 10:15 Trigger
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 Trigger
"0 * 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:59 daily
"0 0/5 14 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily
"0 0/5 14,18 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily and from 6 o'clock in the afternoon to 6:55
"0 0-5 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:05 daily
"0 10,44 14?" 3 WED "2:10 and 2:44 triggers in Wednesday of every March
"0 15 10?" * Mon-fri "Monday to Friday 10:15 trigger
"0 15 10 15 *?" 15th 10:15 per month
"0 L *?" 10:15 on the last day of the month
"0 15 10?" * 6L "Last month of Friday 10:15 Trigger
"0 15 10?" * 6L 2002-2005 "2002 to 2005 the last of the monthly Friday 10:15 trigger
"0 15 10?" * 6#3 "Monthly third Friday 10:15 trigger
Three: Quartz principle
according to the above spring configuration, we are more clear about the internal situation of quartz, below we mainly explain the configuration involved in each point
1. We first look at the last step, Schedulerfactorybean, Scheduler Factory implementation, which can produce a corresponding multiple jobdetail and trigger, each jobdetail corresponds to trigger represents a task
Quartz's schedulerfactory is a standard factory class and is not suitable for use in spring environments. In addition, to ensure that the scheduler can perceive the life cycle of the spring container and complete the automatic start-up and shutdown operations, it is necessary to correlate the lifecycle of the scheduler with the spring container. So that after the spring container starts, Scheduler automatically starts to work, and the scheduler is turned off automatically before the spring container is closed. To do this, spring provides Schedulerfactorybean, a factorybean that roughly has the following features:
1) Provide configuration information for scheduler in a more bean-style manner;
2) The life cycle of scheduler and spring container is established, and the phase integration processes is related;
3) Replace Quartz's own configuration file with some or all of the property configuration.
2.jobDetail, which represents an executable business call
3.trigger: Scheduled time schedule, when, how much time to execute the time schedule
4. Threadpooltaskexecutor, the thread pool, is used to execute each corresponding job in parallel, improving efficiency, which is one of the most important reasons for not recommending using the JDK's own timer.
Original: http://www.blogjava.net/mikechen/archive/2012/07/03/382090.html