Spring整合Quartz (cronTrigger和simpleTrigger实现方法)
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实现方法)的更多相关文章
- 使用Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- Spring4定时器 cronTrigger和simpleTrigger实现方法
spring4定时器 cronTrigger和simpleTrigger实现方法 Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.Quartz 允许 ...
- Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- spring整合quartz并持久化
spring整合quartz有两种方式: 一.常见是使用配置文件,将定时任务保存到内存中 简单示例: <!-- 短信催还提醒任务调度 --> <bean id="overd ...
- spring整合quartz时间任务调度框架
spring整合quartz框架 1.创建maven工程 2.导入jar包(pom.xml) <dependencies> <dependency> <groupId&g ...
- Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)
Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境) 转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...
- 初识quartz 并分析 项目中spring整合quartz的配置【原创+转载】
初识quartz 并分析 项目中spring整合quartz的配置[原创+转载]2018年01月29日 12:08:07 守望dfdfdf 阅读数:114 标签: quartz 更多个人分类: 工具 ...
- Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入
Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入 Spring4整合quartz2.2.3中Job任务使用@Autowired不能注入 >> ...
- 使用spring整合Quartz实现—定时器
使用spring整合Quartz实现—定时器(Maven项目做演示) 不基于特定的基类的方法 一,开发环境以及依赖的jar包 Spring 4.2.6.RELEASE Maven 3.3.9 Jdk ...
随机推荐
- 基于boost的bind与function的一个简单示例消息处理框架
前两年开始接触boost,boost库真是博大精深:今天简单介绍一下boost中之前用到的的bind与function,感觉挺实用的,分享给大家,我对boost用的也不多,让大家见笑了. 上次文发了一 ...
- SkyWalking APM8.1.0 搭建与项目集成使用
SkyWalking介绍 SkyWalking是什么? SkyWalking是一个可观测性分析平台和应用性能管理系统,提供分布式跟踪.服务网格遥测分析.度量聚合和可视化一体化解决方案,并支持多种开发语 ...
- 关于css布局中,inline-block元素间隙的处理方法
关于inline-block元素间隙的处理 参考橱窗外的小孩,原文链接https://www.cnblogs.com/showcase/p/10469361.html 如下,两个inline-bloc ...
- Go之Gorm简介及使用案例
简介 ORM Object-Relationl Mapping, 它的作用是映射数据库和对象之间的关系,方便我们在实现数据库操作的时候不用去写复杂的sql语句,把对数据库的操作上升到对于对象的操作 G ...
- web新手第二周知识汇总
这周学习了盒模型以及一些定位的知识,现在简单做下汇总 盒模型组成部分: ie浏览器默认值是border-box content(内容盒)蓝色 padding(内容和边框的距离 绿色 填充盒包含内容)b ...
- ElasticSearch在CentOS的安装
ElasticSearch在CentOS的安装 一.tar包安装 单机安装 创建elastic用户,ElasticSearch不支持root用户运行 useradd elastic 上传文件到 /so ...
- error PRJ0003 : 生成“cmd.exe”时出错 2010-01-19 22:26
今天用vs2005编译时代码时竟然出现了error PRJ0003 : 生成“cmd.exe”时出错这样的错误,这不是刺激我吗,我们先看msdn的解释吧. 错误消息 生成“command line”时 ...
- 【算法•日更•第十期】树型动态规划&区间动态规划:加分二叉树题解
废话不多说,直接上题: 1580:加分二叉树 时间限制: 1000 ms 内存限制: 524288 KB提交数: 121 通过数: 91 [题目描述] 原题来自:NOIP 20 ...
- 自建本地服务器,自建Web服务器——保姆级教程!
搭建本地服务器,Web服务器--保姆级教程! 本文首发于https://blog.chens.life/How-to-build-your-own-server.html. 先上图!大致思路就是如此. ...
- linux驱动之内核多线程(二)
本文摘自http://www.cnblogs.com/zhuyp1015/archive/2012/06/11/2545702.html 内核多线程是在项目中使用到,自己也不熟悉,遇到一个很囧的问题, ...