spring4.0 整合 Quartz 實現任務調度(一)

前言

  項目需求:html

    二維碼推送到一體機上,給學生簽到掃描用。而後須要的是 上課前20分鐘 ,幸虧在幫帶個人學長作 p2p 的時候,接觸過。天然 quartz 是首選。因此我就配置了下,搞了個小樣例給你們。java

正文 

  spring4.0 整合 Quartz 實現任務調度。這是期末項目的最後一篇,剩下到暑假吧。
web

Quartz 介紹spring

    Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; 數據庫

    Quartz框架是一個全功能、開源的任務調度服務,能夠集成幾乎任何的java應用程序—從小的單片機系統到大型的電子商務系統。Quartz能夠執行上千上萬的任務調度。spring-mvc

 

   核心概念session

     Quartz核心的概念:scheduler任務調度、Job任務、Trigger觸發器、JobDetail任務細節mvc

     相關文檔:http://www.quartz-scheduler.org/documentation/quartz-2.2.x/quick-startapp

實戰 

  第一步 :spring、quartz 相應的jar包,添加到項目中(須要的call me)框架

    /WEB-INF/lib/quartz-2.2.1.jar

    以及spring的一些必要包

  

    第二步:web.xml中配置spring

<?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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>quartz</display-name>
    <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>

    <!--Spring WebApplicationContext上下文,稱爲父上下文(父容器) -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--Srping <listener> <listener-class> org.springframework.web.context.request.RequestContextListener 
        </listener-class> </listener> -->
    <!--加載spring的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--Spring MVC 配置 DispatcherServlet -->
    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--filter配置,解決編碼問題 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--OpenSessionInViewFilter配置,解決延遲加載時Session會關閉的問題 -->
    <!-- <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> -->

    <!-- session過時時間: 20 -->
    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>

    <!-- 錯誤界面 -->
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/WEB-INF/error/500.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/error/500.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/error/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>400</error-code>
        <location>/WEB-INF/error/400.jsp</location>
    </error-page>

</web-app>

 #有些你不用的,就不要寫了。

 

    第三:在spring配置文件中配置quartz任務調度(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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<!--Quartz-->
            
    <!-- 集成方式:JobDetailFactoryBean,而且任務類須要繼承QuartzJobBean-->
    <!-- 定義jobDetail -->
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <!-- durability 表示任務完成以後是否依然保留到數據庫,默認false   -->
        <property name="durability" value="true" />  
        <!--     目標類  /wmuitp/src/test/SpringQuartz.java-->
        <property name="jobClass" value="test.SpringQuartzTest"></property>
        
           <!--  在這個例子中,jobDataAsMap沒有用,此目標類中接受的參數 ,若參數爲service,則能夠在此進行參數配置,相似struts2 -->
           <!--
        <property name="jobDataAsMap">  
            <map>  
                <entry key="service"><value>simple is the beat</value></entry>  
            </map>  
        </property>
            -->
    </bean>
    
    <!-- 定義simpleTrigger觸發器 -->
    <!--     
    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="jobDetail"></property>
        <property name="repeatCount">
            <value>8</value>
        </property>
        <property name="repeatInterval">
            <value>1000</value>
        </property>
        <property name="startDelay">
            <value>4</value>
        </property>
    </bean> 
    -->
    
    <!-- 另外一種觸發器是CornTrigger -->
     <bean id="cornTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
       <property name="jobDetail" ref="jobDetail"/>
       <!-- 每一個10秒觸發 -->
       <property name="cronExpression" value="0/10 * * * * ?"/>
    </bean> 
    
    <!-- 定義核心調度器 -->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
        <ref bean="cornTrigger"/>
      </property>
    </bean>
    
 </beans>


#目標類

<property name="jobClass" value="test.SpringQuartzTest"></property>
 下面第四步:編寫目標類
package test;
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class SpringQuartzTest extends QuartzJobBean{
    
    /*業務實現*/
    public void work() {
//        System.out.println("執行調度任務:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.fff").format(new Date()+"開始執行"));
        System.out.println("執行調度任務:"+new Date());
    }

    @Override
    protected void executeInternal(JobExecutionContext arg0)
            throws JobExecutionException {
        this.work();
    }

}
#須要繼承QuartzJobBean
測試運行結果(這個很重要 能服衆)


須要源碼的能夠留言給我,必定奉上。還請多多指教

相關文章
相關標籤/搜索