导航

利用ServletContextListener实现定时任务

Posted on 2013-11-25 15:26  酷鱼影子  阅读(553)  评论(0编辑  收藏  举报

package com.ed.cnc.servletListener;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客import javax.servlet.ServletContextEvent;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客import javax.servlet.ServletContextListener;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客import com.ed.cnc.city.StatisticsTask;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客/** *//**

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客 * 统计ContextListener

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客 * @author westd

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客 *

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客 */

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客/** *//**

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客 * @author westd

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客 *

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客 */

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客public class StatisticsContextListener implements ServletContextListener

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客{

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    private java.util.Timer timer = null;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    /** *//**

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客     * 这个方法在Web应用服务做好接受请求的时候被调用。

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客     * 

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客     * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客     */

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    public void contextInitialized(ServletContextEvent event) 

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客{

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        timer = new java.util.Timer(true);

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        event.getServletContext().log("定时器已启动"); 

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        timer.schedule(new StatisticsTask(event.getServletContext()), 0, 60*60*1000);//每隔1小时

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        event.getServletContext().log("已经添加任务调度表");

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    }

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    /** *//**

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客     * 这个方法在Web应用服务被移除,没有能力再接受请求的时候被调用。

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客     * 

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客     */

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    public void contextDestroyed(ServletContextEvent event)

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客{

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        timer.cancel();

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        event.getServletContext().log("定时器销毁");

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    }

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客}

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

一个继承于TimerTask的一个类:StatisticsTask.java

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客package com.ed.cnc.city;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客import java.util.Calendar;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客import java.util.TimerTask;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客import javax.servlet.ServletContext;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客/** *//**

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客 * 统计任务

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客 * @author westd

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客 *

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客 */

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客public class StatisticsTask extends TimerTask

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客{

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    private static final int STATISTICS_SCHEDULE_HOUR = 0;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    private static boolean isRunning = false;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    private ServletContext context = null;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    public StatisticsTask(ServletContext context)

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客{

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        this.context = context;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    }

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    @Override

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    public void run()

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客{

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        Calendar cal = Calendar.getInstance(); 

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        //System.out.println(isRunning);

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        if (!isRunning) 

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客            if (STATISTICS_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY)) //查看是否为凌晨

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客            利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客                isRunning = true; 

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客                context.log("开始执行指定任务");

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客                

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客                //TODO 添加自定义的详细任务

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客                executeTask();

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客                

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客                //指定任务执行结束

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客                isRunning = false;

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客                context.log("指定任务执行结束"); 

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客            } 

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        } 

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        else 

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客{

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客            context.log("上一次任务执行还未结束");

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        }

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    }

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    /** *//**

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客     * 执行任务

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客     */

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    public void executeTask()

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客{

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        System.out.println("任务1利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客");

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        System.out.println("任务2利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客");

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客    }

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客}

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客

web.xml中添加如下代码:

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客<listener>

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客        <listener-class>com.ed.cnc.servletListener.StatisticsContextListener</listener-class>

利用ServletContextListener实现定时任务  - 冯冯 - 冯冯的博客</listener>