首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 认证考试 > JAVA认证 >

Java定时器Web

2008-12-01 
借助于监听器和TimerTask和Timer类进行实现,另外spring提供了定时器的高级实现好像是quarts。

    通过定时器进行任务的执行是比较常见的情况,下面的是个简单的示例:
  主要借助于监听器和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

 

3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/

热点排行