RSS
热门关键字:  下载  cms  模版  开源  dedecms
当前位置 :| 主页 > 站长学院 > 数据库 > Oracle >

WEB 计划任务

来源:Blog.ChinaUnix.net 作者:未知 时间:2006-09-14 Tag: 点击:
在tomcat中可以注册ServletContextListener,

public void contextInitialized(ServletContextEvent sce);
public void contextDestroyed(ServletContextEvent sce);

允许程序在系统启动和关闭的时候作一些工作。我把线程的启动和关闭都放在这里了,这样系统在reload的时候,也会调用ServletContextListener的方法。

package market;

/**
 * 侦听器程序测试
 */
public class marketListener implements javax.servlet.ServletContextListener {
    private java.util.Timer timer;

    public marketListener() {
        System.out.println( "startup init" );
        timer = new java.util.Timer( true );
    }

    public void contextDestroyed( javax.servlet.ServletContextEvent event ) {
        System.out.println( "destory" );
        timer.cancel();
    }

    public void contextInitialized( javax.servlet.ServletContextEvent event ) {
        System.out.println( "start" );
        System.out.println( event.getServletContext().getRealPath( "/" ) );
        timer.schedule( new java.util.TimerTask() {
            public void run() {
                System.out.println( "TimerTask run..." );
            }
        } , 0 , 1000 );
    }

}
对web.xml的配置
<listener>
    <listener-class>market.marketListener</listener-class>
</listener>

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
栏目列表