It is common to execute tasks by using a timer. The following is a simple example:
The listener, timertask, and timer classes are used for implementation. In addition, Spring provides advanced timer implementation, which seems to be quarts.
For more information, see.
- 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 = NULL;
- @ Override
- Public void contextdestroyed (servletcontextevent arg0 ){
- Timer. Cancel ();
- }
- @ Override
- Public void contextinitialized (servletcontextevent arg0 ){
- Timer = new timer (true );
- // Set the task schedule, start time, and interval.
- 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
- Warning: [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
- Information: 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/MIC Rosoft 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 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/
- 20:52:35 org. Apache. Coyote. http11.http11protocol init
- Information: initializing coyote HTTP/1.1 on http-8080
- 2008-11-20 20:52:35 org. Apache. Catalina. startup. Catalina Load
- Information: initialization processed in 795 MS
- 2008-11-20 20:52:35 org. Apache. Catalina. Core. standardservice start
- Information: starting service Catalina
- 2008-11-20 20:52:35 org. Apache. Catalina. Core. standardengine start
- Information: Starting Servlet Engine: Apache Tomcat/6.0.18
- 20:52:36 org. Apache. Catalina. loader. webappclassloader validatejarfile
- Information: 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. class
- Call at Thu Nov 20 20:52:36 CST 2008
- 20:52:36 org. Apache. Coyote. http11.http11protocol start
- Information: Starting coyote HTTP/1.1 on http-8080
- 20:52:36 org. Apache. JK. Common. channelsocket init
- Information: JK: ajp13 listening on/0.0.0.0: 8009
- 2008-11-20 20:52:36 org. Apache. JK. server. jkmain start
- Description: JK running id = 0 time = 0/31 Config = NULL
- 2008-11-20 20:52:36 org. Apache. Catalina. startup. Catalina start
- Information: 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