Java监听器执行定时任务

来源:互联网 发布:中国进出口最新数据 编辑:程序博客网 时间:2024/04/30 08:27

目前 Servlet2.4和JSP2.0常用的有7个监听器接口,分为3类:

1. Servlet上下文进行监听(Application级):

用于监听 ServletContext 对象的创建和删除以及属性的添加、删除和修改等操作,该监听器需要用到如下两个接口类:

(1) ServletContextAttributeListener:监听对ServletContext 属性的操作,比如增加、删除、修改

     attributeAdded(ServletContextAttributeEvent e)          添加属性时调用   

     attributeReplaced(ServletContextAttributeEvent e)        修改属性时调用   

     attributeRemoved(ServletContextAttributeEvent e)        删除属性时调用

 

(2) ServletContextListener:监听对ServletContext  对象的创建和删除

     contextInitialized(ServletContextEvent sce)          服务器启动时调用

     contextDestroyed(ServletContextEvent sce)         服务器关闭时调用

 

2. 监听HTTP会话(Session级):

用于监听 HTTP 会话活动情况和 HTTP 会话中的属性设置情况,也可以监听 HTTP 会话的 active 和 passivate 情况等,该监听器需要用到如下多个接口类:

 

(1) HttpSessionListener:监听HttpSession 的操作

     sessionCreate(HttpSessionEvent se)      客户端第一次请求时调用

     sessionDestroyed(httpSessionEvent se)   用户注销(session过期)时调用

 

(2) HttpSessionActivationListener:用于监听HTTP会话的active和passivate情况

 

(3) HttpSessionAttributeListener:监听HttpSession中的属性操作

     attributeAdded(HttpSessionBindingEvent se)       添加属性时调用

     attributeRemoved(HttpSessionBindingEvent se)    删除属性时调用

     attributeReplaced(HttpSessionBindingEvent se)    修改属性时调用

 

3. 对客户端请求进行监听(Requst级):

用于对客户端的请求进行监听是在 Servlet2.4 规范中新添加的一项新技术,使用的接口如下:

(1) ServletRequestListener 接口类

      requestDestroyed(ServletRequestEvent e)       对销毁客户端进行监听,即当执行request.removeAttribute("xxx") 时调用

      requestInitialized(ServletRequestEvent e)         对实现客户端的请求进行监听

 

 (2)ServletRequestAttributeListener 接口类

      attributeAdded(ServletRequestAttributeEvent e)       对属性添加进行监听

      attributeRemoved(ServletRequestAttributeEvent e)    对属性删除进行监听

      attributeReplaced(ServletRequestAttributeEvent e)    对属性替换进行监听


那么如何用监听器实现定时任务的执行呢:

(1)任务管理类:

public class TimerManager {// 时间间隔private static final long PERIOD_DAY = 24 * 60 * 60 * 1000;public TimerManager() {Calendar calendar = Calendar.getInstance();/** * 定制每日2:00执行方法 ** */calendar.set(Calendar.HOUR_OF_DAY, 20);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);Date date = calendar.getTime(); // 第一次执行定时任务的时间// 如果第一次执行定时任务的时间 小于 当前的时间// 此时要在 第一次执行定时任务的时间 加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。if (date.before(new Date())) {date = this.addDay(date, 1);}Timer timer = new Timer();MyTimerTask task = new MyTimerTask();// 安排指定的任务在指定的时间开始进行重复的固定延迟执行(或其他执行类型)。timer.schedule(task, date, PERIOD_DAY);}// 增加或减少天数public Date addDay(Date date, int num) {Calendar startDT = Calendar.getInstance();startDT.setTime(date);startDT.add(Calendar.DAY_OF_MONTH, num);return startDT.getTime();}}

(2)任务类:

public class MyTimerTask extends TimerTask {// 要执行的内容}
(3)监听器类:
public class NFDFlightDataTaskListener extends HttpServlet implements ServletContextListener {public void contextInitialized(ServletContextEvent event) {new TimerManager();}public void contextDestroyed(ServletContextEvent event) {}}
(4)Web.xml中添加监听器:

<listener>  <listener-class>  com.sinosoft.lis.leaveMessage.NFDFlightDataTaskListener</listener-class>  </listener>

0 0