Natural

           

          Java中用Servlet Listener實現定時監聽

          分兩步走:

              (1)實現 javax.servlet.ServletContextListener 接口的兩個方法:contextInitialized()和contextDestroyed()

              contextInitialized():當Servlet容器啟動時會執行contextDestroyed():當Servlet容器停止時會執行

              (2)在contextInitialized()中加入需要監聽的程序,并由 java.util.Timer 的 schedule() 方法來控制監聽程序執行的頻率

             

          DEMO(這是我的監聽的程序原型)

          package com.company.servlet;

          import java.util.Calendar;
          import java.util.GregorianCalendar;
          import java.util.Timer;

          import javax.servlet.ServletContextEvent;
          import javax.servlet.ServletContextListener;

          import com.company.task.ClearApplicationAttributeTask;

          public class TaskListener implements ServletContextListener {
              
              
          private static Timer timer = null;
              
          private static ClearApplicationAttributeTask caaTask = null;
              
          public void contextDestroyed(ServletContextEvent arg0) {
                  
          //終止此計時器,丟棄所有當前已安排的任務
                  if(timer != null)
                      timer.cancel();
              }

              
          public void contextInitialized(ServletContextEvent arg0) {
                  caaTask 
          = new ClearApplicationAttributeTask(arg0.getServletContext());
                  timer 
          = new Timer(true);
                  
                  
          // 定義任務時間,每天0時執行
                  GregorianCalendar now = new GregorianCalendar();
                  now.set(Calendar.HOUR, 
          0);
                  now.set(Calendar.MINUTE, 
          0);
                  now.set(Calendar.SECOND, 
          0);
                  timer.schedule(caaTask, now.getTime());
              }

          }


          package com.company.task;

          import java.util.TimerTask;

          import javax.servlet.ServletContext;

          public class ClearApplicationAttributeTask extends TimerTask {
              
          private ServletContext context = null;
              
              
          public ClearApplicationAttributeTask(ServletContext sc)
              {
                  
          this.context = sc;
              }
              
          public void run() 
              {

                  
          // 在這里可以做一些想做的事情,比如清空application中的屬性
                  context.removeAttribute("app_name");
              }

          }

          將編譯好的class文件放入WEB-INF/classes中,最后別忘記了在Servlet容器中當前WEB應用的web.xml中加入監聽語句:

            <listener>
                
          <listener-class>com.company.servlet.TaskListener</listener-class>
            
          </listener>


          posted on 2010-03-11 12:51 此號已被刪 閱讀(1087) 評論(0)  編輯  收藏 所屬分類: J2EE

          導航

          統計

          常用鏈接

          留言簿(8)

          隨筆分類(83)

          隨筆檔案(78)

          文章檔案(2)

          相冊

          收藏夾(7)

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 扬州市| 天柱县| 珠海市| 达孜县| 康定县| 澎湖县| 昆山市| 信宜市| 措美县| 林口县| 荥阳市| 保康县| 宜章县| 台江县| 通城县| 平顶山市| 天峨县| 桓台县| 海伦市| 南开区| 措美县| 东山县| 青冈县| 宾川县| 南乐县| 陵水| 安化县| 大宁县| 九寨沟县| 睢宁县| 枣强县| 绩溪县| 揭阳市| 邯郸县| 鸡东县| 福鼎市| 凤阳县| 神农架林区| 全椒县| 苏州市| 辽阳市|