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