springmvc中實現quartz定時任務(每分鐘的第3秒執行任務調度方法)

1:實現觸發器,最大的問題是jar包的處理(*.jar定時jar和sourcecodesource code):

此處,最關鍵的jar爲第二個,名字最長。

maven依賴:

    <dependency>  
        <groupId>org.apache.servicemix.bundles</groupId>  
        <artifactId>org.apache.servicemix.bundles.spring-context-support</artifactId>  
        <version>4.0.7.RELEASE_2</version>  
    </dependency>  
    <dependency>  
        <groupId>org.quartz-scheduler</groupId>  
        <artifactId>quartz</artifactId>  
        <version>1.8.6</version>  
    </dependency>  

2:觸發器在web.xml中配置:

    <?xml version="1.0" encoding="UTF-8"?>  
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns="http://java.sun.com/xml/ns/javaee"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
        id="WebApp_ID" version="3.0">  
        <display-name>webdemo1</display-name>  
          <!-- 監聽spring上下文容器 -->  
      <listener>  
        <listener-class>  
                org.springframework.web.context.ContextLoaderListener   
        </listener-class>   
      </listener>  
      <!-- 加載spring的xml配置文件到spring的上下文容器中 -->  
      <context-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath*:applicationContext-*.xml</param-value>  
      </context-param>  
        <!-- 配置springmvc DispatcherServlet  -->  
      <servlet>  
        <servlet-name>mvc</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/springmvc.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
      </servlet>  
      <!-- 配置DispatcherServlet需要攔截的url -->  
      <servlet-mapping>  
        <servlet-name>mvc</servlet-name>  
        <url-pattern>*.html</url-pattern>  
      </servlet-mapping>  
          
        <welcome-file-list>  
            <welcome-file>index.html</welcome-file>  
            <welcome-file>index.htm</welcome-file>  
            <welcome-file>index.jsp</welcome-file>  
            <welcome-file>default.html</welcome-file>  
            <welcome-file>default.htm</welcome-file>  
            <welcome-file>default.jsp</welcome-file>  
        </welcome-file-list>  
    </web-app>  

3:springmvc的配置:

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans  
        xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:context="http://www.springframework.org/schema/context"    
        xmlns:mvc="http://www.springframework.org/schema/mvc"    
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-3.0.xsd  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
            <!-- springmvc配置 -->  
            <!-- 通過component-scan讓spring掃描package下的所有類,讓spring的註解生效-->  
            <context:component-scan base-package="com.tsxs"></context:component-scan>  
            <!-- 配置springmvc的視圖渲染器,讓其前綴爲:/ 後綴爲: .jsp 將視圖渲染到 /views/<method返回值>.jsp中 -->  
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
                <property name="prefix" value="/WEB-INF/views/"></property>  
                <property name="suffix" value=".jsp"></property>  
            </bean>  
    </beans>  

4:定時任務配置文件

    <?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-4.0.xsd"  
        default-autowire="byName" default-lazy-init="false">  
        <!-- default-autowire="byName" default-lazy-init="false"此兩個值可以不配置 -->  
        <description>Quartz Job Setting</description>  
      <!-- A.配置調度的任務對應bean的id和自定義class-->  
      <bean id="myQuartz" class="com.tsxs.tools.Quartz" />  
      <!-- B.配置調度任務對應的bean的id和執行的方法,作業不併發調度-->  
      <bean id="myDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <property name="targetObject" ref="myQuartz" />  
        <property name="targetMethod" value="tips" />  
        <property name="concurrent" value="false" />  
      </bean>  
      <!-- C.配置調度任務執行的觸發的時間-->  
      <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
      <property name="jobDetail" ref="myDetail" />  
         <property name="cronExpression">  
         <!-- 每分鐘的第3秒執行任務調度 -->  
          <value>3 * * * * ?</value>  
        </property>  
      </bean>  
      <!-- D.Quartz的調度工廠,調度工廠只能有一個,多個調度任務在list中添加 -->  
      <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
          <list>  
             <!-- 所有的調度列表-->  
            <ref bean="myTrigger" />  
    <!-- <ref bean="myTrigger1" />  
            <ref bean="myTrigger2" />  
            對應的bean配置:id="myDetail1" 和 id="myTrigger2" 可以對應的並行多配置-對應執行JavaBean和執行時間(各自觸發time)  
      -->  
          </list>  
        </property>  
      </bean>  
    </beans>  

注:時間配置:可以看quartz配置或者網絡搜索“quartz定時配置”

5:定時任務執行JavaBean:

    package com.tsxs.tools;  
      
    import java.text.SimpleDateFormat;  
    import java.util.Date;  
    import java.util.Locale;  
      
    import org.quartz.Job;  
    import org.quartz.JobExecutionContext;  
    import org.quartz.JobExecutionException;  
      
    public class Quartz {  
    //  public class Quartz implements Job{  
    //此處可以不實現Job接口的execute方法  
    //  private Date date;  
        /** 
         * 定時任務,執行方法 
         * */  
        public void tips(){  
            String time = new SimpleDateFormat("MMM d,yyyy KK:mm:ss a",Locale.ENGLISH).format(System.currentTimeMillis());  
            System.out.println("time:"+time);  
        }  
      
    //  @Override  
    //  public void execute(JobExecutionContext context) throws JobExecutionException {  
    //      date = context.getFireTime();  
    //  }  
    }  

6:運行結果:

time:Jun 24,2015 00:05:03 PM  
time:Jun 24,2015 00:06:03 PM  
time:Jun 24,2015 00:07:03 PM  
time:Jun 24,2015 00:08:03 PM  
time:Jun 24,2015 00:09:03 PM 

注:

①:定時任務執行JavaBean可以不實現Job接口的execute方法

②:在定時任務配置中:設置default-lazy-init="true",否則定時任務不觸發,如果不明確指明default-lazy-init的值,默認是false

③:在定時任務配置中:設置default-autowire="byName"的屬性,可能導致後臺會報org.springframework.beans.factory.BeanCreationException錯誤(此時,就不能通過Bean名稱自動注入,必須通過明確引用注入)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章