<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0048)http://developer.51cto.com/art/200906/129088.htm -->
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>Spring定时器的两种实现方式 - 51CTO.COM</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=x-ua-compatible content=ie=7>
<META
content="本文向您介绍Spring定时器的两种实现方式,包括Java Timer定时和Quartz定时器,两种Spring定时器的实现方式各有优点,可结合具体项目考虑是否采用。"
name=description>
<META content=Spring定时器 name=keywords><LINK
href="Spring定时器的两种实现方式%20-%2051CTO_COM_files/news_09.css" type=text/css
rel=stylesheet><LINK
href="Spring定时器的两种实现方式%20-%2051CTO_COM_files/master_top.css" type=text/css
rel=stylesheet>
<SCRIPT language=javaScript
src="Spring定时器的两种实现方式%20-%2051CTO_COM_files/validate2.js"></SCRIPT>
<SCRIPT language=javaScript
src="Spring定时器的两种实现方式%20-%2051CTO_COM_files/fb_words_list.htm"></SCRIPT>
<SCRIPT language=javaScript
src="Spring定时器的两种实现方式%20-%2051CTO_COM_files/base6.js"></SCRIPT>
<SCRIPT language=javaScript
src="Spring定时器的两种实现方式%20-%2051CTO_COM_files/dialog_utf8.js"></SCRIPT>
<SCRIPT language=javaScript
src="Spring定时器的两种实现方式%20-%2051CTO_COM_files/mail.js"></SCRIPT>
<SCRIPT language=javascript
src="Spring定时器的两种实现方式%20-%2051CTO_COM_files/article.js"></SCRIPT>
<SCRIPT src="Spring定时器的两种实现方式%20-%2051CTO_COM_files/index_1_down.js"></SCRIPT>
<SCRIPT>
var thistid=129088;
</SCRIPT>
<META content="MSHTML 6.00.5730.13" name=GENERATOR></HEAD>
<BODY onresize=set_sreach() onload=set_sreach()>
<DIV class=top>
<DIV class=top_nav>
<DIV class=top_nav_logo title="51CTO.COM - 技术成就梦想 - 中国领先的IT技术网站"></DIV>
<DIV class=top_nav_r><A href="http://www.51cto.com/">51CTO首页</A> | <A
href="http://news.51cto.com/">新闻</A> | <A
href="http://www.51cto.com/col/35">专题</A> | <A
href="http://bbs.51cto.com/">论坛</A> | <A href="http://blog.51cto.com/">博客</A> |
<A href="http://down.51cto.com/">下载</A> | <A href="http://g.51cto.com/">技术圈</A>
| <A href="http://book.51cto.com/">读书</A> | <A class=top_nav_r1
onmouseover=show(1,this) onmouseout=popupClose()
href="http://www.51cto.com/col/35/">技术频道 </A>| <A
href="http://www.cioage.com/">CIO </A>| <A href="http://www.watchstor.com/">存储
</A>| <A href="http://www.hc3i.cn/">HC3i </A>| <A
href="http://www.51cto.com/about/map.htm">地图</A> </DIV></DIV></DIV>
<DIV class=subMenu1 id=s1 onmouseover=popupNoClose() onmouseout=popupClose()>
<UL>
<LI><A href="http://network.51cto.com/">组网</A>
<LI><A href="http://netsecurity.51cto.com/">安全</A>
<LI><A href="http://developer.51cto.com/">开发</A>
<LI><A href="http://database.51cto.com/">数据库</A>
<LI><A href="http://server.51cto.com/">服务器</A>
<LI><A href="http://os.51cto.com/">系统</A>
<LI><A href="http://virtual.51cto.com/">虚拟化</A>
<LI><A href="http://cloud.51cto.com/">云计算</A>
<LI><A href="http://developer.51cto.com/embed/">嵌入式</A> </LI></UL></DIV>
<DIV class=subMenu1 id=s1 onmouseover=popupNoClose() onmouseout=popupClose()>
<UL>
<LI><A href="http://network.51cto.com/">组网建网</A>
<LI><A href="http://netsecurity.51cto.com/">网络安全</A>
<LI><A href="http://server.51cto.com/">服务器</A>
<LI><A href="http://os.51cto.com/">操作系统</A>
<LI><A href="http://virtual.51cto.com/">虚拟化</A>
<LI><A href="http://developer.51cto.com/">开发</A></LI></UL></DIV>
<DIV class=subMenu2 id=s2 onmouseover=popupNoClose( onmouseout=popupClose() )>
<UL>
<LI><A href="http://www.cioage.com/news">资讯前沿</A>
<LI><A href="http://www.cioage.com/insight">业界观察</A>
<LI><A href="http://www.cioage.com/exp">应用体验</A>
<LI><A href="http://www.cioage.com/tech">杀手技术</A></LI></UL></DIV>
<DIV class=subMenu3 id=s3 onmouseover=popupNoClose() onmouseout=popupClose()>
<UL>
<LI><A href="http://news.watchstor.com/">新闻资讯</A>
<LI><A href="http://tech.watchstor.com/">技术中心</A>
<LI><A href="http://video.watchstor.com/">互动视频</A>
<LI><A href="http://special.watchstor.com/">专题汇聚</A></LI></UL></DIV>
<DIV class=BR_8></DIV>
<DIV class=banner>
<DIV class=banner_l id=tonglanad></DIV>
<DIV class=banner_r>
<UL>
<LI id=wordlinkad1>
<LI id=wordlinkad2>
<LI id=wordlinkad3>
<LI id=wordlinkad4></LI></UL></DIV></DIV>
<DIV class=BR_8></DIV>
<DIV id=subNav>
<DIV class=subNav_logo><IMG
src="Spring定时器的两种实现方式%20-%2051CTO_COM_files/kaifa_logo.gif"></DIV>
<DIV class=subNav_l><A href="http://developer.51cto.com/">首页</A> | <A
href="http://developer.51cto.com/java/">Java</A> | <A
href="http://developer.51cto.com/net/">.NET</A> | <A
href="http://developer.51cto.com/web/">Web</A> | <A
href="http://developer.51cto.com/col/1315/">XML</A> | <A
href="http://developer.51cto.com/col/585/">语言工具</A> | <A
href="http://developer.51cto.com/col/1312/">测试</A> | <A
href="http://developer.51cto.com/col/1314/" target=_blank>游戏</A> | <A
href="http://developer.51cto.com/col/583/" target=_blank>移动</A> | <A
href="http://developer.51cto.com/col/1308/">架构</A> | <A
href="http://developer.51cto.com/col/586/">项目管理</A> | <A
href="http://publish.51cto.com/list/460/">全部文章</A></DIV>
<DIV class=search>
<FORM
style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"
name=searchform action=http://www.51cto.com/php/search.php method=post>
<DL>
<DT><INPUT class=new_te1 name=keyword> </DT>
<DD><INPUT type=image
src="Spring定时器的两种实现方式%20-%2051CTO_COM_files/search.gif"></DD></DL></FORM></DIV></DIV>
<DIV class=BR_10></DIV>
<DIV id=container>
<DIV id=left>
<DIV class=title>
<DIV class=breadcrumb>您所在的位置:<A href="http://www.51cto.com/"
target=_blank>首页</A> > <A href="http://developer.51cto.com/">开发</A> > <A
href="http://developer.51cto.com/col/461/">Java</A> > <A
href="http://developer.51cto.com/col/1014/">Java EE</A> > </DIV>
<DIV class=con>
<UL>
<LI>
<H1>Spring定时器的两种实现方式</H1>
<LI class=msg><SPAN><A
href="http://developer.51cto.com/">http://developer.51cto.com/</A>
2009-06-15 15:02 佚名 Javaeye <A
href="http://www.51cto.com/php/feedbackt.php?id=129088">我要评论(<SPAN
id=pinglun_1>0</SPAN>)</A></SPAN> </LI></UL></DIV></DIV>
<DIV class=BR_3></DIV>
<DIV class=container>
<DIV class=sum>
<UL class=summary>本文向您介绍Spring定时器的两种实现方式,包括Java
Timer定时和Quartz定时器,两种Spring定时器的实现方式各有优点,可结合具体项目考虑是否采用。</UL></DIV>
<UL class=con>
<P>
<P>有两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz。<BR><BR><STRONG>1.Java
Timer定时</STRONG> </P>
<P>首先继承java.util.TimerTask类实现run方法 </P><PRE><OL class=dp-j><LI class=alt><SPAN><SPAN class=keyword>import</SPAN><SPAN> java.util.TimerTask; </SPAN></SPAN><LI><SPAN class=keyword>public</SPAN><SPAN> </SPAN><SPAN class=keyword>class</SPAN><SPAN> EmailReportTask </SPAN><SPAN class=keyword>extends</SPAN><SPAN> TimerTask{ </SPAN><LI class=alt><SPAN> </SPAN><SPAN class=annotation>@Override</SPAN><SPAN> </SPAN><LI><SPAN> </SPAN><SPAN class=keyword>public</SPAN><SPAN> </SPAN><SPAN class=keyword>void</SPAN><SPAN> run() { </SPAN><LI class=alt><SPAN> ... </SPAN><LI><SPAN> } </SPAN><LI class=alt><SPAN>} </SPAN></LI></OL></PRE>
<P><STRONG>在Spring定义</STRONG> </P>
<P><EM><BEAN class=EmailReportTask id=reportTimerTask>...</BEAN></EM> </P>
<P><STRONG>配置Spring定时器</STRONG> </P><PRE><OL cl