您的位置:首页 > 其它

服务重启后,session销毁的时候,在线统计人数出现负数

2009-05-28 21:51 218 查看
服务重启后,session销毁的时候,在线统计人数出现负数
public void sessionDestroyed(HttpSessionEvent event) {
  System.out.println("session destroyed");
  long createTime = event.getSession().getCreationTime();
  long serverStartupTime = ServiceStartupListener.getStartupTime();
  /*
   * this case is to handle negative online counter.
   * if the session created before the server restart,
   * and the client haven't close his or her browser,
   * also, at that moment, the session is not invalidate or time out
   * so, in this case, the session will not create again, but the counter
   * is already reset to 0. As a result, when this session destroyed,
   * we can't subtract by 1.
     这种是为了处理在线统计出现负数的问题,如果这个session在服务器启动前创建
但是客户并没有关闭他的浏览器,同时,这个session依然未超时或失效,那么在这样的情况下,刷新浏览器,session不会重新创建,但是此时的计数器已经回执为0了。结果,当这个session销毁时,就不能执行减1的操作
   */
  if(createTime < serverStartupTime) return;
 
  //when a session destroyed, a client leave from the website
  synchronized (lock) {
   OnLineCounter.setAllClientCount( OnLineCounter.getAllClientCount()-1 );
  }
 }

如前面蓝色字体描述的,我遇到这样的问题,虽然这个问题有点极端,但是实际运行中肯定能遇上,一旦遇上,那数据不一致造成的影响可大可小!问题是这样的:设定每个session的会话时间为2分钟(测试需要,随便多少都行,但是手上的操作要来得及啊),开启多个ie7,即启动了多个session,这个时候,在这几个session销毁之前重启服务器,但是ie7不关闭!服务启动完毕后,重新刷新那几个浏览器,发现session并未重新创建(如果重新创建,那么监听程序会在控制台上打印相应的信息).会话时间过后,控制台上打印出相应的session销毁的信息。于是负数就出现了..
Just like the blue words described, a problem I encounter. It's a little extremity, but we really encounter in fact.
Once you encounterd, the data maybe not right!Problem is like this: set the interval of the session with 2 and
open several ie7 browsers, that means several sessions are opened. At this moment, we restart the server
before the session destroyed,but do not close the browsers! when the server started again, refresh those
browsers, you can find that no new session is created. but when session's time out, the console will print the
info about the destroy of cressponding session. And then, negative number come out!

为了解决了这个问题,本人冥思苦想,情况特殊,网上搜索一阵也没有相关的文章。但是既然是技术问题,那就一定能用技术来解决!于是吃晚饭时我想到了解决办法-->目标就是要在那样的情况下session销毁的时候不做减法。要达到这个目标,我需要知道这个session是在服务启动之前创建的。如何知道?当方法被我想到的时候,就显得简单了,但是在我想来想去找不到方法时,确实很难,因为这种要求有点奇怪:服务启动前创建的session。。。
to solve this problem, I thought and thought, cause the special case, I can't find relative passage on the website.
but it's a technology problem, It can be solve by technology!!! So I found my method to solve that problem-->
the purpose is not to do subtract at the online number when the session destroyed. In order to reach this point,
I need to know whether the session created before the server started or not! but how can I kown it??? When I
found my answer, it seems simple. But when I can't find any methods at that time, It made me feel difficult,
becasue it's very strange to know the before-server-started-session。。。

解决方案:为了知道session是在服务启动前或后创建的,设计一个ServletContextListener-->ServiceStartupListener:覆盖一个方法即可:
public void contextInitialized(ServletContextEvent event) {
     startupTime = System.currentTimeMillis();
 }
然后写入配置文件。这样,通过文章开头那段代码就能判断session是在服务启动前创建的还是在服务启动后创建的。当然,这不能保证百分百精准无误,毕竟cpu运行代码是要消耗时间的,所以一两秒的出入还是存在可能的!不过通过这样的方法,成功解决了我遇到的问题!
the resolvent: to know whether the session created before the server started or not, I designed a Listener:-->
ServiceStartupListener:                  ---cover this method:
public void contextInitialized(ServletContextEvent event) {
     startupTime = System.currentTimeMillis();
 }
then register it to the web.xml。Then, we will know the session is created before server started or not.
Indeed, this can't ensure the 100 percent correct, but it is real need time to run codes in CPU, so one
or two second errors maybe exist!But by this method, I solved my problem successfully!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息