通過定時器進行任務的執行是比較常見的情況,下面的是個簡單的樣本:
主要藉助於監聽器和TimerTask和Timer類進行實現,另外spring提供了定時器的進階實現好像是quarts。
需要的可以看看。
- package com.rx.timer;
- import java.util.Date;
- import java.util.TimerTask;
- public class MyTask extends TimerTask {
- @Override
- public void run() {
- System.out.println("call at " + (new Date()));
- }
- }
- package com.rx.timer;
- import java.util.Timer;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- public class MyListen implements ServletContextListener {
- private Timer timer = null;
-
- @Override
- public void contextDestroyed(ServletContextEvent arg0) {
- timer.cancel();
- }
- @Override
- public void contextInitialized(ServletContextEvent arg0) {
- timer = new Timer(true);
- //設定任務計劃,啟動和間隔時間
- timer.schedule(new MyTask(), 0, 1000 * 10);
- }
-
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_ID" 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">
- <display-name>
- WebTimer</display-name>
-
- <listener>
- <listener-class>com.rx.timer.MyListen</listener-class>
- </listener>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
- 2008-11-20 20:52:35 org.apache.tomcat.util.digester.SetPropertiesRule begin
- 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:WebTimer' did not find a matching property.
- 2008-11-20 20:52:35 org.apache.catalina.core.AprLifecycleListener init
- 資訊: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:/Program Files/Java/jre1.6.0_07/bin;.;C:/WINDOWS/Sun/Java/bin;C:/WINDOWS/system32;C:/WINDOWS;C:/Program Files/Java/jre1.6.0_07/bin/client;C:/Program Files/Java/jre1.6.0_07/bin;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;C:/Program Files/Microsoft SQL Server/90/Tools/binn/;C:/PROGRA~1/IBM/SQLLIB/BIN;C:/PROGRA~1/IBM/SQLLIB/FUNCTION;C:/PROGRA~1/IBM/SQLLIB/SAMPLES/REPL;C:/IBM/Installation Manager/eclipse/lib;C:/Program Files/MySQL/MySQL Server 4.1/bin;C:/Program Files/TortoiseSVN/bin;C:/Program Files/Subversion/bin;C:/Program Files/StormII/Codec;C:/Program Files/StormII;C:/Program Files/IDM Computer Solutions/UltraEdit/
- 2008-11-20 20:52:35 org.apache.coyote.http11.Http11Protocol init
- 資訊: Initializing Coyote HTTP/1.1 on http-8080
- 2008-11-20 20:52:35 org.apache.catalina.startup.Catalina load
- 資訊: Initialization processed in 795 ms
- 2008-11-20 20:52:35 org.apache.catalina.core.StandardService start
- 資訊: Starting service Catalina
- 2008-11-20 20:52:35 org.apache.catalina.core.StandardEngine start
- 資訊: Starting Servlet Engine: Apache Tomcat/6.0.18
- 2008-11-20 20:52:36 org.apache.catalina.loader.WebappClassLoader validateJarFile
- 資訊: validateJarFile(D:/eclipseworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/WebTimer/WEB-INF/lib/servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
- call at Thu Nov 20 20:52:36 CST 2008
- 2008-11-20 20:52:36 org.apache.coyote.http11.Http11Protocol start
- 資訊: Starting Coyote HTTP/1.1 on http-8080
- 2008-11-20 20:52:36 org.apache.jk.common.ChannelSocket init
- 資訊: JK: ajp13 listening on /0.0.0.0:8009
- 2008-11-20 20:52:36 org.apache.jk.server.JkMain start
- 資訊: Jk running ID=0 time=0/31 config=null
- 2008-11-20 20:52:36 org.apache.catalina.startup.Catalina start
- 資訊: Server startup in 654 ms
- call at Thu Nov 20 20:52:46 CST 2008
- call at Thu Nov 20 20:52:56 CST 2008
- call at Thu Nov 20 20:53:06 CST 2008
- call at Thu Nov 20 20:53:16 CST 2008
- call at Thu Nov 20 20:53:26 CST 2008
- call at Thu Nov 20 20:53:36 CST 2008
- call at Thu Nov 20 20:53:46 CST 2008
- call at Thu Nov 20 20:53:56 CST 2008
- call at Thu Nov 20 20:54:06 CST 2008
- call at Thu Nov 20 20:54:16 CST 2008
- call at Thu Nov 20 20:54:26 CST 2008
- call at Thu Nov 20 20:54:36 CST 2008
- call at Thu Nov 20 20:54:46 CST 2008
- call at Thu Nov 20 20:54:56 CST 2008
- call at Thu Nov 20 20:55:06 CST 2008
- call at Thu Nov 20 20:55:16 CST 2008
- call at Thu Nov 20 20:55:26 CST 2008
- call at Thu Nov 20 20:55:36 CST 2008
- call at Thu Nov 20 20:55:46 CST 2008
- call at Thu Nov 20 20:55:56 CST 2008
- call at Thu Nov 20 20:56:06 CST 2008
- call at Thu Nov 20 20:56:16 CST 2008
- call at Thu Nov 20 20:56:26 CST 2008
- call at Thu Nov 20 20:56:36 CST 2008
- call at Thu Nov 20 20:56:46 CST 2008
- call at Thu Nov 20 20:56:56 CST 2008
- call at Thu Nov 20 20:57:06 CST 2008
- call at Thu Nov 20 20:57:16 CST 2008