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 ...
随机推荐
- NHibernate3剖析:Query篇之NHibernate.Linq增强查询
系列引入 NHibernate3.0剖析系列分别从Configuration篇.Mapping篇.Query篇.Session策略篇.应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种 ...
- 屌丝也能开发安卓版2048(App Inventor)
想编写安卓游戏.java太难.来试试App Inventor.尽管有人觉得他是中学生的玩具,可是也能编写2048这种火爆游戏,不须要太复杂的算法. 整个游戏有几个模块: 一.游戏初始化 数列转化为图形 ...
- 【React Native开发】React Native控件之ProgressBarAndroid进度条解说(12)
),React Native技术交流4群(458982758).请不要反复加群! 欢迎各位大牛,React Native技术爱好者增加交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文 ...
- 【App 开发框架 - App Framework】
http://edm.mcake.com/mark/jqmboi/#plugins 官网:http://app-framework-software.intel.com/index.php 官方API ...
- 查看typedef类型
typedef unsigned long int NUM; #include <iostream> using namespace std; NUM x; cout << t ...
- 【英雄会】微软题目:几个bing
今天是元旦,开篇先祝福大家在新的一年心想事成,工作顺利,开心生活每一天 . 看到[英雄会]上出现了微软出的题目:几个bing,题目内容如下: 本届大赛由微软必应词典冠名,必应词典(Bing Dicti ...
- JAR包放在WEB-INF/lib子目录报ClassNotFoundException解决方案
对于Java Web应用依赖的jar包,我们通常会放到WEB-INF/lib目录下,但是笔者喜欢把不同框架的jar包放在不同的子目录下,例如新建一个struts目录存放struts框架的jar包等. ...
- A. Amr and Music
解题思路:给出n种乐器学习所需要的时间,以及总共的天数, 问最多能够学多少门乐器,并且输出这几门乐器在原序列中的序号(不唯一) 按照升序排序,为了学到最多的乐器,肯定要选择花费时间最少的来学习 然后用 ...
- oracle根据成绩排名查询某个名次段的人员
先说一下表结构 名字name 分数fenshu 表名test1,以下查询的是成绩排名为第三名和第四名,这个模板让你查随意排名段的人 select name,fenshu,mc from (se ...
- java zyUpload 实现多文件上传
1.html部分 <form enctype="multipart/form-data"> <label>请选择文件</label> <i ...