Inherited and simplified Quartz in Spring
Quartz is a powerful enterprise-level task scheduling framework. Spring inherits and simplifies Quartz. Let's see how to configure Quartz in Spring:
First, let's write a scheduled class:
Java code Collection code
package com.kay.quartz ;
public class QuartzJob
{
public void work()
{
System.out.println("Quartz's task scheduling!!!");
}
}
Spring's configuration file:
Java code Collection code
<?xml version="1.0" encoding=" UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!- - Job class to call -->
<bean id="quartzJob" class="com.kay.quartz. QuartzJob"></bean>
<!-- Define the calling object and the method of calling the object-->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- The calling class-->
<property name= "targetObject">
<ref bean="quartzJob"/>
</property>
<!-- call a method in the class-->
<property name="targetMethod">
<value>work</value>
</property>
< /bean>
<!-- define trigger time-->
<bean id="doTime" class="org. springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask"/>
</property>
<!-- cron expression-->
<property name="cronExpression">
<value>10,15,20,25,30,35,40,45, 50,55 * * * * ?</value>
</property>
</bean>
<!-- If the general management class sets lazy-init='false', the scheduler will be executed when the container starts -->
<bean id ="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>
</beans>
Test program:
Java code collection code
package com.kay.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest
{
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println("Test start.");
ApplicationContext context = new ClassPathXmlApplicationContext("quartz-config.xml");
//If in the configuration file Set the lazy-init of the startQuertz bean to false to avoid instantiation
//context.getBean("startQuertz");
System.out.print("Test end..");
}
}
We need to put the log4j configuration file into the src directory and start the main class.
About cron expressions (from the web):
Cron expressions include the following 7 fields:
second
minute
hour
month date
month
week date
year (optional fields)
special characters
Cron triggers utilize a series of special characters as follows:
inverse A slash (/) character indicates an incremental value. For example, "5/15" in the seconds field means every 15 seconds starting at the 5th second.
The question mark (?) character and the letter L character are only available in day-of-month and day-of-week fields. A question mark indicates that the field does not contain a specific value. So, if you specify a day of the month, you can insert a "?" in the day of the week field, indicating that the day of the week value doesn't matter. The letter L character is short for last. Placed in the date of the month field, it means that it is scheduled to be executed on the last day of the month. In the day of the week field, if "L" exists alone, it is equal to "7", otherwise it represents the last instance of the day of the week in the current month. So "0L" means that it is scheduled to execute on the last Sunday of the month.
The letter (W) character in the day-of-month field schedules execution on the weekday closest to the specified value. Putting "1W" in the date of month field means to schedule execution on the first working day of the month.
The pound (#) character specifies a specific weekday instance for a given month. Put "MON#2" in the day of the week field to schedule the task on the second Monday of the month.
The asterisk (*) character is a wildcard character, meaning that the field can accept any possible value.
Field Allowed Values Allowed special characters
seconds 0-59 , -*/
minute 0-59 , -*/
hour 0-23 , -*/
date 1-31 , -* ?/LWC
month 1-12 or JAN-DEC , - */
Week 1-7 or SUN-SAT , - * ? / LC
#Year (optional) Leave blank, 1970-2099 , - */
Expression meaning
"0 0 12 * * ?" Triggered at 12 noon every day
" 0 15 10 ? * *" triggers every day at 10:15 am
"0 15 10 * * ?" every day at 10:15 am triggers
"0 15 10 * * ? *" every day at 10:15 am triggers
"0 15 10 * * ? 2005 "Trigger "0 * 14 * * ?" every day at 10:15 am in 2005 and "0 0/5 14 * * ?"
every 1 minute between 2 pm and 2:59 pm every day
"0 0/5 14,18 * * ?" triggers "0 0-5 14 * * ?" every 5 minutes from 2pm to 2:55pm and from 6pm to 6:55
pm every day at 2pm Triggers "0 10,44 14 ? 3 WED" every 1 minute from pm to 2:05 pm
Every March Wednesday at 2:10 pm and 2:44 pm
"0 15 10 ? *MON-FRI" Monday to week Trigger
"0 15 10 15 * ?" on Friday at 10:15 am Trigger
"0 15 10 L * ?" on the 15th of every month at 10:15 am Trigger
"0 15 10 ? * 6L" at 10:15 am on the last day of every month " fires "0 15 10
? * 6L 2002-2005" at 10:15 am on the last Friday of every month
Triggered
every at 6:00am
6 * * *
every two hours 0
*/2 * * *
every two hours between 11:00pm and 8:00am at 8:00am
23-7/2, 8 * * *
4th of every month and every Monday to Wednesday at 11:00
11 4 * 1-3
January 1st at 4:00
4 1 1 *
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326481297&siteId=291194637
Recommended
Ranking