1. Introduction to the use of QuartzJobBean:
Timer functions are often used in project development, such as batching in the evening, processing accounts regularly, etc. jdk also provides Timer to implement timer tasks, a brief introduction to Timer implementation.The use of Timer is roughly divided into two steps:
First, write a task class that inherits TimerTask and overrides the run method
package com.erayt.timer; import java.util.TimerTask; public class MTimerTask extends TimerTask{ private String name; public MTimerTask(String nicKname){ this.name = nicKname; } @Override public void run() { // TODO Auto-generated method stub System.out.println("Current name:"+name); } }
Second, create Timer objects, set tasks and schedule strategies
package com.erayt.timer; import java.util.Timer; public class MyTimer extends Timer{ public static void main(String[] args) { Timer timer = new Timer();//Create Timer Object MTimerTask task = new MTimerTask("Hello"); timer.schedule(task, 2000, 1000);//Parameters: Set the task, which is triggered at intervals after startup, and how often after triggering } }
This article does not focus on the use of Timer. The most common way to integrate spring with Quartz in a project is to use QuartzJobBean.There are three key objects:
1. Scheduling work class: org.springframework.scheduling.quartz.JobDetailBean, which specifies the scheduling work class through the jobClass attribute;
2. Schedule trigger: org.springframework.scheduling.quartz.CronTriggerBean, which specifies the work class through the jobDetail property, and
The cronExpression property specifies the dispatch frequency;
3. Scheduling factory class: org.springframework.scheduling.quartz.SchedulerFactoryBean, which specifies one or more triggers through the triggers property.
Below is the implementation of a simple test for the project.
2. Required jar packages (version not fixed):
pring-2.5.6.jar, commons-logging-1.1.jar, quartz-all-1.6.5.jar, commons-collections-3.2.jar (without this package, startup tests will fail
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.SetUtils).
3. Create a java project and import jar packages
4. Writing work classes (simple implementation)
package com.erayt.job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class Job1 extends QuartzJobBean { private int timeout;//How often does the schedule start private static int i = 0; //JobDetailBean injects this property value through jobDataAsMap, specifying how many seconds after the server starts calling the work class for the first time public void setTimeout(int timeout) { this.timeout = timeout; } //Tasks to Schedule protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("QuartzJobBean-Dispatch" + ++i + "Have in hand..."); } }
5. Configure the spring file (this is written in the root directory)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- JobDetailBean 1,Work Class Configuration --> <bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.erayt.job.Job1" /> <property name="jobDataAsMap"> <map> <entry key="timeout" value="10" /><!-- Job1 In timeout Property, schedule enabled after 10 seconds--> </map> </property> </bean> <!-- CronTriggerBean 2,Trigger Configuration--> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="job1" /><!-- Work class in injection step 1 --> <!-- Schedule every second --> <property name="cronExpression" value="0/1 * * * * ?" /> </bean> <!-- SchedulerFactoryBean 3,Configure Scheduling Factory --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /><!-- Trigger in Injection Step 2 --> </list> </property> </bean> </beans>
6. Write a scheduling test class (load the spring configuration file)
package com.erayt.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ScheduleTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); } }
7. The dispatch results are as follows:
QuartzJobBean-Schedule 1 is in progress...
QuartzJobBean-Schedule 2 is in progress...
QuartzJobBean-Schedule 3 is in progress...
QuartzJobBean-Schedule 4 is in progress...
QuartzJobBean-Schedule 5 is in progress...
QuartzJobBean-Schedule 6 is in progress...
8. Supplement some scheduling time allocation methods.
Specific time settings are available for reference
"0/10 * * * * *?" Triggered every 10 seconds
"0 0 12 * *?" Triggers every day at 12 noon
"0 1510?* *" Triggers every day at 10:15 a.m.
"0 1510 * *?" Triggers every day at 10:15 a.m.
"0 1510 * *?*" Triggers every day at 10:15 a.m.
"0 1510 * *? 2005" Triggered every morning at 10:15 a.m. in 2005
"0 * 14 * *?" Triggers every minute between 2 p.m. and 2:59 p.m. each day
"0/5 14 * *?" Triggers every 5 minutes between 2 p.m. and 2:55 p.m. each day
"0/5 14,18 * *?" Triggers every 5 minutes between 2 p.m. and 2:55 p.m. each day and between 6 p.m. and 6:55 p.m.
"0-5 14 * *?" Triggers every minute between 2 p.m. and 2:05 p.m. each day
"0 10,44 14? 3 WED" is triggered every March Wednesday at 2:10 p.m. and 2:44 p.m.
"0 1510?* MON-FRI" Triggers from Monday to Friday at 10:15 a.m.
"0 15 10 15 *?" Triggered at 10:15 a.m. on the 15th of each month
"0 15 10 L *?" Triggered at 10:15 on the last day of each month
"0 1510?* 6L" Triggers at 10:15 a.m. on the last Friday of the month
"0 1510?* 6L 2002-2005" Triggered at 10:15 a.m. on the last Friday of each month, 2002-2005
"0 1510?* 6#3" Triggers on the third Friday of the month at 10:15 a.m.
Execute every 5 seconds: */5 * * * * *?
Execute every 1 minute: 0 */1 * * *?
Execute once a day at 23:00:23 * *?
Run once a day at 1 am: 0 01 * *?
Run once a month at 1 AM on 1st: 0 01 1 *?
Execute at 23 o'clock on last day of month: 0 0 23 L *?
Once a week, at 1 a.m. on Sundays: 0.01? * L
Execute once at 26, 29, 33 points: 0 26, 29, 33 * *?
Every day at 0, 13, 18, 21 o'clock: 0 0, 13, 18, 21 * *?