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 ...
随机推荐
- HDU - 4758 Walk Through Squares (AC自己主动机+DP)
Description On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...
- 研读:AirBag Boosting Smartphone Resistance to Malware Infection
- scikit-learn:3.5. Validation curves: plotting scores to evaluate models
參考:http://scikit-learn.org/stable/modules/learning_curve.html estimator's generalization error can b ...
- bzoj3993: [SDOI2015]星际战争(网络流)
3993: [SDOI2015]星际战争 题目:传送门 题解: 洛谷AC了,但是因为bzoj的spj有问题所以暂时没A 一道老题目了,二分时间然后网络流判断. 每次st-->武器连时间*攻击力 ...
- iOS:简单使用UIAlertVIew和UIActionSheet
做iOS开发的同学想必都用过UIAlertVIew或者UIActionSheet.UIAlertVIew 可以弹出一个出现在屏幕中间的提示视图,给用户展示信息,并让用户自己选择操作,UIActionS ...
- zzulioj--1791-- 旋转矩阵(模拟水题)
旋转矩阵 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 268 Solved: 116 SubmitStatusWeb Board Descr ...
- Redis-3-string类型
Redis-3-string类型 标签(空格分隔): redis set key value [ex 秒数] / [px 毫秒数] [nx] /[xx] mset key value key valu ...
- winform控件命名规范对照表
WinForm Control 命名规范 数据类型 数据类型简写 标准命名举例 Label lbl lblMessage LinkLabel llbl llblToday Button btn btn ...
- 编程语言与Python学习(一)
1.1 编程与编程语言 1.1.1 编程语言 计算机的发明,是为了用机器解放人力,而编程的目的则是将人类的思想流程按照某种能够被计算机识别的表达方式传递给计算机,从而达到让计算机能够像人脑一样自动执行 ...
- Windows 安装PostgreSQL
下载二进制包:https://www.enterprisedb.com/download-postgresql-binaries 直接解压到C盘 Microsoft Windows [版本 6.3.9 ...