一、版本:

  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定时任务的更多相关文章

  1. Spring整合Quartz定时任务执行2次,Spring定时任务执行2次

    Spring整合Quartz定时任务执行2次,Spring定时任务执行2次 >>>>>>>>>>>>>>>&g ...

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

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

  3. (2)Spring集成Quartz定时任务框架介绍和Cron表达式详解

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...

  4. Spring集成Quartz定时任务框架介绍和Cron表达式详解

    原文地址:http://www.cnblogs.com/obullxl/archive/2011/07/10/spring-quartz-cron-integration.html 在JavaEE系统 ...

  5. Spring整合Quartz定时任务 在集群、分布式系统中的应用

    概述 虽然单个Quartz实例能给予你很好的Job调度能力,但它不能满足典型的企业需求,如可伸缩性.高可靠性满足.假如你需要故障转移的能力并能运行日益增多的 Job,Quartz集群势必成为你应用的一 ...

  6. Spring集成Quartz定时任务框架介绍

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...

  7. Spring之Quartz定时任务和Cron表达式详解

    1.定时业务逻辑类 public class ExpireJobTask { /** Logger */ private static final Logger logger = LoggerFact ...

  8. Spring集成Quartz定时任务

    1.导入jar包 2.配置applicationContext.xml文件 <!-- 任务调度1 --> <!-- bean id="simpleJob" cla ...

  9. Spring MVC+Quartz 定时任务持久化

    请自行参考: http://sloanseaman.com/wordpress/2011/06/06/spring-and-quartz-and-persistence/ https://object ...

随机推荐

  1. Android-HttpURLConnection自己主动管理cookie

    Volley那么好用的框架居然没有内置对cookie的处理,自己搞一个! public class MobCookieManager {//转载请标明出处:http://blog.csdn.net/g ...

  2. linux怎么开启telnet服务

    1>编辑telent的配置文件/etc/xinetd.d/telnet 如下: (设置disable = no,也就是开启telnet服务) service telnet { disable = ...

  3. Microsoft Edge 针对 Web 开发人员更新日志

    Windows 10 build16215 之 Edge 新功能 新功能: 增加了对高级事件监听器的支持(“once”和“passive”)via 增加了对CSS object-fit/object- ...

  4. 忘记了本地mysql密码应该怎么找回

    1.    首先以系统管理员身份登陆系统 2.    打开命令行窗口停止mysql服务 C:\>net stop mysql MySQL 服务正在停止. MySQL 服务已成功停止 3.到mys ...

  5. ajax返回数据时,如何将javascript值(通常为对象或数组)转为json字符串

    ajax获取值时,返回的数据为空时 alert后出现 [ ]; 用if语句判断时不为空,此时如何判断返回的数据是否为空.可将返回的值转化为json字符串. JSON.stringify() 方法用于将 ...

  6. 脚本_实时显示网卡eth0上的数据流量

    #!bin/bash#功能:使用死循环,实时显示网卡eth0发送的数据包流量#作者:liusingbonwhile : do       echo "本地网卡eth0的数据流量信息如下:&q ...

  7. 贰、js的基础(二)类型转换

    JS 数据类型转换 方法主要有三种 转换函数.强制类型转换.利用js变量弱类型转换. 1. 转换函数: js提供了parseInt()和parseFloat()两个转换函数.前者把值转换成整数,后者把 ...

  8. URL正则

    现有需求 表单填写域名只能填写 baseURL 或者 baseURL+端口 不带协议 否则为不合法 String url1 = ".com:90"; String url2 = & ...

  9. WordPress 不错的插件

    Akismet – 防止垃圾评论 WP-PostViews Plus - 页面访问量统计 All in One SEO Pack – 搜索引擎优化的插件,自动优化搜索引擎. WP Super Cach ...

  10. webpack 操作

    依赖安装 :  全局安装webpack : sudo npm install webpack -g 本地安装webpack : npm install webpack —save-dev  需要注意的 ...