java web 定时器

JAVA WEB定时器,定时器的启动一般随web server的启动而启动,一般有两种方法.

方法一:在web.xml里配置一个Servlet,继承HttpServlet,并设置其随web server的启动而启动。然后在该Servlet的init()方法里启动定时器,在destory()方法里销毁定时器。


方法二:在web.xml里配置一个Listener,继承ServletContextListener,然后在该Listener的初始化方法里启动定时器,在其销毁的方法朝左销毁定时器。

在servlet中启动定时器

一,继承HttpServlet的写法:

1,Servlet

  1. import java.io.IOException;   
  2. import java.util.Timer;   
  3. import javax.servlet.RequestDispatcher;   
  4. import javax.servlet.ServletContext;   
  5. import javax.servlet.ServletException;   
  6. import javax.servlet.http.HttpServlet;   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;   
  9.   
  10. import org.apache.commons.lang.StringUtils;   
  11.   
  12. public class ConvergeDataServlet extends HttpServlet {   
  13.   
  14.     private static final long serialVersionUID = 1L;   
  15.   
  16.     private Timer timer1 = null;   
  17.        
  18.     private Task task1;   
  19.        
  20.     /**
  21.       * Constructor of the object.
  22.       */  
  23.     public ConvergeDataServlet() {   
  24.         super();   
  25.      }   
  26.   
  27.     /**
  28.       * Destruction of the servlet.
  29.       */  
  30.     public void destroy() {   
  31.         super.destroy();   
  32.         if(timer1!=null){   
  33.              timer1.cancel();   
  34.          }   
  35.      }   
  36.   
  37.        
  38.     public void doGet(HttpServletRequest request, HttpServletResponse response)   
  39.             throws ServletException, IOException {   
  40.            
  41.      }   
  42.   
  43.        
  44.     public void doPost(HttpServletRequest request, HttpServletResponse response)   
  45.             throws ServletException, IOException {   
  46.          doGet(request, response);          
  47.      }   
  48.   
  49.     // init方法启动定时器   
  50.     public void init() throws ServletException {   
  51.            
  52.          ServletContext context = getServletContext();   
  53.            
  54.         // (true为用定时间刷新缓存)   
  55.          String startTask = getInitParameter("startTask");   
  56.                    
  57.         // 定时刷新时间(分钟)   
  58.          Long delay = Long.parseLong(getInitParameter("delay"));   
  59.            
  60.         // 启动定时器   
  61.         if(startTask.equals("true")){   
  62.              timer1 = new Timer(true);   
  63.              task1 = new Task(context);   
  64.              timer1.schedule(task1, delay * 60 * 1000, delay * 60 * 1000);   
  65.          }   
  66.      }   
  67. }   

2,任务

  1. import java.util.TimerTask;   
  2.   
  3. import javax.servlet.ServletContext;   
  4.   
  5. public class Task extends TimerTask{   
  6.   
  7.     private ServletContext context;   
  8.        
  9.     private static boolean isRunning = true;   
  10.        
  11.     public Task(ServletContext context){   
  12.         this.context = context;   
  13.      }   
  14.        
  15.            
  16.     @Override  
  17.     public void run() {   
  18.         if(isRunning){   
  19.              
  20.          }   
  21.      }   
  22.   
  23. }   

3,web.xml中的配置

< servlet >
     < servlet-name >taskservlet< / servlet-name >
    < servlet-class >com.task< /servlet-class >
    < init-param >
    < param-name >startTask< /param-name >
    < param-value >true< /param-value >
    < /init-param >
    < init-param >
    < param-name >intervalTime< /param-name >
    < param-value >1< /param-value >
    < /init-param >
    < load-on-startup >300< /load-on-startup >
< /servlet >

二,继承ServletContextListener

1,定时器

/**
*
*/
package com.annlee.website.util;

import java.util.Timer;

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

/**
* @author Administrator 定时器
*/
public class TimerListener implements ServletContextListener {

private Timer timer;
private AnnleeTimerTask task;

public void contextDestroyed(ServletContextEvent arg0) {
   timer.cancel();
   System.out.println("定时器已销毁");
}

public void contextInitialized(ServletContextEvent event) {

   timer = new java.util.Timer(true);
   task = new AnnleeTimerTask(event.getServletContext());
   System.out.println("定时器已启动");
   timer.schedule(task, 0, 60 * 1000);
   System.out.println("已经添加任务调度表");
}

}

2,任务

package com.annlee.website.util;

import java.util.Calendar;
import java.util.TimerTask;

import javax.servlet.ServletContext;

import com.annlee.website.model.service.member.UsmAccessDataService;

/**
* 定时器的任务
*/
public class AnnleeTimerTask extends TimerTask {

private ServletContext context;
    private static boolean isRunning = false;
    private static boolean flag = true;
    private static final int C_SCHEDULE_HOUR = 0;
    private UsmAccessDataService accessDataService;
  
    public AnnleeTimerTask(ServletContext context){
     this.context = context;
    }

    public void run() {
    accessDataService = new UsmAccessDataService();
        Calendar cal = Calendar.getInstance();
        if (!isRunning) {
            if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY) && flag) {
                isRunning = true;
            //    accessDataService.SendHttpByMonth(0,0, 0);
                System.out.println("正在执行任务");
                isRunning = false;
                flag = false;
                context.log("指定任务执行结束");
            }
        } else {
            context.log("上一次任务执行还未结束");
        }
        if(C_SCHEDULE_HOUR != cal.get(Calendar.HOUR_OF_DAY)){
            flag = true;
        }
      }

}

3,web.xml中的配置


  
    com.annlee.website.util.TimerListener
  

你可能感兴趣的:(java基础,jsp)