启动tomcat的同时 启动其他线程

来源:互联网 发布:js audio play 编辑:程序博客网 时间:2024/06/03 12:23

实现ServletContextListener接口,并覆盖其中的两个方法:

public class StartMonitorMain implements ServletContextListener {
   
    public void contextInitialized(ServletContextEvent arg0) {
        try {
            while (true) {
                Thread monitor = new MonitorThread();
                monitor.start();

                while (true) {
                    if (!monitor.isAlive()) {
                        break;
                    }
                }
                Thread.sleep(3 * 60 * 1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub

    }

 

}

 

web.xml中添加侦听器:

<listener>
        <listener-class>cn.golaxy.ws.main.StartMonitorMain</listener-class>
</listener>

原创粉丝点击