Java – ICEFaces Session Auto-Extension

icefacesjavajava-ee-5jsftomcat

I have an application in which users frequently leave the page running in their browser all day. The page has an intervalrendered that updates data every 30 seconds.

However, if the user doesn't interact with the page for a while, it still expires. I'd like it to auto-extend for as long as the browser is open and making the scheduled requests.

Is there a way to automatically extend the session everytime that the scheduled renderer is fired for one of these pages?

Best Answer

I don't really want to do Javascript hacks to click buttons when my code is already being called every 30 seconds by ICEFaces. The following hacked workaround for ICEFaces internal timer seems to work:

  private void updateSessionExpiration () {
      HttpSession sess = getSession();
      if (sess != null) {
        try {
            Field fld = SessionDispatcher.class.getDeclaredField("SessionMonitors");
            fld.setAccessible(true);
            Map map = (Map)fld.get(null);
            String sessID = sess.getId();
            if (map.containsKey(sessID)) {
                log.info("About to touch session...");
                SessionDispatcher.Monitor mon = 
                     (SessionDispatcher.Monitor)map.get(sessID);
                mon.touchSession();
            }
        } catch (Exception e) {
            log.error("Failed to touch session");
        }
      }
  }

Also, with ICEFaces 1.8.2RC1 (and presumably eventually with the release version of ICEFaces 1.8.2 as well), there are two new workarounds available:

HttpServletRequest request = (HttpServletRequest) servletRequest; 
HttpSession session = request.getSession(); 
if (session != null) { 
    SessionDispatcher.Monitor.lookupSessionMonitor(session).touchSession(); 
} 

Or, put this in the web.xml to update on any hit to URLs within a specific pattern:

<filter> 
    <filter-name>Touch Session</filter-name> 
    <filter-class>com.icesoft.faces.webapp.http.servlet.TouchSessionFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>Touch Session</filter-name> 
    <url-pattern>/seam/remoting/*</url-pattern> 
</filter-mapping> 
Related Topic