2014-05-16 22:51 by Jeff Lihtml
系列文章:[傳送門]java
項目需求:web
二維碼推送到一體機上,給學生簽到掃描用。而後須要的是 上課前20分鐘 ,幸虧在幫帶個人學長作 p2p 的時候,接觸過。天然 quartz 是首選。因此我就配置了下,搞了個小樣例給你們。spring
spring4.0 整合 Quartz 實現任務調度。這是期末項目的最後一篇,剩下到暑假吧。數據庫
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; api
Quartz框架是一個全功能、開源的任務調度服務,能夠集成幾乎任何的java應用程序—從小的單片機系統到大型的電子商務系統。Quartz能夠執行上千上萬的任務調度。spring-mvc
核心概念session
Quartz核心的概念:scheduler任務調度、Job任務、Trigger觸發器、JobDetail任務細節mvc
相關文檔:http://www.quartz-scheduler.org/documentation/quartz-2.2.x/quick-start
app
第一步 :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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>wmuitp</display-name> <!--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任務調度
<!--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>
#目標類
<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 Date()); } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { this.work(); } }
#須要繼承QuartzJobBean
spring quartz
http://url.cn/RzETYu 加入個人羣
路上走來一步一個腳印,但願你們和我一塊兒。
感謝讀者!很喜歡大家給個人支持。若是支持,點個贊。
知識來源: 《spring in action》 quartz api