Spring + Quartz配置实例
Spring为创建Quartz的Scheduler、Trigger和JobDetail提供了便利的FactoryBean类,以便能够在Spring 容器中享受注入的好处。此外Spring还提供了一些便利工具类直接将Spring中的Bean包装成合法的任务。Spring进一步降低了使用Quartz的难度,能以更具Spring风格的方式使用Quartz。概括来说它提供了两方面的支持:
1)为Quartz的重要组件类提供更具Bean风格的扩展类;
2)提供创建Scheduler的BeanFactory类,方便在Spring环境下创建对应的组件对象,并结合Spring容器生命周期进行启动和停止的动作。
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 要调用的工作类 -->
<bean id="quartzJob" class="test.Task"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobtask"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob" />
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask" />
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime" />
</list>
</property>
</bean>
</beans>
要调度的工作类:
public class Task{
public void work() {
System.out.println("Quartz的任务调度!");
}
}
主测试文件:
public class QuartzTest {
public static void main(String[] args) {
System.out.println("Test start.");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
System.out.print("Test end..\n");
}
}
关闭 quartz:
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContextEvent
.getServletContext()
.getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
StdScheduler quartzScheduler = (StdScheduler) webApplicationContext
.getBean("startQuertz");
if (quartzScheduler != null) {
System.out.println("quartzScheduler关闭!");
quartzScheduler.shutdown(true);
}
若非spring加载:
try {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
scheduler.shutdown(true);
System.out.println("====关闭quartz===="+scheduler.getSchedulerName());
} catch (SchedulerException e) {
e.printStackTrace();
}
参考文章【http://www.cnblogs.com/kay/archive/2007/11/02/947372.html】
【http://www.diybl.com/course/3_program/java/javajs/2008530/118164.html】
Spring + Quartz配置实例的更多相关文章
- 使用spring+quartz配置多个定时任务
Spring被用在了越来越多的项目中, quartz也被公认为是比较好用的定时器设置工具, 在这里通过一个demo说明如何使用spring和quartz配置多个定时任务. 环境: eclipse + ...
- spring quartz 配置实现定时任务 详解
一. 编写定时任务JAVA类 比如: public class QuartzJob { public QuartzJob(){ System.out.println(" ...
- spring注解配置实例
在spring中使用注解配置前需要先在配置文件指定需要扫描的包. 通过注解的方式依赖注入,可以不用创建set方法,也不用在xml文件中申明注入关系. 实例结构如下: 整个流程是: 先创建好数据库的表对 ...
- spring quartz 配置
quartz简介 各种企业应用几乎都会碰到任务调度的需求,就拿论坛来说:每隔半个小时生成精华文章的RSS文件,每天凌晨统计论坛用户的积分排名,每隔30分钟执行锁定用户解锁任务.任务调度本身涉及到多线程 ...
- spring quartz 配置多个定时任务
1.配置文件-quartz-1.7.3jar spring版本为3.1.3jar <?xml version="1.0" encoding="UTF-8&quo ...
- Spring+Quartz配置定时任务
一.Quartz介绍 在企业应用中,我们经常会碰到时间任务调度的需求,比如每天凌晨生成前天报表,每小时生成一次汇总数据等等.Quartz是出了名的任务调度框架,它可以与J2SE和J2EE应用程序相结合 ...
- Spring MVC配置实例
1.下载Jar文件,添加到项目 lib文件夹中. 使用eclipse新建 Web 项目.下载导入相关的 jar 和 Tomcat.我的java版本是JDK1.8 对应的 Tomcat 版本是 8.0. ...
- Spring+hibernate 配置实例
转自:http://www.cnblogs.com/hongten/archive/2012/03/10/java_spring_hibernate.html 项目结构: http://www.cnb ...
- spring quartz 配置及说明
方式一,jobDetail的bean利用MethodInvokingJobDetailFactoryBean 工厂包装 : ()定义一个bean,执行具体的业务操作. <bean id=&quo ...
随机推荐
- js 日期控件laydate使用
官网 http://sentsin.com/layui/laydate/ 1. 下载官网上的压缩包,解压后只需要复制laydate 文件夹到你的项目中; 2. 在页面引入 <script t ...
- Zipf’s Law
Let f(w) be the frequency of a word w in free text. Suppose that all the words of a text are ranked ...
- (简单) POJ 2406 Power Strings,扩展KMP。
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...
- 使用for循环输出杨辉三角-还是不懂得需要复习
package com.chongrui.test; /* *使用for循环输出杨辉三角杨辉三角形由数字排列,可以把它看作一个数字表,其基本特征是两侧的数值均为1,其他位置的数值是其正上方的数值与左上 ...
- spring 5种通知
方法实现接口 package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalculator { int add(in ...
- C#webbrowser控件技巧(取得javascript变量值,禁止显示脚本错误)
C#中的webbrowser控件比较好用. 下面本人搜索整理的几个小技巧. 1. 从C#中取得javascript的变量值. using mshtml;using System.Reflection; ...
- MapReduce 简单的全文搜索
上一个已经实现了反向索引,那么为什么不尝试下全文搜索呢.例如有了 Hello file3.txt:1; MapReduce file3.txt:2;fil1.txt:1;fil2.tx ...
- sql数据库中查询第几条到第几条的数据
通用方法: select top 500 * from (select top 1000 * from UserSearchDatas order by ID) a order by ID desc ...
- 使用virsh搭建虚拟机管理环境
这里简单的说说需要安装的依赖库 1. 安装kvm centos yum install kvm ubuntu apt-get install kvm 2. 安装qemu yum install qem ...
- USACO 2015 December Contest, Platinum Problem Max Flow【树链剖分】
题意比较难理解,就是给你n个点的树,然后给你m个修改操作,每一次修改包括一个点对(x, y),意味着将x到y所有的点权值加一,最后问你整个树上的点权最大是多少. 比较裸的树链剖分了,感谢Haild的讲 ...