Spring integrates Quzrtz timing tasks

1. What is Quartz

Quartz is an open source project of the OpenSymphony open source organization in the field of Job scheduling. It can be combined with J2EE and J2SE applications or used alone. Quartz can be used to create simple or complex daily schedules for running ten, hundreds, or even tens of thousands of Jobs. Jobs can be made into standard Java components or EJBs.

Quartz is a task scheduling system, a system responsible for executing (or notifying) other software components when a predetermined (scheduled) time arrives.

Quartz ships with a small Java library (.jar file) that contains all of the Quartz core functionality. The main interface (API) for these functions is the Scheduler interface. It provides simple operations such as: adding or canceling tasks to the schedule, starting/stopping/pausing schedule progress.

2. Download and installation of Quartz

The latest version of quartz can be downloaded from the official website.

Official website: http://quartz-scheduler.org/

Maven dependencies, this tutorial is about Quartz+spring, so the dependencies are as follows:

 

<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
	<version>2.2.2</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.1.3.RELEASE</version>
</dependency>

 3. Quartz framework architecture

 

       Core objects of the Quartz framework

  • Scheduler - the core scheduler
  • Job – task
  • JobDetail - job description
  • Trigger -- trigger

    relationship between objects



 

 4. How to use

    1. Step 1: Create the Job class

       Create a java class and create a normal method as a task handling method.

    2. Step 2: Configure the Job to the spring container

<bean id="schedule1" class="com.mypackage.test.schedule.Schedule1"></bean>

    3. Step 3: Configure the Job class with JobDetail

<bean id="SpringQtzJobMethod"
	class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	<property name="targetObject">
		<ref bean="schedule1" />
	</property>
	<property name="targetMethod"> <!-- method name to execute -->
		<value>execute</value>
	</property>
</bean>

    4. Step 4: Configure the scheduling trigger

<bean id="cronTriggerFactoryBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean ">
	<property name="jobDetail" ref="SpringQtzJobMethod"></property>
	<property name="cronExpression" value="0/5 * * * * ?"></property>
</bean>

   5. Step 5: Configure the scheduling factory

<bean id="SpringJobSchedulerFactoryBean"
	class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	<property name="triggers">
		<list>
			<ref bean="cronTriggerFactoryBean" />
		</list>
	</property>
</bean>

 5. Cron expressions

Cron expressions are used to configure CronTrigger instances. Cron expressions are strings that are actually composed of seven sub-expressions, describing the schedule of individual details. These subexpressions are separated by whitespace and represent:

       1.Seconds

       2.Minutes

       3.Hours

       4.Day-of-Month

       5.Month

       6.Day-of-Week

       7.Year (optional field)

Example "0 0 12 ? *WED" is executed every Wednesday at 12:00 PM,

Individual subexpressions can contain ranges, for example, in the previous example ("WED") could be replaced with "MON-FRI", "MON, WED, FRI" or even "MON-WED,SAT".

"*" represents the entire time period.

Each field has a set of valid values ​​that can be specified, such as

Seconds (seconds): can be represented by numbers 0-59,

Minutes (minutes): can be represented by numbers 0-59,

Hours (hours): can be represented by numbers 0-23,

Day-of-Month (day): You can use any of the numbers 1-31, but pay attention to some special months

Month (month): can be represented by 0-11 or by the string "JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC"

Day-of-Week (weekly): can be represented by numbers 1-7 (1 = Sunday) or by the string "SUN, MON, TUE, WED, THU, FRI and SAT"

"/": It is a special unit, expressed as "every", such as "0/15", which means every 15 minutes, "0" means starting from "0", "3/20" means every 20 minutes Execute once, "3" means start executing from the 3rd minute

"?": Indicates a certain day of the month, or a certain day of the week

"L": used for monthly, or weekly, expressed as the last day of the month, or the last day of the week of each month, such as "6L" for "the last Friday of the month"

"W": expressed as the most recent working day, for example, "15W" on the day-of-month field is expressed as "the closest working day to the 15th of this month"

""#": is used to specify "" the nth working day of each month. For example, in the field of day-of-week, the content is "6#3" or "FRI#3", which means " 3rd Friday of every month”

 

1) The format of the Cron expression: seconds, minutes, hours, days, months, and years (optional).

       Field Name Allowed Values ​​Allowed Special Characters  

       seconds 0-59 , -*/  

       points 0-59 , - * /  

       hours 0-23 , - * /  

       Day 1-31 , -*?/LWC  

       月                         1-12 or JAN-DEC         , - * /  

       Day of the week 1-7 or SUN-SAT , - * ? / LC #  

       year (optional field) empty, 1970-2099 , - */

 

       "?" character: Indicates an indeterminate value

       "," character: specify several values

       "-" character: specifies a range of values

       "/" character: Specifies the increment of a value. n/m means starting from n and increasing m each time

       "L" character: used in day to indicate the last day of the month, used in week to indicate the last week of the month X

       "W" character: Specifies the working day closest to the given date (Monday to Friday)

       "#" character: Indicates the week X of the month. 6#3 means the 3rd Friday of the month

 

 

2) Cron expression example:

Execute every 5 seconds: */5 * * * * ?

Execute every 1 minute: 0 */1 * * * ?

Execute once a day at 23:00: 0 0 23 * * ?

Execute once a day at 1 am: 0 0 1 * * ?

Execute once every month at 1:00 am: 0 0 1 1 * ?

Executed at 23:00 on the last day of every month: 0 0 23 L * ?

Executed once a week at 1 am on Sunday: 0 0 1 ? *L

Execute once at 26, 29, 33: 0 26,29,33 * * * ?

Execute once every day at 0:00, 13:00, 18:00, and 21:00: 0 0 0,13,18,21 * * ?

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326526524&siteId=291194637