日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Quartz 框架快速入门(四)

發布時間:2025/3/21 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Quartz 框架快速入门(四) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Springscheduling.quartz包中對Quartz框架進行了封裝,使得開發時不用寫任何QuartSpring的代碼就可以實現定時任務。Spring通過JobDetailBeanMethodInvokingJobDetailFactoryBean實現Job的定義。后者更加實用,只需指定要運行的類,和該類中要運行的方法即可,Spring將自動生成符合Quartz要求的JobDetail

在上一篇文章《Quartz 框架快速入門(三)》中我們將示例遷移到Web環境下了,但使用的是Quartz的啟動機制,這一篇中我們將讓Web服務器啟動Spring,通過Spring的配置文件來進行任務的調度

1,創建一個Web項目,加入spring.jarquartz-1.6.0.jarcommons-collections.jarjta.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 框架快速入门(四)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。