返回

J2EE,struts2 拦截器中获取 server

发布时间:2023-08-30 17:51:15 280

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配置内容
//
//
// com.alpha.util.TimerListener
//

 

 

 

/**
* 拦截器方法
*/
@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;
}

 

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
Js 获取数组最大和最小值 2023-08-30 15:02:28