任务调度--spring下的任务调度quartz

来源:互联网 发布:吉林师范大学教务网络 编辑:程序博客网 时间:2024/04/30 15:07

之前写过Timer实现任务调度,这篇文章用来写一下在spring下使用quartz实现任务调度,直接上代码:
定义任务对象:

package com;/** * 1. 定义任务对象 *  * @author Administrator * */public class DataBackup {    //提供任务方法 - 任务实现的内容    public void backup(){        System.out.println("备份数据库");    }}

spring的配置文件

<!-- 该配置文件为spring的基本配置文件, springmvc,aop ,transaction等的配置均在此基础上进行 --><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"      xsi:schemaLocation= "http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd           http://www.springframework.org/schema/context          http://www.springframework.org/schema/context/spring-context-3.2.xsd        " >     <context:component-scan base-package ="com"/>      <!-- 2.将任务对象交给ioc容器管理       -->      <bean id="bu" class="com.DataBackup"> </bean>    <!--  3. 设置任务调度细节                                 任务内容有哪个任务对象提供,这个对象中有哪个方法 -->       <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">         <property name="targetObject" ref="bu"></property>         <property name="targetMethod" value="backup"></property>       </bean>     <!--    4.设置这个任务调度jobdetail执行的时间、周期                 >为哪个任务分配执行时间和周期                    setJobDetail(JobDetail detail)                >分配执行周期                    setCronExpression(string expression)                                            通过一个表达式- cron表达式                         [秒    0-59 *                                                           分   0-59 *                                                           时    0-23 *                                                           月内日期   1-31 *                                                           月   1-12 *                                                           周内日期    0-6 *                                                           年    可选 1970-2099                                                          ?   只能出现在月内日期和周内日期字段中,表示该字段无关紧要 放在月内日期或周内日期的位置                                                                当指定了月内日期 周内日期无需指定                          /  增量值 写在/后                          # 第几个 0 10 9 ? 1 2#2  一月的第二个星期二                          W 工作日  1W 一月的第一个工作日                          L 最后一个实例  1L一月的最后一天                                                             ]     -->       <bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">             <property name="jobDetail" ref="jobDetail"></property>              <property name="cronExpression" value="0 * * * * ?"></property>       </bean>      <!--5  配置总调度类  将之前配置的任务调度的过程加载到容器中-->        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">                  <property name="triggers">                    <list>                        <ref bean="trigger"/>                    </list>                  </property>        </bean></beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"     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_3_0.xsd"> <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener>         <!-- 配置spring分发器,spring表示对应的 servlet【名可以改】配置文件为spring-servlet.xml -->     <servlet >            <servlet-name >spring </servlet-name >           <servlet-class >org.springframework.web.servlet.DispatcherServlet </servlet-class >     </servlet >     <servlet-mapping >      <!-- 会拦截.do请求-->            <servlet-name >spring </servlet-name >            <url-pattern >*.do </url-pattern >     </servlet-mapping >  <display-name></display-name>   <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

quartz中常用的表达式:
“0 0 12 * * ?” 每天中午12点触发
“0 15 10 ? * *” 每天上午10:15触发
“0 15 10 * * ?” 每天上午10:15触发
“0 15 10 * * ? *” 每天上午10:15触发
“0 15 10 * * ? 2005” 2005年的每天上午10:15触发
“0 * 14 * * ?” 在每天下午2点到下午2:59期间的每1分钟触发
“0 0/5 14 * * ?” 在每天下午2点到下午2:55期间的每5分钟触发
“0 0/5 14,18 * * ?” 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
0 0/5 8-10,12-16,20-22 * * ?

"0 0-10/2 14 * * ?" 在每天下午2点到下午2:05期间的每2分钟触发 "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 "0 15 10 15 * ?" 每月15日上午10:15触发 "0 15 10 L * ?" 每月最后一日的上午10:15触发 "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发 

每天早上6点 0 0 6 * * *
每两个小时 0 * /2 * *
23到次日早上7点,每隔2小时 , 以及8点执行
0 0 23-7/2,8 * * ?
1-3月,每天的4点11分执行
0 11 4 * 1-3 ?

0 4 1 1 * ?
0 0
原创粉丝点击