tomcat6 使用comet衍生出的两个额外问题

开发了一个轮询推送功能,网上也有很多文章讲这个就不说怎么做的了。现在发现两个问题:

一:就是登录进主页面后,由于浏览器在不停轮询,导致后端认为前端一直在操作,而正常设定的session超时就跳转到登录页面的功能就失效了。因为服务器认为这个session一直活跃着的。

本以为tomcat会有配置,特定的url不算入session的accessTime,找了很久没有这个功能。那就想有没有办法控制浏览器发送请求的时候不带上JSSESSIONID的cookie,也没办法实现。所以只有用自己写代码的形式来控制了。

总的思路有两种,一种是后端控制,另一种是前端控制。

  后端控制的思路就是用过滤器过滤所有请求,把轮询的请求排除,自己来计算session超时的时间,到了就调用session销毁的方法。特别说明一点映射到实现tomcat6的CometProcessor的servlet的url不会经过Filter,这样自动就排除了(一开始不晓得,我还去在struts.xml的配置里把这个url设为excludePattern,其实完全没必要,因为都不会经过struts2的过滤器)。

  前端控制的思路是用一些js框架监控前端的事件,如果长时间没有操作,就发送logout请求到服务器。

---------------------------------------------------------------------------------------------------------------------------

tomcat6一个特殊属性:

 

org.apache.catalina. STRICT_SERVLET_COMPLIANCE

If this is true the following actions will occur:

  • any wrapped request or response object passed to an application dispatcher will be checked to ensure that it has wrapped the original request or response. (SRV.8.2 / SRV.14.2.5.1)
  • a call to Response.getWriter() if no character encoding has been specified will result in subsequent calls to Response.getCharacterEncoding() returning ISO-8859-1 and the Content-Type response header will include a charset=ISO-8859-1 component. (SRV.15.2.22.1)
  • every request that is associated with a session will cause the session's last accessed time to be updated regardless of whether or not the request explicitly accesses the session. (SRV.7.6)
  • cookies will be parsed strictly, by default v0 cookies will not work with any invalid characters. 
    If set to false, any v0 cookie with invalid character will be switched to a v1 cookie and the value will be quoted.
  • the path in ServletContext.getResource / getResourceAsStream calls must start with a "/".
    If set to false, code like getResource("myfolder/myresource.txt") will work.

 

If this is true the default value will be changed for:

  • org.apache.catalina.connector.Request. ALLOW_EMPTY_QUERY_STRING property
  • org.apache.tomcat.util.http.ServerCookie. PRESERVE_COOKIE_HEADER property
  • The webXmlValidation attribute of any Context element.
  • The webXmlNamespaceAware attribute of any Context element.
  • The tldValidation attribute of any Context element.

 

If not specified, the default value of false will be used.

这个属性默认值是false。

也就是说我们不设这个值的话,tomcat6去更新session的最后使用时间会在getsession方法被调用的时候才去执行。也就是说如果我们代码里不显示的去获取session的话,即使带着jssessionId这个cookie的请求进来,tomcat也不会去更新session访问时间。所以就很好办了,只要我们轮询url访问的servlet里不显示调用session就OK啦。

反编译tomcat源码出来看了下,如果在tomcat\conf目录下的catalina.properties里把org.apache.catalina. STRICT_SERVLET_COMPLIANCE为true了。那么容器在org.apache.catalina.core.StandarHostValue的invoke方法会执行request.getSession(false);这样时间就被更新了。

 

 

二:CometProcessor的线程局部变量问题。

  发现一个请求的生命周期里 CometEvent是BEGIN和ERROR的时候CurrentThread并不是同一个(其他两种事件类型的情况没试),而Request是同一个(不是同一个岂不乱套了)。推测tomcat在使用NIO的时候不同的CometEvent并没有一定要同一个Thread来执行,而是从线程池里取一个来执行,当然上下文(主要是Request,Respose)是要保持着的,每次执行的时候要传进来。这也是能理解的,毕竟NIO就是为了大并发处理嘛,执行Begin的那个线程把Begin的逻辑执行完了就该放入线程池里供其他请求使用,如果一定要Error的时候再用这个线程来执行,那这段时间这个线程就不能复用了,也就失去了NIO的意义。

  项目里用了一个ThreadLoacl,都在一个全局的Filter里进行设置。开发完轮询功能正常,后来发现轮询的请求根本没进入那个设置ThreadLoacl的Filter,(前面说了CometProcessor的servlet不会经过普通Filter),那怎么还在正常使用呢,ThreadLoacl还有值。原因是其他请求在线程上设置了这个值,线程完了进入线程池并没有执行清除的操作,所以现在从线程池取出来ThreadLoacl也是有值的。(线程对象就是ThreadLoaclMap的一个Key而已)。所以为了正确,CometProcessor里也要进行ThreadLocal的初始化。

 

posted @ 2016-02-23 15:39  寂静沙滩  阅读(749)  评论(0编辑  收藏  举报