| Code highlighting produced by Actipro CodeHighlighter (freeware) |
| http://www.CodeHighlighter.com/ |
|
| -->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()); |
| } |
|
| } |