User: jules_gosnell Date: 02/01/31 16:32:24 Modified: jetty/src/main/org/jboss/jetty/session DistributedHttpSession.java DistributedHttpSessionManager.java Log: some way further to configurable snapshot strategies and intervals, Revision Changes Path 1.5 +37 -17 contrib/jetty/src/main/org/jboss/jetty/session/DistributedHttpSession.java Index: DistributedHttpSession.java =================================================================== RCS file: /cvsroot/jboss/contrib/jetty/src/main/org/jboss/jetty/session/DistributedHttpSession.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- DistributedHttpSession.java 2002/01/16 00:36:51 1.4 +++ DistributedHttpSession.java 2002/02/01 00:32:24 1.5 @@ -46,7 +46,6 @@ final boolean _heldByValue =true; // attributes are held in store by value, not reference final boolean _accessDirties =true; // updating _lastAccessedTime dirties session - final boolean _snapshotNeedsActivation =true; // they will not have been properly passivated final boolean _isDistributed =true; volatile boolean _isNew =true; // risky ! volatile boolean _isValid =true; // risky ! @@ -96,7 +95,7 @@ _attributes.putAll(data.getAttributes()); - boolean notify=data.getAttributesWerePassivated() || _snapshotNeedsActivation; + boolean notify=data.getAttributesWerePassivated() || _manager.getSnapshotStrategy().getNotifyActivate(); // send activate events to listening attributes if (notify && _attributes.size()>0) @@ -426,10 +425,12 @@ { if (attribute instanceof HttpSessionActivationListener) { - _log.warn("WARNING: About to activate a session attribute that was not passivated: "+attribute); - _log.warn("WARNING: This was probably due to an uncontrolled server shutdown."); - ((HttpSessionActivationListener)attribute) - .sessionDidActivate(event); + if (warn) + { + _log.warn("WARNING: About to activate a session attribute that was not passivated: "+attribute); + _log.warn("WARNING: This was probably due to an uncontrolled server shutdown."); + } + ((HttpSessionActivationListener)attribute) .sessionDidActivate(event); } } @@ -443,20 +444,20 @@ //---------------------------------------- - protected void - snapshot() + public void + snapshot(boolean notifyActivate, boolean notifyPassivate) { - store(false, false); + store(notifyActivate, notifyPassivate, false); } protected void passivate() { - store(true, true); + store(false, true, true); } protected synchronized void - store(boolean notifyAttributes, boolean shutdown) + store(boolean notifyActivate, boolean notifyPassivate, boolean shutdown) { // if there are no changes and this is not the final passivation // during a controlled shutdown of webapp or webcontainer, we @@ -469,10 +470,10 @@ { // whilst this method is synchronized, _attributes may still be // modified via their public access which only synchronise on - // _attributes itself. In order to cause as little contention for - // the _attributes map as possible I am going to try taking a copy - // here - otherwise we will have to hang on to a ock on it for - // ages. + // _attributes itself. In order to cause as little contention + // for the _attributes map as possible I am going to try taking + // a copy here - otherwise we will have to hang on to a lock on + // it for ages. // copy is a shallow, not deep, copy. If attributes within it // are being modified by another thread whilst being @@ -489,7 +490,8 @@ } } - if (notifyAttributes && copy!=null) + // about to passivate... + if (notifyPassivate && copy!=null) { // send passivate events to listening attributes... HttpSessionEvent event=new HttpSessionEvent(this); @@ -516,8 +518,26 @@ // provided I slow down the collection of the stored state, it // will never be collected before me.... data.setMaxInactiveInterval(getMaxInactiveInterval()+5); - data.setAttributesWerePassivated(notifyAttributes); + data.setAttributesWerePassivated(notifyPassivate); store.put(getId(), data); + + // about to activate... + if (notifyActivate && copy!=null) + { + // send activate events to listening attributes... + HttpSessionEvent event=new HttpSessionEvent(this); + Iterator i = copy.values().iterator(); + + try + { + while (i.hasNext()) + notifySessionDidActivate(i.next(), event, false); + } + catch (Exception e) + { + _log.error("Problem whilst activating session attributes", e); + } + } } } 1.8 +185 -8 contrib/jetty/src/main/org/jboss/jetty/session/DistributedHttpSessionManager.java Index: DistributedHttpSessionManager.java =================================================================== RCS file: /cvsroot/jboss/contrib/jetty/src/main/org/jboss/jetty/session/DistributedHttpSessionManager.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- DistributedHttpSessionManager.java 2002/01/22 21:03:04 1.7 +++ DistributedHttpSessionManager.java 2002/02/01 00:32:24 1.8 @@ -5,7 +5,7 @@ * See terms of license at gnu.org. */ -// $Id: DistributedHttpSessionManager.java,v 1.7 2002/01/22 21:03:04 jules_gosnell Exp $ +// $Id: DistributedHttpSessionManager.java,v 1.8 2002/02/01 00:32:24 jules_gosnell Exp $ // TODO @@ -41,6 +41,123 @@ //------------------------------------------------------------------------------ +// for a snapshot strategy that stores 'every' change, we really need +// a different HttpSession implementation - e.g. that IS an EJB... - +// this implies that the DistributedStore instance needs some input in +// the selection of the class to instantiate for an HttpSession. + +abstract class + SnapshotStrategy +{ + final boolean _notifyActivate; + final boolean _notifyPassivate; + + SnapshotStrategy() + { + _notifyActivate=false; + _notifyPassivate=false; + } + + SnapshotStrategy(boolean notifyActivate, boolean notifyPassivate) + { + _notifyActivate=notifyActivate; + _notifyPassivate=notifyPassivate; + } + + public void + snapshot(DistributedHttpSession session) + { + session.snapshot(_notifyActivate, _notifyPassivate); + } + + public boolean + getNotifyActivate() + { + return _notifyActivate; + } + + public boolean + getNotifyPassivate() + { + return _notifyPassivate; + } +} + +class DummySnapshotStrategy + extends SnapshotStrategy +{ + public void snapshot(DistributedHttpSession session) + {} +} + +class RequestSnapshotStrategy + extends SnapshotStrategy +{ + RequestSnapshotStrategy(boolean notifyActivate, boolean notifyPassivate) + { + super(notifyActivate, notifyPassivate); + } + + public void snapshot(DistributedHttpSession session) + {} +} + +class IntervalSnapshotStrategy + extends SnapshotStrategy +{ + class TimeOutNotifier + implements AbstractTimeOutManager.TimeOutNotifier + { + public void + timeOut(Object object) + { + DistributedHttpSession session=(DistributedHttpSession)object; + System.out.println("snapshotting session ("+session.getId()+")"); + snapshot(session); + // _manager.reregister(session, System.currentTimeMillis(), _interval*1000); + } + } + + class TimeOutTester + implements AbstractTimeOutManager.TimeOutTester + { + public long + timeRemaining(Object object, long now, long maxInactiveInterval) + { + DistributedHttpSession session=(DistributedHttpSession)object; + return session.getLastAccessedTime()+ maxInactiveInterval- now; + } + } + + // final AbstractTimeOutManager _manager; + final int _interval; + + IntervalSnapshotStrategy(/* AbstractTimeOutManager manager,*/ + int interval, boolean notifyActivate, boolean notifyPassivate) + { + super(notifyActivate, notifyPassivate); + // _manager=manager; + _interval=interval; + } + + public void snapshot(DistributedHttpSession session) + {} +} + +class IdleSnapshotStrategy + extends SnapshotStrategy +{ + IdleSnapshotStrategy(boolean notifyActivate, boolean notifyPassivate) + { + super(notifyActivate, notifyPassivate); + } + + public void snapshot(DistributedHttpSession session) + {} +} + +//------------------------------------------------------------------------------ + class MyTimeOutNotifier implements AbstractTimeOutManager.TimeOutNotifier { @@ -67,7 +184,7 @@ //------------------------------------------------------------------------------ /** * - * @version $Id: DistributedHttpSessionManager.java,v 1.7 2002/01/22 21:03:04 jules_gosnell Exp $ + * @version $Id: DistributedHttpSessionManager.java,v 1.8 2002/02/01 00:32:24 jules_gosnell Exp $ * @author [EMAIL PROTECTED] */ //---------------------------------------- @@ -79,6 +196,7 @@ static { _scavenger.start(); } final AbstractStore _store; + final SnapshotStrategy _snapshotStrategy; final Logger _log; final JBossWebApplicationContext _context; final ServletHandler _handler; @@ -90,6 +208,7 @@ volatile boolean _isStarted =false; // TODO volatile int _dftMaxIdleSecs =0; // <=0 means never timeout... + //---------------------------------------- static AbstractStore @@ -102,19 +221,71 @@ } catch (Exception e) { - Logger.getLogger("DistributedHttpSessionManager"). - error("problem initialising distributed store", e); + Logger.getLogger("DistributedHttpSessionManager").error("problem initialising distributed store", e); return null; } } public - DistributedHttpSessionManager(JBossWebApplicationContext context, String store) + DistributedHttpSessionManager(JBossWebApplicationContext context, + String storageStrategy, + String snapshotFrequency, + String snapshotNotificationPolicy) { _context=context; _handler=_context.getServletHandler(); _log =Logger.getLogger(getClass().getName()+"#" +_context.getContextPath()); - _store =instantiateStore(store); + _store =instantiateStore(storageStrategy); + + SnapshotStrategy tmp=null; + + boolean notifyActivation=false; + boolean notifyPassivation=false; + if (snapshotNotificationPolicy.equalsIgnoreCase("both")) + { + notifyActivation=true; + notifyPassivation=true; + } + else if (snapshotNotificationPolicy.equalsIgnoreCase("neither")) + { + notifyActivation=true; + notifyPassivation=true; + } + else if (snapshotNotificationPolicy.equalsIgnoreCase("activate")) + { + notifyActivation=true; + notifyPassivation=false; + } + else if (snapshotNotificationPolicy.equalsIgnoreCase("passivate")) + { + notifyActivation=false; + notifyPassivation=true; + } + else + { + _log.warn("bad HttpSessionSnapshotNotificationPolicy: "+snapshotNotificationPolicy+" - should be both/neither/passivate/activate"); + } + + if (snapshotFrequency.equalsIgnoreCase("never")) + tmp=new DummySnapshotStrategy(); + else if (snapshotFrequency.equalsIgnoreCase("idle")) + tmp=new IdleSnapshotStrategy(notifyActivation, notifyPassivation); + else if (snapshotFrequency.equalsIgnoreCase("request")) + tmp=new RequestSnapshotStrategy(notifyActivation, notifyPassivation); + else + { + try + { + tmp=new IntervalSnapshotStrategy(Integer.decode(snapshotFrequency).intValue(), + notifyActivation, notifyPassivation); + } + catch (NumberFormatException e) + { + _log.warn("bad HttpSessionSnapshotFrequency: "+snapshotFrequency+" - should be never/idle/request/<#minutes>"); + tmp=new DummySnapshotStrategy(); + } + } + _snapshotStrategy=tmp; } //---------------------------------------- @@ -176,7 +347,7 @@ if (session==null && _isDistributed && _store.isValid()) { AbstractHttpSessionData data=_store.get(id, !_maintainDistributedState); - if (data!=null) + if (data!=null) { session=new DistributedHttpSession(this, id, data); putSession(id,session); @@ -273,6 +444,12 @@ return _store; } + SnapshotStrategy + getSnapshotStrategy() // TODO + { + return _snapshotStrategy; + } + ServletHandler getHandler() // TODO { @@ -354,7 +531,7 @@ notifyAttributeRemoved(HttpSession session, String key, Object value) { int n=_sessionAttributeListeners.size(); - if (n>0) + if (n>0) { HttpSessionBindingEvent event = new HttpSessionBindingEvent(session, key, value);
_______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development