Spring+Quartz timing tasks

Usage of Spring+Quartz:
 
1. The business method class
package com.task;
/**
 * Business method
 *  */ public class TestJob {  public void execute() {   try {    System.out.println("My business method was called---------!");   } catch (Exception ex) {    ex.printStackTrace();   }  } }  









2. Configuration file beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" http://www.springframework.org/schema/beans "
 xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
 xmlns:aop=" http://www.springframework.org/schema/aop "
 xmlns:context=" http://www.springframework.org/schema/context "
 xmlns:tx=" http://www.springframework.org/schema/tx "
 xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            
              
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">  <!-- 普通的业务Bean -->  <bean id="testJob" class="com.task.TestJob" />  <!-- 触发器 -->  <bean   class="org.springframework.scheduling.quartz.SchedulerFactoryBean">   <property name="triggers">    <list>     <ref bean="testTrigger" />    </list>   </property>   <property name="autoStartup" value="true" />  </bean>  <bean id="testTrigger"  
              













  class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="testJobDetail" />
  <!-- Trigger every 1 second -->
  <property name="cronExpression" value ="*/1 * * * * ?" />
 </bean>
 <!-- Scheduling-->
 <bean id="testJobDetail"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name= "targetObject" ref="testJob" />
  <property name="targetMethod" value="execute" />
  <!-- Whether to allow concurrent execution of tasks. When the value is false, it means that a new thread must be started after the previous thread is processed -->
  <property name="concurrent" value="

3. Test method
package com.task;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**  * Test class  *  */  
 
 
public class Test {
 public static void main(String[] args) {
  try {
   AbstractApplicationContext context = new ClassPathXmlApplicationContext("com/task/beans.xml");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e .printStackTrace();
  }
  System.out.println("The execution of the Main method has started! The timer is executed with the initialization of Spring...");
  System.out.println("The execution of the Main method has ended!");
 }
}
 
The required jar package, spring.jar (now the jar package of spring is released separately, the class of the scheduled task is in spring-context.jar, but I couldn't find it after the introduction, and finally I had to download the spring.jar 2.5 version, I hope the children's shoes who know the reason tell me, thanks in advance.) commons-collection.jar, commons-logging.jar, quartz.jar, jta.jar. The version of the jar package can be decided by yourself, according to your own project needs.
 
Finally add a quartz cron expression usage
Detailed usage
field of Cron expression Allowed values ​​Special characters allowed 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, -*/  
 
 
 
 
 
 

Example:
0/5 * * * * ? : execute every 5 seconds

The "*" character is used to specify all values. For example: "*" means "every minute" in the field of minutes. The "?" character is only used in date and week fields. It is used to specify "non-explicit values". It is useful when you need to specify something via one of these two fields. Look at the example below and you will understand. The day of the month and day of the week elements, when mutually exclusive, should be indicated by setting a question mark to indicate that you do not want to set that field. 
 

The "-" character is used to specify a range. For example: "10-12" means "10 o'clock, 11 o'clock, 12 o'clock" in the hour field.

The "," character is used to specify additional values. For example: "MON, WED, FRI" means "Monday, Wednesday, Friday" in the week field.

The "/" character is used to specify the increment. For example: "0/15" in the seconds field means 0, 15, 30 and 45 seconds per minute. "5/15" in the minutes field means 5, 20, 35 and 50 of the hour. The symbol "*" before "/" (eg: */10) is equivalent to 0 before "/" (eg: 0/10). Remember the essence: each numeric field of an expression is a set with a maximum and minimum value, such as: the set of seconds and minutes fields is 0-59, the date field is 1-31, and the month field is 1- 12. The character "/" can help you to take the corresponding value in each character field. For example, when "7/6" is in the month field, it will only be triggered in July, not every June.

L is the abbreviation of 'last', which can represent the day-of-month and day-of-week fields, but has different meanings in the two fields, such as the last day of the month in the day-of-month field. If it means '7' or 'SAT' in the day-of-week field, if a number is added in front of the day-of-week field, it means the last day of the month, for example '6L' means the last day of the month a friday.

The character "W" is only allowed for date fields. This character is used to specify the most recent weekday of the date. For example: if you write "15W" in the date field, it means: the nearest working day on the 15th of this month. So, if the 15th is a Saturday, the task will trigger on the 14th. If the 15th falls on a Sunday, the task will trigger on Monday the 16th. If you fill in "1W" in the date field, even if the 1st is Saturday, the task will only be triggered on the next Monday, that is, the 3rd. The most recent working day specified by the "W" character cannot span months. The character "W" can only be used with a single numerical value, not a numerical field, such as: 1-15W is wrong.

"L" and "W" can be used in conjunction in the date field, with LW representing the weekday of the last week of the month.

The character "#" is only allowed in the week field. This character is used to specify the day of the month. For example: "6#3" means Friday of the third week of the month (6 means Friday, 3 means the third week). "2#1" means Monday of the first week of the month. "4#5" means Wednesday of the fifth week.

The character "C" is allowed in date and day fields. This character relies on a designated "calendar". That is to say, the value of this expression depends on the calculation result of the related "calendar". If there is no "calendar" associated, it is equivalent to all the contained "calendars". For example, if the date field is "5C", it means the first day in the associated "calendar", or the last 5 days of the first day of the month. The week field is "1C", which means the first day in the associated "calendar", or the day after the first day of the week, that is, the day after Sunday (Monday).

Example expression "0 0 12 * * ?" Trigger "0 15 10 ? * *" at
12:00 noon every day Trigger "0 15 10 * * ?" at 10:15 am every day "0 15 10 * * ?" Trigger at 10:15 am every day "0 15 10 * * ? *" fires "0 15 10 * * ? 2005" every day at 10:15 am "0 * 14 * * ?" every day at 10:15 am in 2005 every 1 minute between 2 pm and 2:59 pm Triggers "0 0/5 14 * * ?" every 5 minutes between 2pm and 2:55pm every day and triggers "0 0/5 14,18 * * ?" every day between 2pm and 2:55pm and Triggers "0 0-5 14 * * ?" every 5 minutes from 6:00 pm to 6:55 pm and "0 10,44 14 ? 3 WED" every 1 minute from 2:00 pm to 2:05 pm every day Trigger "0 15 10 ? *MON-FRI" at 2:10 pm and 2:44 pm on Wednesdays in March Trigger "0 15 10 15 * ?" at 10:15 am Monday-Friday at 10:15 am on the 15th of each month Trigger "0 15 10 L * ?" Trigger "0 15 10 ? * 6L" on the last day of the month at 10:15 AM Trigger "0 15 10 ? * 6L " on the last Friday of the month at 10:15 AMFired on the last Friday of every month at 10:15am from 2002 to 2005 "0 15 10 ? * 6#3" Fired on the third Friday of every month at 10:15am












 

 

<!--EndFragment-->

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327086074&siteId=291194637