Java Web项目中定时器的简单运用


Java Web项目中定时器的简单运用:

  1. import java.io.FileNotFoundException;  
  2. import java.io.IOException;  
  3. import java.util.Date;  
  4. import java.util.Timer;  
  5.   
  6. import javax.servlet.ServletConfig;  
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9.   
  10. import org.apache.log4j.PropertyConfigurator;  
  11. public class InitServlet extends HttpServlet {  
  12.   
  13.     private static final long serialVersionUID = -2693899729090015551L;  
  14.   
  15.     public static final String FILE_SEPARATOR = System.getProperties().getProperty("file.separator");   
  16.       
  17.     private static String contextPath;  
  18.       
  19.     private static String serverConfig;  
  20.       
  21.     private static String ftpPath;  
  22.       
  23.     // 添加定时器任务   
  24.     private Timer timer;  
  25.       
  26.     /** 
  27.      * @Override 
  28.      * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig) <BR> 
  29.      * Method name: init <BR> 
  30.      * Description: please write your description <BR> 
  31.      * Remark: <BR> 
  32.      * @param config 
  33.      * @throws ServletException  <BR> 
  34.     */  
  35.     @SuppressWarnings("deprecation")  
  36.     @Override  
  37.     public void init(ServletConfig config) throws ServletException {  
  38.         super.init(config);  
  39.         String prefix = config.getServletContext().getRealPath("/");  
  40.         InitServlet.contextPath = prefix;  
  41.         if(FILE_SEPARATOR.equals("\\")) {  
  42.             // 获取内容服务器配置文件的路径   
  43.             serverConfig = prefix + "\\WEB-INF\\config.properties";  
  44.         } else if(FILE_SEPARATOR.equals("/")) {  
  45.             serverConfig = prefix + "/WEB-INF/config.properties";  
  46.         }  
  47.                   
  48.                   // 处理的是ftp文件位置,具体到哪个文件在Action中处理   
  49.         Property property = new Property(InitServlet.getServerConfig());  
  50.           
  51.         // 读取path信息   
  52.         String path = null;  
  53.         try {  
  54.             path = property.getProperty("path");  
  55.             if(FILE_SEPARATOR.equals("/")) {  
  56.                 // 在Linux环境中判断是否以"/"结尾   
  57.                 if (!("/".equalsIgnoreCase(path.substring(path.length()-1, path.length())))) {  
  58.                     path = path + "/";  
  59.                 }  
  60.             } else if(FILE_SEPARATOR.equals("\\")) {  
  61.                 // 在Windows环境下判断是否以"\\"结尾   
  62.                 if (!("\\".equalsIgnoreCase(path.substring(path.length()-1, path.length())))) {  
  63.                     path = path + "\\";  
  64.                 }  
  65.             }  
  66.         } catch (FileNotFoundException e1) {  
  67.             e1.printStackTrace();  
  68.         } catch (IOException e1) {  
  69.             e1.printStackTrace();  
  70.         }  
  71.         InitServlet.ftpPath = path;  
  72.           
  73.         // 创建定时器   
  74.         timer = new Timer();  
  75.         // 启动定时器任务   
  76.         Date date = new Date();  
  77.         // 设定每天凌晨2点开始   
  78.         date.setHours(CommonStateDefine.HOUR);  
  79.         timer.schedule(new DeleteRetransFolderTicker(), new Date(date.getTime()  
  80.                 + 1 * 24 * 60 * 60 * 1000), 1 * 24 * 60 * 60 * 1000);  
  81.     }  
  82.   
  83.     public static final String getContextPath() {  
  84.         return contextPath;  
  85.     }  
  86.   
  87.     public static final String getServerConfig() {  
  88.         return serverConfig;  
  89.     }  
  90.   
  91.     public static String getFtpPath() {  
  92.         return ftpPath;  
  93.     }  
  94. }  

相关内容