https://blog.csdn.net/u010378410/article/details/26016025
前言
系列文章:[傳送門]
項目需求:
二維碼推送到一體機上,給學生簽到掃描用。然后須要的是 上課前20分鐘 ,幸好在幫帶我的學長做 p2p 的時候。接觸過。自然 quartz 是首選。所以我就配置了下,搞了個小例子給大家。
正文?
spring4.0 整合 Quartz 實現任務調度。
這是期末項目的最后一篇。剩下到暑假吧。
? ?Quartz?介紹
Quartz能夠運行上千上萬的任務調度。
實戰?
第一步 :spring、quartz 對應的jar包。加入到項目中(須要的call me)
/WEB-INF/lib/quartz-2.2.1.jar
以及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>
? #有些你不用的,就不要寫了。
<!--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());}@Overrideprotected void executeInternal(JobExecutionContext arg0)throws JobExecutionException {this.work();} }
#須要繼承QuartzJobBean測試運行結果(這個非常重要 能服眾)
總結
spring quartz
感謝及資源共享
http://url.cn/RzETYu?加入我的群
路上走來一步一個腳印。希望大家和我一起。
感謝讀者!
非常喜歡你們給我的支持。假設支持,點個贊。
知識來源: 《spring in action》