Skip to Main Content

Destroy session beans from application scoped bean

1031939Aug 9 2013 — edited Aug 19 2013

Hello everybody!

I have a problem that I don't manage to solve.

I'm developping a web application with JSF under Eclipse / Tomcat.

This application contains an application scoped bean that manages all initialisation stuff and several session beans that manage parts of the application.

I realized that when the application was stopped in the tomcat manager, several threads launched in the application scoped bean were still alive, so I coded a @PreDestroy method in order to stop and free all existing threads.

It works fine.

However, the session beans are not correctly destroyed, as they still are writing traces after the application stops.

I tried then to destroy manually the opened sessions in my @PreDestroy method, using the following code :




FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

Or :


for (HttpSession s : SessionListener.getCurrentSessions()) {

  if (s != null) {

  s.invalidate();

  }

  }


using the following sessionListener class :


public class SessionListener implements HttpSessionListener, Serializable {

  private static final long serialVersionUID = -7860781560150046445L;

  private static List<HttpSession> currentSessions = new ArrayList<HttpSession>();

  public SessionListener() {

  }

  /**

  * @return the currentSessions

  */

  public static List<HttpSession> getCurrentSessions() {

  return currentSessions;

  }

  @Override

  public void sessionCreated(HttpSessionEvent arg0) {

  currentSessions.add(arg0.getSession());

  }

  @Override

  public void sessionDestroyed(HttpSessionEvent arg0) {

  currentSessions.remove(arg0.getSession());

  }

}

but both of these methods throw the following exception:

java.util.logging.ErrorManager: 5

java.lang.NullPointerException

  at java.util.PropertyResourceBundle.handleGetObject(PropertyResourceBundle.java:155)

  at java.util.ResourceBundle.getObject(ResourceBundle.java:387)

  at java.util.ResourceBundle.getString(ResourceBundle.java:353)

  at java.util.logging.Formatter.formatMessage(Formatter.java:118)

  at java.util.logging.SimpleFormatter.format(SimpleFormatter.java:151)

  at java.util.logging.StreamHandler.publish(StreamHandler.java:196)

  at java.util.logging.ConsoleHandler.publish(ConsoleHandler.java:105)

  at java.util.logging.Logger.log(Logger.java:565)

  at java.util.logging.Logger.doLog(Logger.java:586)

  at java.util.logging.Logger.log(Logger.java:675)

  at com.sun.faces.mgbean.BeanBuilder.destroy(BeanBuilder.java:115)

  at com.sun.faces.mgbean.BeanManager.destroy(BeanManager.java:279)

  at com.sun.faces.application.WebappLifecycleListener.handleAttributeEvent(WebappLifecycleListener.java:307)

  at com.sun.faces.application.WebappLifecycleListener.contextDestroyed(WebappLifecycleListener.java:361)

  at com.sun.faces.config.ConfigureListener.contextDestroyed(ConfigureListener.java:303)

  at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4837)

  at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5483)

  at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)

  at org.apache.catalina.manager.ManagerServlet.stop(ManagerServlet.java:1306)

  at org.apache.catalina.manager.HTMLManagerServlet.stop(HTMLManagerServlet.java:733)

  at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:221)

  at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)

  at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)

  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

  at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:213)

  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

  at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)

  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)

  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)

  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581)

  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)

  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)

  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)

  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)

  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)

  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)

  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)

  at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)

  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

  at java.lang.Thread.run(Thread.java:722)

Does somebody have an idea of what am i doing wrong?
Is there a better way to correctly handle applicationscoped and sessionscoped beans destruction?
Thanks in advance

Comments
Post Details
Added on Aug 9 2013
1 comment
1,602 views