原始出发点:
我们开发的系统中包含多个子系统,而我开发的查询设计器是个独立子系统,在单独操作查询设计器时,因没有与控制中心交互,当单独操作查询设计器的时间超过控制中心设置的超时时间时就可能会引发控制中心的超时,但实际上用户一直在操作。为了解决这个问题,就需要在用户操作查询设计器时,同时给控制中心发送一个信息,使在操作查询设计器时控制中心不会超时。
查询设计器首页是index.html,控制中心打开查询设计器时url中包含了mySessionId参数,而当查询设计器再进行其他的操作时,就不再发送mySessionId参数。
为了解决这个问题,需要使用struts的过滤器和拦截器。首先我定义了一个拦截器,期望得到mySessionId参数,但实际不能如我所愿,拦截器只拦截了*.action,对首页的index.html参数无能为力。查看struts框架:
发现拦截器比较靠下,可能会有很多东西到不了拦截器。而过滤器基本是请求的第一层,那就写一个过滤器,结果如我所愿。
这样我就可以实现在过滤器中得到mySessionId参数,并保存到session中。然后在拦截器,每次用户操作都向控制中心发送一个请求,这样就实现了只要用户操作查询设计器,控制中心就不会超时的要求。
自定义过滤器代码:
/**
* 功能说明:用于子系统跨域托收SessionId参数
* 日期:2012-12-14
* Author: 常绍新
*/
public class HummerStrutsFilter extends StrutsPrepareAndExecuteFilter {
@Override
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String eagle2SessionId = request.getParameter("mySessionId");
if(eagle2SessionId!=null){
request.getSession().setAttribute("mySessionId ", eagle2SessionId);
}
else if(request.getSession().getAttribute("mySessionId ")==null){
//return;
}
super.doFilter(req, res, chain);
}
}
自定义拦截器代码:
public class HummerAsnyNoticeInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 5543269481004367532L;
@Resource
AsnyHttpRequest asnyHttpRequest;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
invocation.invoke();
// 获取mySessionId
ActionContext ac = invocation.getInvocationContext();
String mySessionId = (String) ac.getSession().get("mySessionId ");
// 向数据中心发送异步请求
asnyHttpRequest.sendNotice(eagle2SessionId);
return null;
}
}
向数据中心发送异步请求的代码就不贴了^_^。
还需要修改配置:
Struts.xml中:<interceptors>节点中增加:
<interceptor name="HummerAsnyNoticeInterceptor" class="com.dfsoft.hummer.domain.common.HummerAsnyNoticeInterceptor" />
<interceptors><interceptor-stack>节点下增加:
<interceptor-ref name="HummerAsnyNoticeInterceptor" />
Web.xml文件中增加:
<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.dfsoft.hummer.domain.common.HummerStrutsFilter</filter-class>
</filter>
到这里已经实现了功能要求。
备选方案1:
在首页index.html页面中获取mySessionId参数,再向一个action中发送该参数,该action中保存mySessionId到session中,然后如上实现拦截器,在每次请求时向控制中心发送信息。
备选方案2:
改写查询设计器首页为action,接收关保存mySessionId参数,然后还使用上面拦截器在每次请求时向控制中心发送信息。
备选方案3:
在首页index.html页面中获取mySessionId参数,使用js实现一个定时器,定时向控制中心发送请求。这时只要打开查询设计器,控制中心就不会超时^_^。