一、在web.xml中将配置文件的位置指定好。

Web.xml的配置如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="2.5"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/classes/beans.xml</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

</web-app>

二、导入相关的jar包

三、编写相关的类文件

package cn.itcast;

import java.util.Date;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.scheduling.quartz.QuartzJobBean;

/**

* 文件名    : CodeCurDate.java<br/>

* 创建人    :涂作权<br/>

下午02:09:20<br/>

* 描述          :  创建一个要执行任务的类,该类必须继承QuartzJobBean规范<br/>

* 版本号    : V1.0

*/

publicclass CodeCurDateextends QuartzJobBean {

/**

* 以某个时间段为周期,循环执行的方法

* 到大某个时间,要执行的方法

*/

protectedvoidexecuteInternal(JobExecutionContextarg0)

throws JobExecutionException {

System.out.println("ppppppppppppppppppppppppppppppppp");

System.out.println(new Date());

}

}

四、编写相关的配置文件

<?xmlversion="1.0"encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

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.5.xsd

                                  http://www.springframework.org/schema/context

                                  http://www.springframework.org/schema/context/spring-context-2.5.xsd

                                   http://www.springframework.org/schema/tx

                                  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

                                  http://www.springframework.org/schema/aop

                                  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!-- 1创建执行任务的类的实例 -->

<beanid="codeCurDate"class="org.springframework.scheduling.quartz.JobDetailBean">

<!--在spring中执行任务的类的实例的创建,不是通过spring的普通的方法,而是把融合到其他JobDetailBean类中-->

<property name="jobClass">

<value>cn.itcast.CodeCurDate</value>

</property>

</bean>

<!-- 2创建一个触发器,整合执行任务的类的实例和时间关联 -->

<beanid="codeCurDateTrigger"class="org.springframework.scheduling.quartz.SimpleTriggerBean">

<!-- 注入要执行任务的类的实例 -->

<property name="jobDetail"ref="codeCurDate"></property>

秒,以毫秒为单位 -->

<property name="startDelay"value="2000"/>

秒执行任务一次,以毫秒为单位-->

<property name="repeatInterval"value="4000"/>

</bean>

<!-- 3注册触发器,启动调度任务 -->

<beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<property name="triggers">

<list>

<refbean="codeCurDateTrigger"/>

</list>

</property>

</bean>

</beans>

五、启动服务器,接着就可以看到控制台中每隔一段时间就与输出。

二、通过CronTrigerBean的方式实现的任务调度策略

首先:编写任务执行类

package cn.itcast;

import java.util.Date;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

importorg.springframework.scheduling.quartz.QuartzJobBean;

/**

*文件名     : CodeCurDate.java<br/>

*创建人     :涂作权<br/>

*日期时间:2013-6-18 下午02:54:57<br/>

*描述           :  <br/>

*版本号     :V1.0

*/

public class CodeCurDate extendsQuartzJobBean {

/**

* 以某个时间段为周期,循环执行的方法

* 到大某个时间,要执行的方法

*/

protectedvoid executeInternal(JobExecutionContext arg0)

throwsJobExecutionException {

System.out.println("PPPPPPPPPPPPPPPPPPPPPPPP");

System.out.println(newDate());

}

}

其次:在Spring的配置文件进行配置,配置代码如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

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.5.xsd

                                  http://www.springframework.org/schema/context

                                  http://www.springframework.org/schema/context/spring-context-2.5.xsd

                                   http://www.springframework.org/schema/tx

                                  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

                                  http://www.springframework.org/schema/aop

                                  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!-- 1创建执行任务的类的实例 -->

<beanid="codeCurDate"class="org.springframework.scheduling.quartz.JobDetailBean">

<!--在spring中执行任务的类的实例的创建,不是通过spring的普通的方法,而是把融合到其他JobDetailBean类中-->

<property name="jobClass">

<value>cn.itcast.CodeCurDate</value>

</property>

</bean>

<!-- 2创建一个触发器,整合执行任务的类的实例和时间关联 -->

<beanid="codeCurDateTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">

<!-- 注入执行任务的类 -->

<property name="jobDetail"ref="codeCurDate"/>

分将调用该触发器的执行 -->

<property name="cronExpression"value="0 32 13 * * ?"/>

</bean>

<!-- 3注册触发器,启动调度任务 -->

<beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<property name="triggers">

<list>

<refbean="codeCurDateTrigger"/>

</list>

</property>

</bean>

</beans>

最后:在web.xml中配置相关数据

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="2.5"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/classes/beans.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

</web-app>

Spring整合的quartz任务调度的实现方式的更多相关文章

  1. Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)

    1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...

  2. Spring整合Struts2框架的第一种方式(Action由Struts2框架来创建)。在我的上一篇博文中介绍的通过web工厂的方式获取servcie的方法因为太麻烦,所以开发的时候不会使用。

    1. spring整合struts的基本操作见我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2 ...

  3. spring整合Quartz2持久化任务调度

    转摘 https://blog.csdn.net/qwe6112071/article/details/50999386 因为通过Bean配置生成的JobDetail和CronTrigger或Simp ...

  4. spring整合mybatis,springMVC的0配置文件方式

    0配置文件的形式主要是采用spring3.0提供的@configuration注解和spring容器在启动的时候会加载实现了WebApplicationInitializer的类,并调用其onStar ...

  5. Spring整合Hibernate:1、annotation方式管理SessionFactory

    1.在applicationContext.xml文件中初始化SessionFactory(annotation方式) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...

  6. Spring与Quartz的整合实现定时任务调度 以及crontab的用法

    最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现.对于一个新手来说,花了我不少时间,这里我写个笔记,给大家参考. 我使用的是Maven来管理项目,需要的Jar包我给大家贴 ...

  7. Spring与Quartz的整合实现定时任务调度(转)

    源:http://kevin19900306.iteye.com/blog/1397744 最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现.对于一个新手来说,花了我不少 ...

  8. Spring与Quartz的整合实现定时任务调度

    摘自: http://kevin19900306.iteye.com/blog/1397744 最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现.对于一个新手来说,花了我 ...

  9. Spring与Quartz的整合实现定时任务调度(转)

    最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现.对于一个新手来说,花了我不少时间,这里我写个笔记,给大家参考.我使用的是Maven来管理项目,需要的Jar包我给大家贴出 ...

随机推荐

  1. iOS开发RunTime之函数调用

    文章来自小笨狼的iOS博客,一直认为csdn的博客UI不太好看,看博客不太爽.所以自己搭建了一个博客. 欢迎各位去链接中看我的博客.也欢迎大家加QQ群讨论iOS技术问题 经过两个多月的面试,工作最终尘 ...

  2. BZOJ 1592: [Usaco2008 Feb]Making the Grade 路面修整( dp )

    最优的做法最后路面的高度一定是原来某一路面的高度. dp(x, t) = min{ dp(x - 1, k) } + | H[x] - h(t) | ( 1 <= k <= t ) 表示前 ...

  3. log4net使用经验总结

    下面介绍几条我认为比较好的经验,让我们更好的运用log4net 1.web程序时不把log4net的配置文件放在web.config中 原因:一个项目随着需求的变更,配置字节会特别多,不便查阅及维护. ...

  4. SQL中on条件与where条件的区别(转载)

    数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1. on条件是在生成临时表时使用的条 ...

  5. HDU 3966 Aragorn&#39;s Story(树链剖分)

    HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...

  6. sharepoint具体错误提示

    sharepoint页面发生错误时,默认不会显示具体错误信息,只显示“未知错误”提示.需要修改配置站点的webconfig文件,才能显示出具体错误提示.具体方法如下: 将safeMode中的CallS ...

  7. 高仿精仿快播应用android源码下载

    今天给大家在网上找到的一款高仿精仿快播应用android源码,分享给大家,希望大家功能喜欢. 说明源码更新中.... 源码即将上传 也可以到这个网站下载:download

  8. 一步一步实现FormsAuthentic验证登录

    本文不讲原理,只讲用法,原理性的东西网上特别多,不过还是会对一些要用到的东西进行解释,不深入讲原理.本文中用的是Vs2012   .net mvc 4.0.原理看这篇文章,看完这个文章绝对受益匪浅. ...

  9. 菜鸟从零学编程(七)——搭建一个完整的Java开发环境

    作为一个Java程序员,配置一个java开发环境是必备的技能,今天给广大菜鸟初学者补上一课.环境的配置,大概就分三个1,JDK 2,Tomcat(或者其他的)3,eclipse(或者myeclipse ...

  10. [IOS]Setting Bundle + StoryBoard

    用storyboard添加一个导航栏,其中首页有一个switch,与setting联动,还有一个button,使用modal连接另一个viewControl,其上也有一个按钮,按下销毁本viewCon ...