I am attempting to use non-sticky sessions using a syncronous Persistent
Manager and a JDBC Store.  
 
I have written a class SyncPersistentManager that extends
PersistentManagerBase and implements HttpSessionAttributeListener to
store the sessions in the JDBC store each time an attribute in the
HttpSession is modified.
 
Is there a simple extension point that I can use to get Tomcat to read
the session from the store if HttpSession.getAttribute is called.  I was
hoping to be able to implement this functionality via some extension
point rather than having to modify the StandardSession object as the
Session object does not appear to be pluggable (unless I'm missing
something).
 
Anyone have any ideas?
 
Thanks,
 
Gary Blomquist
 
 
Source for SyncPersistentManager
 
 
public class SyncPersistentManager extends PersistentManagerBase
implements HttpSessionAttributeListener {
 
 boolean registered = false;
 
 private static final String info = "SyncPersistentManager/1.0";
 
 protected static String name = "SyncPersistentManager";
 
 public SyncPersistentManager() {
  super();
 }
 
 public String getInfo() {
  return info;
 }
 
 public String getName() {
  return name;
 }
     
 public void attributeAdded(HttpSessionBindingEvent event) {
  persistSession(event);
 }
 
 public void attributeRemoved(HttpSessionBindingEvent event) {
  persistSession(event);
 }
 
 public void attributeReplaced(HttpSessionBindingEvent event) {
  persistSession(event);
 }
 
 private void persistSession(HttpSessionBindingEvent event) {
  HttpSession httpSession = event.getSession();
  try {
     StandardSession standardSession =
(StandardSession)findSession(httpSession.getId());
     this.writeSession(standardSession);
  } catch (IOException e) {
   System.out.println("SyncPersistentManager::error persisting session "
+ e.getMessage());
  } catch (Exception e) {
   System.out.println("SyncPersistentManager::error persisting session "
+ e.getMessage());
  }
 }
 
 protected synchronized void writeSession(Session session) throws
IOException {
  if ( ! registered ) {
   register();
  }
    super.writeSession(session);
 }
  
 private void register() {
  Context context = (Context)getContainer();
  Object listeners[] = context.getApplicationEventListeners();
  Object updatedListeners[] = new Object[listeners.length + 1];
  for (int i=0; i<(updatedListeners.length - 1); i++) {
   updatedListeners[i] = listeners[i];
  }
  updatedListeners[updatedListeners.length -1] = this;
  context.setApplicationEventListeners(updatedListeners);
  registered = true;
 }
 
}

Reply via email to