Tomcat中设置监听类定时执行程序

fmms 13年前
     首先建立一个监听类:    <br />      <pre class="brush:java; toolbar: true; auto-links: false;">    import   java.util.Timer;      import   javax.servlet.ServletContextEvent;      import   javax.servlet.ServletContextListener;           public   class   TestListener   implements   ServletContextListener   {                            private   Timer   timer   =   null;                             public   void   contextInitialized(ServletContextEvent   event)   {                       timer   =   new   Timer(true);                       timer.schedule(new   TestTimer(),   0,   2000);//milliseconds                }                         public   void   contextDestroyed(ServletContextEvent   event)   {                                               timer.cancel();                }        }             </pre>    <br /> 新建一个任务类    <br />      <br />    <pre class="brush:java; toolbar: true; auto-links: false;">      import   java.util.TimerTask;        public   class   TestTimer   extends   TimerTask   {                              public   void   run()   {                     System.out.println("ssss");              }        }</pre>    <br /> 配置xml信息    <br />    <pre class="brush:xml; toolbar: true; auto-links: false;">        <listener>             <listener-class> test.TestListener </listener-class>         </listener></pre>    <br />