quartz和timer的区别
轉自:http://apps.hi.baidu.com/share/detail/33720498
1定時器的作用
在實際的開發(fā)中,如果項目中需要定時執(zhí)行或者需要重復執(zhí)行一定的工作,定時器顯現的尤為重要。
當然如果我們不了解定時器就會用線程去實現,例如:
package org.lzstone.action
public class FinanceAction extends Thread{
?????? private Date date;
?????? public void run{
?????? try{
?????? while(true){
?????? Thread.sleep((int)(Math.random()*1000));
?????? date = new Date();
?????? //定時執(zhí)行任務
?????? }
?????? }catch(Exception e){
??????? e.printStackTrace();
?????? }
}
}
自己實現定時器的工作很復雜,如果實現不好占用內存過多,系統(tǒng)就此Over,所以處理定時執(zhí)行或者重復執(zhí)行的任務,定時器是很好的選擇
2.Java中常見的定時器
1)借助Java.util.Timer來實現
2)OpenSymphony社區(qū)提供的Quartz來實現
3.介紹Timer
利用Timer開發(fā)定時任務是主要分為兩個步驟:
1)創(chuàng)建定時任務類
示例代碼:
package org.lzstone.action
import java.util.TimeTask
public class LzstoneTimeTask extends TimeTask{
?????? public void run(){
????????????? //執(zhí)行的定時器任務
?????? }
}
2)運行定時任務,運行定時任務分為兩種方式:
2.1)程序直接啟動
示例代碼:
package org.lzstone.action
public class LzstoneMain{
?????? …….
?????? public void run(){
??????? //執(zhí)行定時器的任務
??????? //創(chuàng)建實例
??????? Timer timer = new Timer();
??????? 參數:
??????? new LzstoneTimeTask()- 所要安排的任務。
??????? 0- 執(zhí)行任務前的延遲時間,單位是毫秒。
??????? 1*1000- 執(zhí)行各后續(xù)任務之間的時間間隔,單位是毫秒。
??????? timer.schedule(new LzstoneTimeTask(),0,1*1000);
?????? }
}
2.2)web監(jiān)聽方式
示例代碼:
package org.lzstone.action
public class LzstoneMain implements ServletContextListener{
?????? private Timer timer = null;
?????? //初始化監(jiān)聽器,創(chuàng)建實例,執(zhí)行任務
?????? public void contextInitialized(ServletContextEvent event){
?????????????? timer = new Timer();
?????????????? timer.schedule(new LzstoneTimeTask(),0,1*1000);
?????? }
?????? //銷毀監(jiān)聽器,停止執(zhí)行任務
?????? public void contextDestroyed(ServletContextEvent event){
????????????? //注意,在此計時器調用的計時器任務的 run 方法內調用此方法,就可以絕對確保正在執(zhí)行的任務是此計時器所執(zhí)行的最后一個任務。
????????????? timer.cancel();
??????? }
}
web.xml配置
<listener>
?? <listener-class>
??????? org.lzstone.action.LzstoneMain
?? </listener-class>
</listener>
4. 介紹Quartz
Quartz是OpenSymphony開源組織在Job scheduling領域又一個開源項目,可以用來創(chuàng)建簡單或者復雜的定時任務,利用Quartz開發(fā)定時任務的步驟與Timer類
似。
利用Quartz開發(fā)定時任務是主要分為兩個步驟:
1)創(chuàng)建定時任務類
示例代碼:
package org.lzstone.action
public class LzstoneTimeTask implements Job{
?????? public void execute(JobExecutionContext context) throws JobExecutionException{
????????????? //執(zhí)行的定時器任務
?????? }
}
2)運行定時任務,運行定時任務分為兩種方式:
2.1)程序直接啟動,創(chuàng)建任務調度器及配置相應的任務計劃
示例代碼:
package org.lzstone.action
public class LzstoneMain{
?????? private static Scheduler sched;
?????? public static void run() throws Exception{
????????????? //創(chuàng)建LzstoneTimeTask的定時任務
????????????? JobDetail jobDetail = new JobDetail(“l(fā)zstoneJob”,sched.DEFAULT_GROUP,LzstoneTimeTask.class);
????????????? //目標 創(chuàng)建任務計劃
????????????? CronTrigger trigger = new CronTrigger(“l(fā)zstoneTrigger”,”lzstone”,”0 0 12 * * ?”);
????????????? //0 0 12 * * ? 代表每天的中午12點觸發(fā)
????????????? sched = new org.quartz.impl.StdSchedulerFactory().getScheduler();
????????????? sched.scheduleJob(jobDetail,trigger);
????????????? sched.start();
?????? }
?????? //停止
?????? public static void stop() throws Exception{
????????????? sched.shutdown();
??????? }
}
//執(zhí)行
public class Main{
?????? ………….
?????? public void run(){
??????????? LzstoneMain.run();
?????? }
?????? …………
}
2.2)web監(jiān)聽方式
示例代碼:
package org.lzstone.action
public class LzstoneMainListener implements ServletContextListener{
?????? private Timer timer = null;
?????? //初始化監(jiān)聽器,創(chuàng)建實例,執(zhí)行任務
?????? public void contextInitialized(ServletContextEvent event){
?????????????? LzstoneMain.run();
?????? }
?????? //銷毀監(jiān)聽器,停止執(zhí)行任務
?????? public void contextDestroyed(ServletContextEvent event){
????????????? LzstoneMain.stop();
??????? }
}
web.xml配置
<listener>
?? <listener-class>
??????? org.lzstone.action.LzstoneMainListener
?? </listener-class>
</listener>
5.對比
Timer方式實現定時器,原理簡單,實現方便,在執(zhí)行簡單的任務比較方便,不足之處是無法確定執(zhí)行時間,并且依賴性比較強,必須繼承指定的類
Quartz方式實現定時器,方便,清晰指定啟動時間,定時參數比較靈活,容易實現比較復雜的定時任務,不足之處是需要實現特定接口,加載其框架
兩種方式各有優(yōu)缺點,在特定場合可以根據其特點選擇使用。
6.spring定時任務
Spring定時任務對Timer與Quartz都提供了支持,并且實現步驟基本一樣
首先配置Spring對Timer的支持
1.1 創(chuàng)建定時任務類
package org.lzstone.action
import java.util.TimeTask
public class LzstoneTimeTask extends TimeTask{
?????? public void run(){
????????????? //執(zhí)行的定時器任務
?????? }
}
1.2 注冊定時任務類,配置任務計劃與任務調度器
??? 在項目的WEB-INF下面創(chuàng)建TimerConfig.xml文件(一般叫做applicationContext.xml)
<?xml version=”1.0” encoding=”UTF-8”?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “http://www.springframework.org/dtd/spring-beans.dtd”>
<beans>
<bean>
<!–注冊定時執(zhí)行任務實體–>
<bean id=”lzstoneTimeTask” class=”org.lzstone.action.LzstoneTimeTask”/>
<!–注冊定時器信息–>
<bean id=”taskInfo” class=”org.springframework.scheduling.timer.ScheduledTimerTask”>
<!–第一次執(zhí)行任務前需要等待的時間,這里設置為3秒–>
<property name=”delay”>
<value>3000</value>
</property>
<!–設置任務的執(zhí)行周期 這里設置為4秒–>
<property name=”period”>
? <value>4000</value>
</property>
<!–設置具體執(zhí)行的任務 這里設置為lzstoneTimeTask–>
<property name=”timerTask”>
<ref local=”lzstoneTimeTask”/>
</property>
</bean>
<!–配置定時器任務的調度器–>
<bean id=”timerFactory” class=”org.springframework.scheduling.timer.TimerFactoryBean”>
<!–注冊定時器列表–>
<property name=”scheduledTimerTasks”>
??? <list>
??????? <ref local=”taskInfo”/>
??????? ……..
??? </list>
</property>
</bean>
</beans>
1.3 web項目中的啟動設置
??? <context-param>
????? <param-name>contextConfigLocation</param-name>
????? <param-value>/WEB-INF/TimerConfig.xml</param-value>
???? </context-param>
???? <listener>
???????? <listener-class>
????????????????? org.springframework.web.context.ContextLoaderListener
???????? </listener-class>
???? </listener>
配置Spring對Quartz的支持
2.1 創(chuàng)建定時任務類
package org.lzstone.action
public class LzstoneQuartzTask{
?????? public void execute(){
????????????? //執(zhí)行的定時器任務
?????? }
}
2.2 注冊定時任務類,配置任務計劃與任務調度器
??? 在項目的WEB-INF下面創(chuàng)建QuartzConfig.xml文件(一般叫做applicationContext.xml)
<?xml version=”1.0” encoding=”UTF-8”?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “http://www.springframework.org/dtd/spring-beans.dtd”>
<beans>
<bean>
<!–注冊定時執(zhí)行任務實體–>
<bean id=”lzstoneQuartzTask” class=”org.lzstone.action.LzstoneQuartzTask”/>
<!–注冊定時器信息–>
<bean id=”taskInfo” class=”org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean”>
<!–指定要執(zhí)行的定時任務類? 這里是LzstoneQuartzTask–>
<property name=”targetObject”>
<ref local=”lzstoneQuartzTask”/>
</property>
<!–指定定時器任務類要執(zhí)行的方法名稱 這里是execute–>
<property name=”targetMethod”>
<value>execute</value>
</property>
</bean>
<!–配置定時器任務的調度器–>
<bean id=”quartzTrigger” class=”org.springframework.scheduling.quartz.CronTriggerBean”>
<!–聲明要運行的實體–>
<property name=”jobDetail”>
??? <ref local=”taskInfo”/>
</property>
<!–設置運行時間–>
<property name=”cronExpression”>
??? <value>0 0 12 * * ?</value>
</property>
</bean>
<!–注冊監(jiān)聽器–>
<bean id=”registerQuartz” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
<!–注冊定時器實體 集合–>
<property name=”triggers”>
??? <list>
????????? <ref local=”quartzTrigger”/>
??? </list>
</property>
</bean>
</beans>
2.3 web項目中的啟動設置
??? <context-param>
????? <param-name>contextConfigLocation</param-name>
????? <param-value>/WEB-INF/QuartzConfig.xml</param-value>
???? </context-param>
???? <listener>
???????? <listener-class>
????????????????? org.springframework.web.context.ContextLoaderListener
???????? </listener-class>
???? </listener>
總結
以上是生活随笔為你收集整理的quartz和timer的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何从12306网站下载自己喜欢的手机铃
- 下一篇: 路由器温度测试软件,[折腾] 夏天别热晕