Quartz 框架快速入门(四)
Spring的scheduling.quartz包中對Quartz框架進行了封裝,使得開發時不用寫任何QuartSpring的代碼就可以實現定時任務。Spring通過JobDetailBean,MethodInvokingJobDetailFactoryBean實現Job的定義。后者更加實用,只需指定要運行的類,和該類中要運行的方法即可,Spring將自動生成符合Quartz要求的JobDetail。
在上一篇文章《Quartz 框架快速入門(三)》中我們將示例遷移到Web環境下了,但使用的是Quartz的啟動機制,這一篇中我們將讓Web服務器啟動Spring,通過Spring的配置文件來進行任務的調度
1,創建一個Web項目,加入spring.jar,quartz-1.6.0.jar,commons-collections.jar,jta.jar?,commons-logging.jar這幾個包.
?????2,創建一個類,在類中添加一個方法execute,我們將對這個方法進行定時調度.
package?com.vista.quartz;import?java.util.Date;
import?org.apache.commons.logging.Log;
import?org.apache.commons.logging.LogFactory;
import?org.quartz.JobExecutionContext;
import?org.quartz.JobExecutionException;
public?class?HelloWorld?
{
????private?static?Log?logger?=?LogFactory.getLog(HelloWorld.class);//日志記錄器
????public?HelloWorld()
????{
????}
????public?void?execute()
????{
????????logger.info("Kick?your?ass?and?Fuck?your?mother!?-?"?+?new?Date());?
????}
}
2. Spring配置文件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"
????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
????<!--?要調用的工作類?-->
????<bean?id="quartzJob"?class="com.vista.quartz.HelloWorld"></bean>
????<!--?定義調用對象和調用對象的方法?-->
????<bean?id="jobtask"?class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
????????<!--?調用的類?-->
????????<property?name="targetObject">
????????????<ref?bean="quartzJob"/>
????????</property>
????????<!--?調用類中的方法?-->
????????<property?name="targetMethod">
?????????????<value>execute</value>
????????</property>
????</bean>
????<!--?定義觸發時間?-->
????<bean?id="doTime"?class="org.springframework.scheduling.quartz.CronTriggerBean">
????????<property?name="jobDetail">
????????????<ref?bean="jobtask"/>
????????</property>
????????<!--?cron表達式?-->
????????<property?name="cronExpression">
????????????<value>10,15,20,25,30,35,40,45,50,55?*?*?*?*??</value>
????????</property>
????</bean>
????<!--?總管理類?如果將lazy-init='false'那么容器啟動就會執行調度程序??-->
????<bean?id="startQuertz"?lazy-init="false"?autowire="no"?class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
????????<property?name="triggers">
????????????<list>
???????????????<ref?bean="doTime"/>
????????????</list>
????????</property>
????</bean>
</beans>
3,先在控制臺中對上面的代碼進行測試,我們要做的只是加載Spring的配置文件就可以了,代碼如下:
package?com.vista.quartz;import?org.springframework.context.ApplicationContext;
import?org.springframework.context.support.ClassPathXmlApplicationContext;
public?class?Test?
{
????public?static?void?main(String[]?args)?
????{
?????????System.out.println("Test?start.");
????????????ApplicationContext?context?=?new?ClassPathXmlApplicationContext("applicationContext.xml");
????????????//如果配置文件中將startQuertz?bean的lazy-init設置為false?則不用實例化
????????????//context.getBean("startQuertz");
?????????System.out.print("Test?end..");
????}
}
4,然后將Web.xml修改如下,讓tomcat在啟動時去初始化Spring:
<?xml?version="1.0"?encoding="UTF-8"?><web-app?version="2.4"?
????xmlns="http://java.sun.com/xml/ns/j2ee"?
????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
????xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee?
????http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
?????<context-param>
????????<param-name>contextConfigLocation</param-name>
????????<param-value>
????????????/WEB-INF/classes/applicationContext.xml
????????</param-value>
????</context-param>?
????
????<servlet>
????????<servlet-name>SpringContextServlet</servlet-name>
????????<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
????????<load-on-startup>1</load-on-startup>
????</servlet>?
??<welcome-file-list>
????<welcome-file>index.jsp</welcome-file>
??</welcome-file-list>
</web-app>
5,最后啟動Tomcat,測試結果如下圖所示:
總結
以上是生活随笔為你收集整理的Quartz 框架快速入门(四)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Quartz 框架快速入门(三)
- 下一篇: 显卡驱动程序卸载以及安装