Home > Java > javaTutorial > Tutorial on configuring Quartz in Java's Spring framework

Tutorial on configuring Quartz in Java's Spring framework

高洛峰
Release: 2017-01-23 10:58:31
Original
1519 people have browsed it

The process of configuring Quartz in Spring:

1. Import the JAR package

The JAR package required by quartz is already included in spring, and its location is \lib\quartz in the directory after spring is decompressed quartz-all-1.6.1.jar under

Just copy it to the WEB-INF/lib 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监听 -->
  
  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
</web-app>
Copy after login

3. Write the quartz configuration file: application-quartz.xml. In fact, these configurations are completely fine. 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">
  
  <!-- 任务实体 -->
  <bean id="testTaskBean" class="com.jp.task.TestTask" />
  
  <!-- 定时更新政策任务方法 -->
  <bean id="testTaskTimerMethod"
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="testTaskBean" />
    <!-- targetMethod 配置定时执行的方法名 -->
    <property name="targetMethod" value="executeAction" />
    <property name="concurrent" value="false" />
  </bean>
  
  <!-- 定时更新政策触发器 -->
  <bean id="testTaskTrigger"
    class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="syncPolicyTaskTimerMethod" />
        <!-- 每个3分钟的第0秒执行 -->
    <property name="cronExpression" value="0 0/3 * * * ?" />
  </bean>
  
  <!-- 订制任务列表 -->
  <bean id="scheduler"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
      <list>
        <ref bean="testTaskTrigger" />
      </list>
    </property>
  </bean>
</beans>
Copy after login

4. Write the JAVA class TestTask

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 {
      
  }
  
}
Copy after login

5 .All work is ready and ready to run. Basically, an error was reported. There are errors like NoSuchMethod.
This is caused by the commons-collections.jar version being too low. Require commons-collections-3.2.jar or above.

At this time, there is commons-collections-3.2.jar in the project you are looking for. What's going on? If you search again, you will find commons-collections-2.1.1.jar in hibernate.

Just delete commons-collections-2.1.1.jar.


PS:cronExpression——Cron expression description

Seconds

Minutes

Hours

Date within the month

Month

Date in the week

Year (optional field)

Special characters

Cron trigger uses a series of special characters, as follows Shown:

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

The question mark (?) character and the letter L character are only available in the day-of-month and day-of-week fields. The question mark indicates that this field does not contain a specific value. So, if you specify a day within the month, you can insert a "?" in the day of the week field to indicate that the day of the week value does not matter. The letter L character is short for last. Put it in the date-in-month field to schedule execution on the last day of the month. In a day-of-the-week field, "L" equals "7" if present by itself, otherwise it represents the last instance of a day-of-the-week within the month. So "0L" means it is scheduled to be executed 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. Put "1W" in the month date field to schedule execution on the first working day of the month.

The hash sign (#) character specifies a specific working day instance for a given month. Put "MON#2" in the day-of-week field to schedule the task on the second Monday of the month.

The asterisk (*) character is a wildcard character, indicating that the field can accept any possible value.
Special characters allowed for field allowed values:
Seconds 0-59, - * /
Minutes 0-59, - * /
Hours 0-23, - * /
Date 1- 31, - * ? / L W C
Month 1-12 or JAN-DEC, - * /
Week 1-7 or SUN-SAT, - * ? / L C
#Year (optional) Leave blank , 1970-2099, - * /
Expression meaning
"0 0 12 * * ?" Triggered at 12 noon every day
"0 15 10 ? * *" Triggered at 10:15 am every day
"0 15 10 * * ?" Triggered every day at 10:15 AM
"0 15 10 * * ? *" Triggered every day at 10:15 AM
"0 15 10 * * ? 2005" Triggered every day at 10 AM in 2005 :15 triggers
"0 * 14 * * ?" Triggers every 1 minute from 2 pm to 2:59 pm every day
"0 0/5 14 * * ?" From 2 pm to pm every day Trigger every 5 minutes during 2:55
"0 0/5 14,18 * * ?" Trigger every 5 minutes between 2 pm to 2:55 and 6 pm to 6:55 every day
"0 0-5 14 * * ?" Triggered every 1 minute from 2 pm to 2:05 pm every day
"0 10,44 14 ? 3 WED" Every Wednesday at 2:10 pm in March and 2:44 triggers
"0 15 10 ? * MON-FRI" triggers at 10:15 am from Monday to Friday
"0 15 10 15 * ?" triggers at 10:15 am on the 15th of every month
"0 15 10 L * ?" Triggered at 10:15 am on the last day of each month
"0 15 10 ? * 6L" Triggered at 10:15 am on the last Friday of each month
"0 15 10 ? * 6L 2002-2005" Triggered at 10:15 am on the last Friday of each month from 2002 to 2005
"0 15 10 ? * 6#3" Triggered at 10:15 am on the third Friday of each month
Every morning at 6 o'clock

0 6 * * *

Every two hours

0 */2 * * *
Between 11 pm and 8 am Every two hours, 8 a.m.

0 23-7/2, 8 * * *

on the 4th of each month and every Monday to Wednesday morning 11 o'clock

0 11 4 * 1-3
January 1st at 4 o'clock in the morning

0 4 1 1 *

More in the Spring framework of Java For articles related to tutorials on configuring Quartz, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template