Spring整合的quartz任务调度的实现方式

一、在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>contextConfigLocationparam-name>

      <param-value>/WEB-INF/classes/beans.xmlparam-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

 *     涂作权

 * 期时间 2013-6-18下午02:09:20

 *            创建一个要执行任务的类,该类必须继承QuartzJobBean规范

 *      V1.0

 */

publicclass CodeCurDate extends 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">

                                  

     

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

        

         <property name="jobClass">

          <value>cn.itcast.CodeCurDatevalue>

         property>

      bean>

     

     

      <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>

     

     

      <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

 *创建人     :涂作权

 *日期时间:2013-6-18 下午02:54:57

 *描述           : 

 *版本号     :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">

 

     

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

        

         <property name="jobClass">

          <value>cn.itcast.CodeCurDatevalue>

         property>

      bean>

     

     

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

        

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

        

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

      bean>

       

     

      <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>contextConfigLocationparam-name>

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

  context-param>

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>

  listener>

web-app>

 

 

 

你可能感兴趣的:(#,Quartz任务调度(定时))