Spring整合Quartz (cronTrigger和simpleTrigger实现方法)


之前有记录过一次springboot整合Quartz的文章,由于偶尔一次自己使用spring需要整合Quartz时有遇到一些异常,导致定时job无法正常执行,所以在此也记录一下spring整合quartz的两种实现方式。SpringBoot整合Quartz

应用框架版本

Spring 5.1.4.RELEASE

Quartz 2.3.0

Maven 3.5.0

Demo目录结构

web.xml配置

<!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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Spring.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"> <bean id="taskServiceBean" class="com.test.quartz.service.TaskService"></bean> <!--************************************************************指定时间间隔定时执行***********************************************************--> <!-- 要执行任务的任务类。 -->
<bean id="timerTaskQuartz" class="com.test.quartz.timer.TimerTask">
<property name="taskService" ref="taskServiceBean"></property>
</bean> <!-- 将需要执行的定时任务注入JOB中。 -->
<bean id="timerTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="timerTaskQuartz"></property>
<!-- 任务类中需要执行的方法 -->
<property name="targetMethod" value="run"></property>
<!-- 上一次未执行完成的,要等待有再执行。 -->
<property name="concurrent" value="true"></property>
</bean> <!-- 基本的定时器,会绑定具体的任务。 -->
<bean id="timerTaskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="timerTaskJob"></property>
<!--延时启动-->
<property name="startDelay" value="3000"></property>
<!--执行频率-->
<property name="repeatInterval" value="3000"></property>
</bean> <bean id="timerTaskFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="timerTaskTrigger"></ref>
</list>
</property>
</bean> <!--************************************************************使用cron表达式在指定时间执行***********************************************************--> <bean name="timeTask2" class="com.test.quartz.timer.TimeTask2">
<property name="taskService" ref="taskServiceBean"></property>
</bean>
<bean id="timerTaskJob2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 执行的类 -->
<property name="targetObject">
<ref bean="timeTask2" />
</property>
<!-- 类中的方法 -->
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="timerTaskJob2" />
</property>
<!-- 每一秒钟执行一次 -->
<property name="cronExpression">
<value>0/1 * * * * ?</value>
</property>
</bean> <bean id="timerTask2FactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"></ref>
</list>
</property>
</bean> </beans>

pom.xml依赖

在使用quartz版本时需要注意与spring的版本对应。

spring 3.0版本内置的是Quartz<2.0的版本,所以在使用Spring 3.0版本以上时,需要使用Quartz 2.0以上的版本

<?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>spring-quartz</groupId>
<artifactId>com.test.quartz</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <name>com.test.quartz Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>5.1.4.RELEASE</spring.version>
<quartz.version>2.3.0</quartz.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
</dependencies> <build>
<finalName>com.test.quartz</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

TimerTask.java

package com.test.quartz.timer;

import com.test.quartz.service.TaskService;

/**
* @author yd
* @date 2019-06-11 16:08
* @describe 定时job1
*/
public class TimerTask { private TaskService taskService; public TaskService getTaskService() {
return taskService;
} public void setTaskService(TaskService taskService) {
this.taskService = taskService;
} public void run(){
String process = taskService.process(this.getClass().getSimpleName());
System.out.println(process);
} }

TimeTask2.java

package com.test.quartz.timer;

import com.test.quartz.service.TaskService;

/**
* @author yd
* @date 2019-06-11 16:28
* @describe 定时job2
*/
public class TimeTask2 { private TaskService taskService; public TaskService getTaskService() {
return taskService;
} public void setTaskService(TaskService taskService) {
this.taskService = taskService;
} public void run(){
String process = taskService.process(this.getClass().getSimpleName());
System.out.println(process);
}
}

service

在对quartz集成的同时,在这里也引入了service层的注入,demo中只是一个简单的样例,可以根据自己的实际需求去对service层进行编辑。

TaskService.java

package com.test.quartz.service;

import org.springframework.stereotype.Service;

/**
* @author yd
* @date 2019-06-11 16:11
* @describe 文件描述
*/ @Service
public class TaskService { public String process(String taskname){
return this.getClass().getSimpleName()+"在处理任务:"+taskname;
}
}

执行结果

Spring整合Quartz (cronTrigger和simpleTrigger实现方法)的更多相关文章

  1. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  2. Spring4定时器 cronTrigger和simpleTrigger实现方法

    spring4定时器 cronTrigger和simpleTrigger实现方法 Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.Quartz 允许 ...

  3. Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  4. spring整合quartz并持久化

    spring整合quartz有两种方式: 一.常见是使用配置文件,将定时任务保存到内存中 简单示例: <!-- 短信催还提醒任务调度 --> <bean id="overd ...

  5. spring整合quartz时间任务调度框架

    spring整合quartz框架 1.创建maven工程 2.导入jar包(pom.xml) <dependencies> <dependency> <groupId&g ...

  6. Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)

    Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境)   转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...

  7. 初识quartz 并分析 项目中spring整合quartz的配置【原创+转载】

    初识quartz 并分析 项目中spring整合quartz的配置[原创+转载]2018年01月29日 12:08:07 守望dfdfdf 阅读数:114 标签: quartz 更多个人分类: 工具 ...

  8. Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入

    Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入 Spring4整合quartz2.2.3中Job任务使用@Autowired不能注入 >> ...

  9. 使用spring整合Quartz实现—定时器

    使用spring整合Quartz实现—定时器(Maven项目做演示) 不基于特定的基类的方法 一,开发环境以及依赖的jar包 Spring 4.2.6.RELEASE Maven 3.3.9 Jdk ...

随机推荐

  1. Linux系统之《消息队列》入手应用

    目录 简述 代码 编译 运行 简述 消息队列是Linux进程间通信方式之一,消息队列一般是用于简单的通信,数据量不大,通信不频繁的情况.如果交互频繁或者数据量大就不适合了. 代码 下面直接上代码,发送 ...

  2. 关键字Run Keyword If 如何写多个条件语句、如何在一个条件下执行多个关键字

    Run Keyword If 关键字给出的示例是: 但是,这往往不能满足我们实际需要,比如,我们需要同时判断多个条件是否成立,或者在条件成立时我们想要执行多个关键字,虽然可以进行封装再调用,但是比较麻 ...

  3. JS笔记 语法

    javascript概述 简称为JS,是一款能够运行在JS解释器.引擎中的脚本语言 JS解释器.引擎 JS的运行环境 1.独立安装的js解释器 -nodeJS 2.嵌入在浏览器中的js解释器 JS基于 ...

  4. 使用Luhn算法实现信用卡号验证

    问题描述: 2:信用卡号的验证 [信用卡号的验证] 当你输入信用卡号码的时候,有没有担心输错了而造成损失呢?其实可以不必这么 担心,因为并不是一个随便的信用卡号码都是合法的,它必须通过 Luhn 算法 ...

  5. java循环嵌套与跳转语句(break,continue)

    一 循环嵌套 嵌套循环是指在一个循环语句的循环体中再定义一个循环语句的语法结构.while.do…while. for循环语句都可以进行嵌套,并且它们之间也可以互相嵌套,如最常见的在for循环中嵌套f ...

  6. Django-model查询[为空、由某字符串开头、由某字符串结尾、包含某字符串],__isnull、__starswith、__endswith、__contains

    使用属性+__isnull就可以判断此字段为空 a = DatasClass.objects.filter(name__isnull=True) 使用属性+__startswith可以判断属性由某字符 ...

  7. 2020-05-08:mycat部署数据库集群的时候 遇到了哪些坑

    福哥答案2020-05-08:答案仅供参考,来自群员 使用activity时,连接mycat设置进去的序列化的流程变量,反序列化会报错这个类型字段类型是blob类型,mycat对这种类型处理时有点问题

  8. C#LeetCode刷题之#21-合并两个有序链表(Merge Two Sorted Lists)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3818 访问. 将两个有序链表合并为一个新的有序链表并返回.新链表 ...

  9. 【luogu4137】 Rmq Problem / mex - 莫队

    题目描述 有一个长度为n的数组{a1,a2,…,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. 思路 莫队水过去了 233 #include <bits/stdc++.h> ...

  10. pytest封神之路第一步 tep介绍

    『 tep is a testing tool to help you write pytest more easily. Try Easy Pytest! 』 tep前身 tep的前身是接口自动化测 ...