1.新建web项目
2.导入依赖
3.配置web.xml
实现方式有2种:
第一种:
1.新建job类继承抽象类重写executeInternal方法
2.在配置文件中配置jobDetail1属性
3.配置trigger,并配置trigger对应的JobDetail
4.将trigger注册到schedule中
5.运行web服务,观察Quartz定时任务
注:
配置jobDetail1属性用的是 org.springframework.scheduling.quartz.JobDetailFactoryBean
第二种:
1.新建job类 自定义方法
2.在配置文件中配置jobDetail1属性
3.配置trigger,并配置trigger对应的JobDetail
4.将trigger注册到schedule中
5.运行web服务,观察Quartz定时任务
注:
配置jobDetail1属性用的是org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
再说一下trigger配置方式有2种:
第一种:简单配置
org.springframework.scheduling.quartz.SimpleTriggerFactoryBean
第二种:简单配置
org.springframework.scheduling.quartz.CronTriggerFactoryBean
实现方式推荐使用第二种,因为比较灵活
trigger配置方式推荐使用第二种
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-quartz</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-quartz</name>
<url>http://www.gblfy.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<spring.version>5.2.8.RELEASE</spring.version>
</properties>
<dependencies>
<!--spring 相关依赖 start-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring 相关依赖 end-->
<!--quartz 依赖-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<build>
<finalName>spring-quartz</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<path>/</path>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
使用JobDetailFactoryBean 继承QuartzJobBean抽象类
package com.gblfy.job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.time.LocalTime;
/**
* 第1种 实现方式
*/
public class MyJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
LocalTime localTime = LocalTime.now();
System.out.println("我MyJob正在执行"+localTime.toString());
}
}
spring-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--第1种-->
<bean id="jobDetail1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.gblfy.job.MyJob"/>
<property name="name" value="myJob1"/>
<property name="group" value="group1"/>
</bean>
<!--触发器1-->
<bean id="tigger1" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail1"/>
<property name="name" value="tigger1"/>
<property name="group" value="group1"/>
<property name="repeatInterval" value="5000"/>
</bean>
<!--将触发器注册到 schedule-->
<bean id="schedule" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="tigger1"/>
</list>
</property>
</bean>
</beans>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
在第一种实现方式的基础上,修改第3和4步即可
使用MethodInvikingJobDetailFactoryBean(比较灵活)
创建job类
package com.gblfy.job;
import java.time.LocalTime;
/**
* 第2种 实现方式
*/
public class MyJob2 {
/**
*
*/
public void excute() {
LocalTime localTime = LocalTime.now();
System.out.println("我MyJob2正在执行" + localTime.toString());
}
/**
*
*/
// public static void excute() {
// LocalTime localTime = LocalTime.now();
// System.out.println("我MyJob2正在执行" + localTime.toString());
// }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--第2种-->
<bean id="myJob2" class="com.gblfy.job.MyJob2"></bean>
<bean id="jobDetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="name" value="myJob2"/>
<property name="group" value="group1"/>
<property name="targetObject" ref="myJob2"/>
<property name="targetMethod" value="excute"/>
</bean>
<!--触发器2-->
<bean id="tigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail2"/>
<property name="name" value="tigger2"/>
<property name="group" value="group1"/>
<property name="cronExpression" value="0/8 * * * * ?"/>
</bean>
<!--将触发器注册到 schedule-->
<bean id="schedule" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="tigger2"/>
</list>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--第1种-->
<bean id="jobDetail1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!--指定执行的job类-->
<property name="jobClass" value="com.gblfy.job.MyJob"/>
<!--下面2个属性拼在一起 指定执行job唯一标识
可以理解为:哪个组的哪个任务-->
<property name="name" value="myJob1"/>
<property name="group" value="group1"/>
</bean>
<!--第2种-->
<bean id="myJob2" class="com.gblfy.job.MyJob2"></bean>
<bean id="jobDetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--下面2个属性拼在一起 指定执行job唯一标识
可以理解为:哪个组的哪个任务-->
<property name="name" value="myJob2"/>
<property name="group" value="group1"/>
<!--指定执行的job的类-->
<property name="targetObject" ref="myJob2"/>
<!--如果执行的job类中的方法为静态
简言之:static 修饰excute方法 使用-->
<!-- <property name="targetClass" value="com.gblfy.job.MyJob2"/>-->
<!--指定执行的job的类中的方法 非静态-->
<property name="targetMethod" value="excute"/>
</bean>
<!--触发器1-->
<bean id="tigger1" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<!--配置jobDetail 属性信息-->
<property name="jobDetail" ref="jobDetail1"/>
<!--指定触发器-->
<property name="name" value="tigger1"/>
<!--指定组信息 -->
<property name="group" value="group1"/>
<!--触发规则 间隔5秒执行一次-->
<property name="repeatInterval" value="5000"/>
</bean>
<!--触发器2-->
<bean id="tigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!--配置jobDetail 属性信息-->
<property name="jobDetail" ref="jobDetail2"/>
<!--指定触发器-->
<property name="name" value="tigger2"/>
<!--组信息 -->
<property name="group" value="group1"/>
<!--触发规则 间隔8秒执行一次-->
<property name="cronExpression" value="0/8 * * * * ?"/>
</bean>
<!--将触发器注册到 schedule-->
<bean id="schedule" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--配置触发器 列表-->
<ref bean="tigger1"/>
<ref bean="tigger2"/>
</list>
</property>
</bean>
</beans>