xml配置,监听器一定要在ContextLoaderListener后面
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 定时器 -->
<listener>
<listener-class>com.alpha.util.TimerListener</listener-class>
</listener>
推荐方法:
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* 安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行
* @author JavaAlpha
* @date 2013-10-30 13:46:15
*/
public class TimerListener implements ServletContextListener{
private Timer timer = null;
public void contextInitialized(ServletContextEvent servletContextEvent)
{
//在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能
timer = new Timer(true);
//添加日志,可在tomcat日志中查看到
servletContextEvent.getServletContext().log("定时发布Timer已启动!");
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContextEvent.getServletContext());
// ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
final EquipmentMaintainService emSvc = (EquipmentMaintainService) ac1.getBean("emService");//Service
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
//设备维护管理--定时提醒
EquipmentMaintainAction em = new EquipmentMaintainAction();
System.out.println("-------定期检修提醒任务--------");
em.timerTaskTbFTServiceRemind(emSvc);
System.out.println("-------定期注油提醒任务--------");
em.timerTaskTbFTOilingRemind(emSvc);
System.out.println("-------维修工单提醒任务--------");
em.timerTaskTbFTMaintainlistRemind(emSvc);
System.out.println("-------报警提醒任务--------");
em.timerTaskTbAlarmRemind(emSvc);
}
}, 30000, 2000);// 这里设定将延时每十分钟固定执行
//}, 1000 * 60 * 60 * 1, 1000 * 60 * 60 * 1);// 这里设定将延时每小时固定执行
//}, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
//添加日志,可在tomcat日志中查看到
servletContextEvent.getServletContext().log("已经添加定时发布任务!");
}
public void contextDestroyed(ServletContextEvent event)
{
// 在这里关闭监听器,所以在这里销毁定时器。
timer.cancel();
event.getServletContext().log("定时发布定时器销毁!");
}
}
// web.xml配置内容
//<!-- 定时器 -->
//<listener>
// <listener-class>com.alpha.util.TimerListener</listener-class>
//</listener>
/**
* 拦截器方法
*/
@SuppressWarnings({ "unchecked", "static-access" })
public String intercept(ActionInvocation invocation) {
String result = null;
HttpServletRequest request = ServletActionContext.getRequest();
BaseAction action = (BaseAction) invocation.getAction();
ServletContext sc = ServletActionContext.getServletContext();
//ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
ModuleService moduleService = (ModuleService) ctx.getBean("moduleService");//模块service
OperationLogService logService = (OperationLogService) ctx.getBean("operationService");//模块service
try {
result = invocation.invoke();
// 判断是否需要写日志
if (applyMethod(excludeMethods, includeMethods, invocation.getProxy().getMethod())) {
this.saveLog(getModule(request, moduleService),AlphaUtil.getIP(request), action.getEmployee().getEId(), action.getEmployee().getEName(), logService);
}
} catch (Exception e) {
e.printStackTrace();
return action.SUCCESS;
}
return result;
}