Configure the tutorial for Quartz in Java's Spring framework

  • 2020-05-09 18:31:37
  • OfStack

The process of configuring Quartz in Spring:

1. Import the JAR package

The JAR package required by quartz is already included in spring and is located under quartz-all-1.6.1.jar under \lib\quartz in the spring unzipped directory.

Just copy it to the WEB-INF /lib section of the project.


2. Configure web.xml to load the quartz configuration file when spring starts


<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
  <!-- spring --> 
  <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:application-*.xml</param-value> 
  </context-param> 
 
  <!-- spring Listening to the  --> 
 
  <listener> 
    <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
  </listener> 
</web-app> 


3. Write quartz configuration files: application-quartz.xml these configuration files can be written in other spring configuration files


<?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: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.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
 
  <!--  Task entity  --> 
  <bean id="testTaskBean" class="com.jp.task.TestTask" /> 
 
  <!--  Update the policy mandate methodology on a regular basis  --> 
  <bean id="testTaskTimerMethod" 
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
    <property name="targetObject" ref="testTaskBean" /> 
    <!-- targetMethod  Configure the method name for timed execution  --> 
    <property name="targetMethod" value="executeAction" /> 
    <property name="concurrent" value="false" /> 
  </bean> 
 
  <!--  Periodically update policy triggers  --> 
  <bean id="testTaskTrigger" 
    class="org.springframework.scheduling.quartz.CronTriggerBean"> 
    <property name="jobDetail" ref="syncPolicyTaskTimerMethod" /> 
        <!--  each 3 Minutes of the first 0 Seconds to perform  --> 
    <property name="cronExpression" value="0 0/3 * * * ?" /> 
  </bean> 
 
  <!--  Customize the task list  --> 
  <bean id="scheduler" 
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
    <property name="triggers"> 
      <list> 
        <ref bean="testTaskTrigger" /> 
      </list> 
    </property> 
  </bean> 
</beans> 

4. Write the JAVA class TestTask to perform the task


package com.jp.task; 
  
import org.quartz.JobExecutionContext; 
import org.quartz.JobExecutionException; 
import org.springframework.scheduling.quartz.QuartzJobBean; 
 
 
public class TestTask extends QuartzJobBean{ 
   
  Logger log = Logger.getLogger( PolicyServiceTest.class ); 
     
  public void executeAction(){ 
    log.info("Hello quartz"); 
  } 
 
  @Override 
  protected void executeInternal(JobExecutionContext arg0) 
      throws JobExecutionException { 
     
  } 
 
} 

5. All the work is ready, 1 run. It's basically wrong. Something like NoSuchMethod.
This is due to the low commons-collections. jar version. commons-collections-3.2.jar or above.

When you look for a project, there is an commons-collections-3.2.jar. What's going on? Look again and you'll find an commons-collections-2.1.1. in hibernate.jar.

Just delete commons-collections-2.1.1.. jar.


PS:cronExpression -- Cron expression description

Second   Points   Hours   Month date   Month   Week date   Year (optional field)   The special character  

Cron triggers make use of 1 series of special characters, as shown below:  

The backslash (/) character represents the increment value. For example, in the second field "5/15" means starting at the fifth second, once every 15 seconds.  

Question mark (?) The character and letter L characters are only available in the month date and week date fields. The question mark indicates that the field does not contain a specific value. So, if you specify a month date, you can insert "?" in the week date field. , indicating that the date value of the week is irrelevant. The letter L character is short for last. Placed in the month date field, indicating that execution is scheduled on the last day of the month. In the week date field, if "L" exists alone, it is equal to "7", otherwise it represents the last instance of the week date in the month. Therefore, "0L" means that the execution is arranged on the last Sunday of the month.  

The letter (W) character in the month date field schedules the execution on the business day closest to the specified value. Place "1W" in the month date field to indicate that execution is scheduled for the first business day of the month.  

The hashtag (#) character specifies a specific working day instance for a given month. Put "MON#2" in the week date field to indicate that the task is scheduled for the second week of the month.  

The asterisk (*) character is a wildcard, indicating that the field can accept any possible value.  
Special characters    :
Seconds 0-59, minus * /    
0 minus 59, minus * /    
Hour 0-23, - * /    
Dates 1-31, - * ? / L W C    
Month 1-12 or JAN-DEC, - * /    
Week 1-7 or SUN-SAT. / L C #    
Year (optional) left blank, 1970-2099, - * /  
The expression means    
"0, 0, 12 * * ?"     is triggered every day at 12 noon
"0 15 10 & # 63; * *" triggers     at 10:15 am every day
"0, 15, 10 * * ?"     is triggered at 10:15 am every day
"0, 15, 10 * * ? *"     is triggered at 10:15 am every day
"0, 15, 10 * * ? "In 2005,     was triggered every day at 10:15 am
"0 * 14 * * ?"     is triggered every minute between 2 p.m. and 2:59 p.m. every day
"0, 0, 5, 14 * * ?"     is triggered every 5 minutes between 2 p.m. and 2:55 p.m. every day
"0 0/5 14,18 * * ?"     is triggered every 5 minutes between 2 p.m. and 2:55 p.m. and between 6 p.m. and 6:55 p.m. every day
"0, 0, minus 5, 14 * * ?"     is triggered every minute between 2 p.m. and 2:05 p.m. every day
"0 10 44 14 & # 63; 3 WED" triggers     at 2:10 p.m. and 2:44 p.m. on Wednesday of march each year
"0 15 10 & # 63; * MON-FRI" triggers     at 10:15 am on weekdays 1 to 5
"0, 15, 10, 15 * ?"     is triggered at 10:15 am on the 15th of each month
"0 15 10 L * ?"     is triggered at 10:15 a.m. on the last day of each month
"0 15 10 & # 63; * 6L" triggers     at 10:15 am on the 5th of the last week of each month
"0 15 10 & # 63; * 6L 2002-2005" trigger     at 10:15 am on the last Friday of the last week of each month from 2002 to 2005
"0 15 10 & # 63; * 6#3" triggers     at 10:15 am on the 5th day of the 3rd week of each month
Every morning at 6 o 'clock  

0 6 * * *  

  every two hours

0 */2 * * *    
Every two hours between 11pm and 8am, 8am  

0 23 minus 7/2, 8 * * *  

  is available on the 4th of the month and every week from 1 to 3 am

0 11 4 * 1-3    
4 a.m. on January 1,  

0 4 1 1 *


Related articles: