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 此號已被刪 閱讀(1083) 評論(0)  編輯  收藏 所屬分類: J2EE

          導航

          統計

          常用鏈接

          留言簿(8)

          隨筆分類(83)

          隨筆檔案(78)

          文章檔案(2)

          相冊

          收藏夾(7)

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 醴陵市| 苏尼特左旗| 元江| 浦北县| 同德县| 湖北省| 安多县| 昔阳县| 依安县| 蒙城县| 武平县| 梅河口市| 商丘市| 介休市| 昌宁县| 柘城县| 清水河县| 勐海县| 仪征市| 师宗县| 桓台县| 连州市| 冀州市| 全州县| 台中县| 密山市| 婺源县| 宜章县| 托克逊县| 卫辉市| 射洪县| 若羌县| 澎湖县| 伊金霍洛旗| 蓝山县| 汉中市| 从江县| 扶沟县| 鸡西市| 健康| 合川市|