Java Scheduled Task Scheduling framework Quartz configuration instance code sharing with spring implementation schedule

Source: Internet
Author: User

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

    • Why not apply Java.util.Timer combined Java.util.TimerTask

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.

    • detailed configuration of quartz in spring

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 publicclassTestJobTask{
2       /**
3        *业务逻辑处理
4        */
5         publicvoidservice(){
6             /**业务逻辑*/
7                 ..
8         }
9 }


2. Add a thread pool

1 <!-- 线程执行器配置,用于任务注册 -->
2 <beanid="executor"class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
3  <propertyname="corePoolSize" value="10"/>
4  <propertyname="maxPoolSize" value="100"/>
5  <propertyname="queueCapacity" value="500"/>
6 </bean>

3. Defining Business logic Classes

1 <!-- 业务对象 -->
2 <beanid="testJobTask" class="com.mike.scheduling.TestJobTask"/>

4. Add Quartz call business logic

1 <!-- 调度业务 -->
2 <beanid="jobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
3  <propertyname="targetObject" ref="testJobTask"/>
4  <propertyname="targetMethod" value="service"/>
5 </bean>


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 <beanid="cronTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">
2  <propertyname="jobDetail" ref="jobDetail"/>
3  <propertyname="cronExpression" value="10 0/1 * * * ?"/>
4 </bean>

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 <beanid="taskTrigger"class="org.springframework.scheduling.quartz.SimpleTriggerBean">
2  <propertyname="jobDetail" ref="jobDetail"/>
3  <propertyname="startDelay" value="10000"/>
4  <propertyname="repeatInterval" value="60000"/>
5 </bean>

Delay 10 seconds to start, then execute every 1 minutes

6. Start calling

1 <!-- 设置调度 -->
2 <beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean">
3  <propertyname="triggers">
4   <list>
5    <refbean="cronTrigger"/>
6   </list>
7  </property>
8  <propertyname="taskExecutor" ref="executor"/>
9 </bean>


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


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.