java—对于绑定到会话的对象,是否允许applicationserver在会话失效期间两次调用“valueunbound(…)”方法?

e5njpo68  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(156)

我有一个在WebSphere8.0、8.5和Tomcat8.5上运行良好的web应用程序(Servlet2.4规范)。现在我们要评估appcliation是否可以在websphereliberty下运行。
websphere日志文件在启动期间提供以下版本号:

product = WebSphere Application Server 17.0.0.2 (wlp-1.0.17.cl170220170523-1818)

在应用程序启动期间,通过在浏览器中发送启动url,某些对象将绑定到会话。例如,通过以下代码:

(...)
GwgImpl gwg = (GwgImpl)session.getAttribute(WEBKEY);
if (gwg == null) {
  gwg = new GwgImpl();
  session.setAttribute(WEBKEY, gwg);
}
(...)

这个 GwgImpl 对象定义如下:

public class GwgImpl implements HttpSessionBindingListener {
  List<String> list = new ArrayList<String>();
  (...)
  public void valueBound(HttpSessionBindingEvent ev) {
  }

  public void valueUnbound(HttpSessionBindingEvent ev) {
    list.clear();
    list = null;
    (...)
  }
  (...)
}

直到现在,一切似乎都很顺利。但是当用户从应用程序和我的 LogoutHandler 通过以下操作使会话无效

(...)
HttpSession session = request.getSession(false);
if (session != null) {
  session.invalidate();
}
(...)

方法 valueUnbound() 在对象中 GwgImpl 叫了两次。因为方法的第一个调用设置变量 listnull ,第二个调用将导致 NullPointerException .
我知道我可以通过添加if语句轻松解决此问题,但由于没有其他应用程序服务器存在此问题,因此我有以下问题:
是否允许应用程序服务器调用 valueUnbound(..) 方法在会话期间两次对绑定到会话的对象无效(我还没有从servlet规范中找到明确的答案)
或者这是WebSphereApplicationServer中的一个bug?

编辑

我修改了我的 valueUnbound(..) 方法查看两个调用的调用堆栈和 HttpSessionBindingEvent .

public void valueUnbound(HttpSessionBindingEvent ev) {
  logger.info("Stacktrace:");
  for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
    logger.info("  " + stackTraceElement.toString());
  }
  logger.info("HttpSessionBindingEvent:");
  logger.info("  SessionID='" + ev.getSession().getId() + "'");
  logger.info("  Name='" + ev.getName() + "'");
  logger.info("  Value='" + ev.getValue() + "'");
  (...)
}

在我的websphere日志文件中,我现在找到以下行:

Stacktrace:
  java.lang.Thread.getStackTrace(Unknown Source)
  com.mycorp.gwg.dao.GwgImpl.valueUnbound(GwgImpl.java:130)
  com.ibm.ws.session.http.HttpSessionObserver.sessionDestroyed(HttpSessionObserver.java:231)
  com.ibm.ws.session.SessionEventDispatcher.sessionDestroyed(SessionEventDispatcher.java:167)
  com.ibm.ws.session.StoreCallback.sessionInvalidated(StoreCallback.java:129)
  com.ibm.ws.session.store.memory.MemorySession.invalidate(MemorySession.java:236)
  com.ibm.ws.session.http.HttpSessionImpl.invalidate(HttpSessionImpl.java:317)
  com.ibm.ws.session.SessionData.invalidate(SessionData.java:256)
  com.ibm.ws.session.HttpSessionFacade.invalidate(HttpSessionFacade.java:197)
  com.mycorp.login.client.LogoutHandler.doLogout(LogoutHandler.java:154)
  (...)
HttpSessionBindingEvent:
  SessionID='X_AmenZHphA-DBkNRoOeWZu'
  Name='gwg'
  Value='null'
(...)
Stacktrace:
  java.lang.Thread.getStackTrace(Unknown Source)
  com.mycorp.gwg.dao.GwgImpl.valueUnbound(GwgImpl.java:130)
  com.ibm.ws.session.http.HttpSessionAttributeObserver.sessionAttributeRemoved(HttpSessionAttributeObserver.java:160)
  com.ibm.ws.session.SessionStateEventDispatcher.sessionAttributeRemoved(SessionStateEventDispatcher.java:142)
  com.ibm.ws.session.StoreCallback.sessionAttributeRemoved(StoreCallback.java:196)
  com.ibm.ws.session.store.memory.MemorySession.removeAttribute(MemorySession.java:537)
  com.ibm.ws.session.store.memory.MemorySession.invalidate(MemorySession.java:253)
  com.ibm.ws.session.http.HttpSessionImpl.invalidate(HttpSessionImpl.java:317)
  com.ibm.ws.session.SessionData.invalidate(SessionData.java:256)
  com.ibm.ws.session.HttpSessionFacade.invalidate(HttpSessionFacade.java:197)
  com.mycorp.login.client.LogoutHandler.doLogout(LogoutHandler.java:154)
  (...)
HttpSessionBindingEvent:
  SessionID='X_AmenZHphA-DBkNRoOeWZu'
  Name='gwg'
  Value='com.mycorp.gwg.dao.GwgImpl@b294b7c'

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题