- 浏览: 203769 次
- 性别:
- 来自: 北京
最新评论
-
guoranaccp:
太帅了
Base64Encoder源码 -
xsw331:
...
javacsv导出 -
rock1103:
不错啊,中文也可以
javacsv导出 -
lyfi:
...
Base64Encoder源码 -
chengpeinishi:
亲们,资源可用哦。
javacsv导出
Spring的scheduling.quartz包中对Quartz框架进行了封装,使得开发时不用写任何QuartSpring的代码就可以实现定时任务。Spring通过JobDetailBean,MethodInvokingJobDetailFactoryBean实现Job的定义。后者更加实用,只需指定要运行的类,和该类中要运行的方法即可,Spring将自动生成符合Quartz要求的JobDetail。
在上一篇文章《Quartz 框架快速入门(三)》中我们将示例迁移到Web环境下了,但使用的是Quartz的启动机制,这一篇中我们将让Web服务器启动Spring,通过Spring的配置文件来进行任务的调度
1,创建一个Web项目,加入spring.jar,quartz-1.6.0.jar,commons-collections.jar,jta.jar ,commons-logging.jar这几个包.
2,创建一个类,在类中添加一个方法execute,我们将对这个方法进行定时调度.
package com.vista.quartz;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloWorld
{
private static Log logger = LogFactory.getLog(HelloWorld.class);//日志记录器
public HelloWorld()
{
}
public void execute()
{
logger.info("Kick your ass and Fuck your mother! - " + new Date());
}
}
2. Spring配置文件applicationContext.xml 修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 要调用的工作类 -->
<bean id="quartzJob" class="com.vista.quartz.HelloWorld"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>execute</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>
3,先在控制台中对上面的代码进行测试,我们要做的只是加载Spring的配置文件就可以了,代码如下:
package com.vista.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{
public static void main(String[] args)
{
System.out.println("Test start.");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
//context.getBean("startQuertz");
System.out.print("Test end..");
}
}
4,然后将Web.xml修改如下,让tomcat在启动时去初始化Spring:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在上一篇文章《Quartz 框架快速入门(三)》中我们将示例迁移到Web环境下了,但使用的是Quartz的启动机制,这一篇中我们将让Web服务器启动Spring,通过Spring的配置文件来进行任务的调度
1,创建一个Web项目,加入spring.jar,quartz-1.6.0.jar,commons-collections.jar,jta.jar ,commons-logging.jar这几个包.
2,创建一个类,在类中添加一个方法execute,我们将对这个方法进行定时调度.
package com.vista.quartz;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloWorld
{
private static Log logger = LogFactory.getLog(HelloWorld.class);//日志记录器
public HelloWorld()
{
}
public void execute()
{
logger.info("Kick your ass and Fuck your mother! - " + new Date());
}
}
2. Spring配置文件applicationContext.xml 修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 要调用的工作类 -->
<bean id="quartzJob" class="com.vista.quartz.HelloWorld"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>execute</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>
3,先在控制台中对上面的代码进行测试,我们要做的只是加载Spring的配置文件就可以了,代码如下:
package com.vista.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{
public static void main(String[] args)
{
System.out.println("Test start.");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化
//context.getBean("startQuertz");
System.out.print("Test end..");
}
}
4,然后将Web.xml修改如下,让tomcat在启动时去初始化Spring:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
发表评论
-
eclipse axis2 插件 安装 link方式
2012-05-15 16:59 1796插件下载地址: http://www.apache.o ... -
JRuby初始安装与使用
2012-03-06 10:28 1125JRUBY的下载:http://dist.codehaus.o ... -
input type=file 标签禁止让用户手动输入
2012-02-21 14:08 960<input name="filePat ... -
通过Mysql语句得到mysql安装路径
2012-01-07 13:07 9061、通过Mysql语句得到mysql安装路径: sel ... -
Apache Ant 环境搭建
2012-01-07 13:05 1089Apache Ant 下载地址:http://ant.apac ... -
Base64Encoder源码
2011-11-09 08:56 6212import java.io.*; /****** ... -
Base64Decoder源码
2011-11-09 08:55 2532import java.io.*; /****** ... -
JS和Java验证IP合法
2011-11-01 17:32 2364/* JS验证IP是否合法 */ function i ... -
修改Mysql启动路径
2011-11-01 17:31 1880修改Mysql启动路径 首先在服务中停止MySq ... -
Mysql提速
2011-09-21 16:55 1419因数据较大,导致执行速度慢,同事帮我优化一下,果然见效。 ... -
更换SVN用户
2011-09-21 16:25 760修改eclipse中的svn用户 1. 查看你的 ... -
Mac下JDK路径
2011-08-26 22:49 1206/System/Library/Java/JavaVirtua ... -
web.xml中load-on-startup标签
2011-08-11 10:54 813在servlet的配置当中,<load-on-st ... -
同时启动多个Tomcat服务器
2011-08-09 09:17 880所用Tomcat服务器都为zip版,非安装版。以两个为例:安装 ... -
String StringBuffer StringBuilder区别
2011-07-30 11:24 770String 字符串常量StringB ... -
java中访问修饰符public,protected,private,friendly
2011-07-30 11:21 10171)public: 表明该数据成员、函数都是对多有用户开发 ... -
存储过程的优点
2011-07-26 01:38 994在性能方面,存储过程的优点: 1、预编译,存储过程预先编译好放 ... -
数据库视图的优点与缺点
2011-07-26 01:34 1719在程序设计的时候必须先了解视图的优缺点,这样可以扬长避短,视图 ... -
eclipse配置tomcat7
2011-05-31 12:32 1008图解如下: 修改服务器启动所需的jar包 即to ... -
Linux下Java环境的JDK+Tomcat+Mysql安装和配置
2011-05-26 13:29 1003CentOS5.4下安装JDK 1、 ...
相关推荐
Quartz框架快速入门Quartz框架快速入门
Quartz 框架快速入门 在前面两篇文章中简单介绍了在java应用程序中如何使用Quartz框架,这一篇中我们将看到如何在web环境下通过配置文件来完成Quartz的后台作业调度,而不必手工去创建Trigger和Scheduler
Quartz框架快速入门.pdf
NULL 博文链接:https://wangrl.iteye.com/blog/1125327
调度器基本使用,了解quartz的基本使用方法
本文档包含以下章节:1、quartz简介;2、quartz快速入门;3、quartz核心API详解;4、quartz.properties配置详解;5、quartz持久化;6、quartz集群部署;7、springboot整合quartz。8、常见分布式调度框架的比较;
Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中。它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。
Quartz中文入门教程 前言 Quartz让任务调度简单 Quartz的发展史 上手Quartz Quartz内部架构 作业 作业管理和存储 有效作业存储 ...Quartz框架的其他特征 Quartz下一步计划 了解更多Quartz特征
定时器框架学习资料,入门手册,新手专用!帮你快速入门开发搭建自己的定时器框架环境
Java框架介绍Quartz从入门到进阶的文档介绍
Java框架介绍Quartz从入门到进阶.pdf
这是一个本人自主编写的quartz调度框架的入门实例,非常简单,无配置文件,纯java调度,main 方法执行,控制台输出,可作为quartz框架的入门学习使用
这份quartz ppt作者专门为入门学习quartz的赌注精心制作,包含了quartz的api详细解读,集群搭建等
Quartz入门和Quartz开发指南
Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中。它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。它有很多特征,如:数据库支持,集群...
1.spring框架使用任务调度quartz的例子。 2.Web App用Quartz实现java schedule 3.详细讲解Quartz如何从入门到精通 4.用 Quartz 进行作业调度
一个很好的调度工具学习资料,可以定时调用发送短信,上传文件等功能,JAR包后面有
Quartz是一个开源的作业调度框架,它完全由 Java写成,并设计用于J2SE和J2EE应用中。它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。它有很 多特征,如:数据库支持,...