spring的quartz定时任务
一、版本:
1.spring:4.1.7;
2.quartz:2.2.1;
二、基于ssm项目:
1.引入jar包:quartz-2.2.1.jar;spring所需包。
2.说明:quartz版本和spring版本要兼容,低版本的quartz集成在高版本的spring中会报错:java.lang.ClassNotFoundException: org.quartz.impl.JobDetailImpl。
其实在spring4.1.x的源码中已经有对应的说明了,原文如下:
Compatible with Quartz 2.1.4 and higher, as of Spring 4.1.
3.常见异常原因ClassNotFoundException: org.springframework.scheduling.quartz.CronTriggerBean:
在quartz 1.8.6及以前版本的时候 调度触发器 依赖的类是 org.springframework.scheduling.quartz.CronTriggerBean
在2.xx版本之后就改为了org.springframework.scheduling.quartz.CronTriggerFactoryBean
因此当你依赖2.x.x版本之后只需将调度触发器的依赖类改为 org.springframework.scheduling.quartz.CronTriggerFactoryBean即可
三、代码:
quartz文件配置applicationContext-quartz.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd"> <!-- quartz定时任务 -->
<!-- <bean id="marketTest" class="com.test.www.web.timeTask.MarketMaintainEmailRemind"> </bean> -->
<!--第四步 把定义好的任务放到调度(Scheduler)工厂里面,注意这里的ref bean -->
<bean id="schedulerFactoryBean3"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="applicationContextSchedulerContextKey" value="applicationContext"/>
<property name="triggers">
<list>
<ref bean="printTimerTrigger10" />
</list>
</property>
</bean>
<!-- CronTriggerBean -->
<bean id="printTimerJob10"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="marketMaintainEmailRemind" /> <!-- marketMaintainEmailRemind marketTest -->
<property name="targetMethod" value="run" />
<property name="concurrent" value="false"/>
</bean>
<bean id="printTimerTrigger10" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="printTimerJob10" />
<property name="cronExpression" value="0 54 12 * * ?" />
</bean> </beans>
在spring的配置文件applicationContext.xml中引入配置文件applicationContext-quartz.xml:
<import resource="applicationContext-quartz.xml" />
定时类:
package com.test.www.web.timeTask; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.test.www.util.PrimaryKeyGenerator;
import com.test.www.web.entity.user.User;
import com.test.www.web.service.user.UserService; //注:MarketMaintainEmailRemind类注册bean默认名称为类名且首字母小写,可通过注解方式自定义名称修改bean名称
@Component //@Service("marketTest") @Component("marketTest")
public class MarketMaintainEmailRemind {
@Resource
private UserService userService; public void run() { try {
System.out.println("aa");
User user = new User();
user.setId(PrimaryKeyGenerator.getLongKey());
user.setUsername("定时任务Test");
user.setPassword("dingshirenwuTest");
userService.insertUser(user);
} catch (Exception e) {
e.printStackTrace();
}
} }
完成后,设定quartz中的时间,等待触发。时间一到就会执行MarketMaintainEmailRemind中的run方法。执行结果如下图所示:

spring的quartz定时任务的更多相关文章
- Spring整合Quartz定时任务执行2次,Spring定时任务执行2次
Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...
- Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)
Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境) 转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...
- (2)Spring集成Quartz定时任务框架介绍和Cron表达式详解
在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
原文地址:http://www.cnblogs.com/obullxl/archive/2011/07/10/spring-quartz-cron-integration.html 在JavaEE系统 ...
- Spring整合Quartz定时任务 在集群、分布式系统中的应用
概述 虽然单个Quartz实例能给予你很好的Job调度能力,但它不能满足典型的企业需求,如可伸缩性.高可靠性满足.假如你需要故障转移的能力并能运行日益增多的 Job,Quartz集群势必成为你应用的一 ...
- Spring集成Quartz定时任务框架介绍
在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...
- Spring之Quartz定时任务和Cron表达式详解
1.定时业务逻辑类 public class ExpireJobTask { /** Logger */ private static final Logger logger = LoggerFact ...
- Spring集成Quartz定时任务
1.导入jar包 2.配置applicationContext.xml文件 <!-- 任务调度1 --> <!-- bean id="simpleJob" cla ...
- Spring MVC+Quartz 定时任务持久化
请自行参考: http://sloanseaman.com/wordpress/2011/06/06/spring-and-quartz-and-persistence/ https://object ...
随机推荐
- hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Linux基础01
** 一些老生常谈的问题 一提起Linux,行业内无人不知<鸟哥私房菜>,就是放在胸口可以防弹的那种书,虽说经典.全面,但对于初学者而言,确实过于厚重,而且容易学着后边忘了前边,毕竟实际操 ...
- (转载)项目实战工具类(一):PhoneUtil(手机信息相关)
项目实战工具类(一):PhoneUtil(手机信息相关) 可以使用的功能: 1.获取手机系统版本号 2.获取手机型号 3.获取手机宽度 4.获取手机高度 5.获取手机imei串号 ,GSM手机的 ...
- js中深拷贝代码实现
function copy(original,o){ if(typeof original != 'object') return original; var o = o || (Array.isAr ...
- UVa 1638 Pole Arrangement【递推】
题意:给出n根高度为1,2,3,---n的杆子,从左边能看到l根,右边能够看到r根,问有多少种可能 看的紫书的思路 先假设已经安排好了高度为2---i的杆子, 那么高度为1的杆子的放置方法有三种情况 ...
- NetworkX-根据权重画图
load_data = sio.loadmat(load_path) #阈值处理 mat=np.array(load_data['R']) mat[mat<0]=0 mat[mat<0.4 ...
- 注解@SuppressWarnings
在JAVA中注解@SuppressWarnings("deprecation")的Deprecation是什么意思 过期的 @SuppressWarnings("depr ...
- javascript中实现继承的几种方式
javascript中实现继承的几种方式 1.借用构造函数实现继承 function Parent1(){ this.name = "parent1" } function Chi ...
- 小程序QQ版表情解析组件
代码片段: [https://developers.weixin.qq.com/s/KLaD5MmD7V45) GitHub: https://github.com/WozHuang/Miniprog ...
- 解决new Date的值为Invalid Date、NaN-NaN的问题
错误代码: let timespan = 1515239514230; let dateTime = new Date(timespan); console.log(dateTime) // 返回 I ...