org.apache.catalina.session

Class PersistentManagerBase

public abstract class PersistentManagerBase extends ManagerBase implements Lifecycle, PropertyChangeListener

Extends the ManagerBase class to implement most of the functionality required by a Manager which supports any kind of persistence, even if onlyfor restarts.

IMPLEMENTATION NOTE: Correct behavior of session storing and reloading depends upon external calls to the start() and stop() methods of this class at the correct times.

Version: $Revision: 892872 $ $Date: 2009-12-21 17:38:29 +0100 (Mon, 21 Dec 2009) $

Author: Craig R. McClanahan Jean-Francois Arcand

Field Summary
protected LifecycleSupportlifecycle
The lifecycle event support for this component.
protected intmaxActiveSessions
The maximum number of active Sessions allowed, or -1 for no limit.
protected intmaxIdleBackup
How long a session must be idle before it should be backed up.
protected intmaxIdleSwap
The maximum time a session may be idle before it should be swapped to file just on general principle.
protected intminIdleSwap
Minimum time a session must be idle before it is swapped to disk.
protected longprocessingTime
Processing time during session expiration and passivation.
protected intrejectedSessions
Number of session creations that failed due to maxActiveSessions.
protected booleansaveOnRestart
Whether to save and reload sessions when the Manager unload and load methods are called.
protected booleanstarted
Has this component been started yet?
protected Storestore
Store object which will manage the Session store.
Method Summary
voidaddLifecycleListener(LifecycleListener listener)
Add a lifecycle event listener to this component.
voidclearStore()
Clear all sessions from the Store.
LifecycleListener[]findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle.
SessionfindSession(String id)
Return the active Session, associated with this Manager, with the specified session id (if any); otherwise return null.
StringgetInfo()
Return descriptive information about this Manager implementation and the corresponding version number, in the format <description>/<version>.
intgetMaxActiveSessions()
Return the maximum number of active Sessions allowed, or -1 for no limit.
intgetMaxIdleBackup()
Indicates how many seconds old a session can get, after its last use in a request, before it should be backed up to the store.
intgetMaxIdleSwap()
The time in seconds after which a session should be swapped out of memory to disk.
intgetMinIdleSwap()
The minimum time in seconds that a session must be idle before it can be swapped out of memory, or -1 if it can be swapped out at any time.
StringgetName()
Return the descriptive short name of this Manager implementation.
intgetRejectedSessions()
Number of session creations that failed due to maxActiveSessions.
booleangetSaveOnRestart()
Indicates whether sessions are saved when the Manager is shut down properly.
StoregetStore()
Return the Store object which manages persistent Session storage for this Manager.
booleanisLoaded(String id)
Return true, if the session id is loaded in memory otherwise false is returned
protected booleanisStarted()
Get the started status.
voidload()
Load all sessions found in the persistence mechanism, assuming they are marked as valid and have not passed their expiration limit.
voidprocessExpires()
Implements the Manager interface, direct call to processExpires and processPersistenceChecks
protected voidprocessMaxActiveSwaps()
Swap idle sessions out to Store if too many are active
protected voidprocessMaxIdleBackups()
Back up idle sessions.
protected voidprocessMaxIdleSwaps()
Swap idle sessions out to Store if they are idle too long.
voidprocessPersistenceChecks()
Called by the background thread after active sessions have been checked for expiration, to allow sessions to be swapped out, backed up, etc.
voidpropertyChange(PropertyChangeEvent event)
Process property change events from our associated Context.
voidremove(Session session)
Remove this Session from the active Sessions for this Manager, and from the Store.
voidremoveLifecycleListener(LifecycleListener listener)
Remove a lifecycle event listener from this component.
protected voidremoveSession(String id)
Remove this Session from the active Sessions for this Manager, and from the Store.
voidremoveSuper(Session session)
Remove this Session from the active Sessions for this Manager, but not from the Store.
voidsetContainer(Container container)
Set the Container with which this Manager has been associated.
voidsetMaxActiveSessions(int max)
Set the maximum number of actives Sessions allowed, or -1 for no limit.
voidsetMaxIdleBackup(int backup)
Sets the option to back sessions up to the Store after they are used in a request.
voidsetMaxIdleSwap(int max)
Sets the time in seconds after which a session should be swapped out of memory to disk.
voidsetMinIdleSwap(int min)
Sets the minimum time in seconds that a session must be idle before it can be swapped out of memory due to maxActiveSession.
voidsetRejectedSessions(int rejectedSessions)
voidsetSaveOnRestart(boolean saveOnRestart)
Set the option to save sessions to the Store when the Manager is shut down, then loaded when the Manager starts again.
protected voidsetStarted(boolean started)
Set the started flag
voidsetStore(Store store)
Set the Store object which will manage persistent Session storage for this Manager.
voidstart()
Prepare for the beginning of active use of the public methods of this component.
voidstop()
Gracefully terminate the active use of the public methods of this component.
protected SessionswapIn(String id)
Look for a session in the Store and, if found, restore it in the Manager's list of active sessions if appropriate.
protected voidswapOut(Session session)
Remove the session from the Manager's list of active sessions and write it out to the Store.
voidunload()
Save all currently active sessions in the appropriate persistence mechanism, if any.
protected voidwriteSession(Session session)
Write the provided session to the Store without modifying the copy in memory or triggering passivation events.

Field Detail

lifecycle

protected LifecycleSupport lifecycle
The lifecycle event support for this component.

maxActiveSessions

protected int maxActiveSessions
The maximum number of active Sessions allowed, or -1 for no limit.

maxIdleBackup

protected int maxIdleBackup
How long a session must be idle before it should be backed up. -1 means sessions won't be backed up.

maxIdleSwap

protected int maxIdleSwap
The maximum time a session may be idle before it should be swapped to file just on general principle. Setting this to -1 means sessions should not be forced out.

minIdleSwap

protected int minIdleSwap
Minimum time a session must be idle before it is swapped to disk. This overrides maxActiveSessions, to prevent thrashing if there are lots of active sessions. Setting to -1 means it's ignored.

processingTime

protected long processingTime
Processing time during session expiration and passivation.

rejectedSessions

protected int rejectedSessions
Number of session creations that failed due to maxActiveSessions.

saveOnRestart

protected boolean saveOnRestart
Whether to save and reload sessions when the Manager unload and load methods are called.

started

protected boolean started
Has this component been started yet?

store

protected Store store
Store object which will manage the Session store.

Method Detail

addLifecycleListener

public void addLifecycleListener(LifecycleListener listener)
Add a lifecycle event listener to this component.

Parameters: listener The listener to add

clearStore

public void clearStore()
Clear all sessions from the Store.

findLifecycleListeners

public LifecycleListener[] findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle. If this Lifecycle has no listeners registered, a zero-length array is returned.

findSession

public Session findSession(String id)
Return the active Session, associated with this Manager, with the specified session id (if any); otherwise return null. This method checks the persistence store if persistence is enabled, otherwise just uses the functionality from ManagerBase.

Parameters: id The session id for the session to be returned

Throws: IllegalStateException if a new session cannot be instantiated for any reason IOException if an input/output error occurs while processing this request

getInfo

public String getInfo()
Return descriptive information about this Manager implementation and the corresponding version number, in the format <description>/<version>.

getMaxActiveSessions

public int getMaxActiveSessions()
Return the maximum number of active Sessions allowed, or -1 for no limit.

getMaxIdleBackup

public int getMaxIdleBackup()
Indicates how many seconds old a session can get, after its last use in a request, before it should be backed up to the store. -1 means sessions are not backed up.

getMaxIdleSwap

public int getMaxIdleSwap()
The time in seconds after which a session should be swapped out of memory to disk.

getMinIdleSwap

public int getMinIdleSwap()
The minimum time in seconds that a session must be idle before it can be swapped out of memory, or -1 if it can be swapped out at any time.

getName

public String getName()
Return the descriptive short name of this Manager implementation.

getRejectedSessions

public int getRejectedSessions()
Number of session creations that failed due to maxActiveSessions.

Returns: The count

getSaveOnRestart

public boolean getSaveOnRestart()
Indicates whether sessions are saved when the Manager is shut down properly. This requires the unload() method to be called.

getStore

public Store getStore()
Return the Store object which manages persistent Session storage for this Manager.

isLoaded

public boolean isLoaded(String id)
Return true, if the session id is loaded in memory otherwise false is returned

Parameters: id The session id for the session to be searched for

isStarted

protected boolean isStarted()
Get the started status.

load

public void load()
Load all sessions found in the persistence mechanism, assuming they are marked as valid and have not passed their expiration limit. If persistence is not supported, this method returns without doing anything.

Note that by default, this method is not called by the MiddleManager class. In order to use it, a subclass must specifically call it, for example in the start() and/or processPersistenceChecks() methods.

processExpires

public void processExpires()
Implements the Manager interface, direct call to processExpires and processPersistenceChecks

processMaxActiveSwaps

protected void processMaxActiveSwaps()
Swap idle sessions out to Store if too many are active

processMaxIdleBackups

protected void processMaxIdleBackups()
Back up idle sessions.

processMaxIdleSwaps

protected void processMaxIdleSwaps()
Swap idle sessions out to Store if they are idle too long.

processPersistenceChecks

public void processPersistenceChecks()
Called by the background thread after active sessions have been checked for expiration, to allow sessions to be swapped out, backed up, etc.

propertyChange

public void propertyChange(PropertyChangeEvent event)
Process property change events from our associated Context.

Parameters: event The property change event that has occurred

remove

public void remove(Session session)
Remove this Session from the active Sessions for this Manager, and from the Store.

Parameters: session Session to be removed

removeLifecycleListener

public void removeLifecycleListener(LifecycleListener listener)
Remove a lifecycle event listener from this component.

Parameters: listener The listener to remove

removeSession

protected void removeSession(String id)
Remove this Session from the active Sessions for this Manager, and from the Store.

Parameters: id Session's id to be removed

removeSuper

public void removeSuper(Session session)
Remove this Session from the active Sessions for this Manager, but not from the Store. (Used by the PersistentValve)

Parameters: session Session to be removed

setContainer

public void setContainer(Container container)
Set the Container with which this Manager has been associated. If it is a Context (the usual case), listen for changes to the session timeout property.

Parameters: container The associated Container

setMaxActiveSessions

public void setMaxActiveSessions(int max)
Set the maximum number of actives Sessions allowed, or -1 for no limit.

Parameters: max The new maximum number of sessions

setMaxIdleBackup

public void setMaxIdleBackup(int backup)
Sets the option to back sessions up to the Store after they are used in a request. Sessions remain available in memory after being backed up, so they are not passivated as they are when swapped out. The value set indicates how old a session may get (since its last use) before it must be backed up: -1 means sessions are not backed up.

Note that this is not a hard limit: sessions are checked against this age limit periodically according to processExpiresFrequency. This value should be considered to indicate when a session is ripe for backing up.

So it is possible that a session may be idle for maxIdleBackup + processExpiresFrequency * engine.backgroundProcessorDelay seconds, plus the time it takes to handle other session expiration, swapping, etc. tasks.

Parameters: backup The number of seconds after their last accessed time when they should be written to the Store.

setMaxIdleSwap

public void setMaxIdleSwap(int max)
Sets the time in seconds after which a session should be swapped out of memory to disk.

setMinIdleSwap

public void setMinIdleSwap(int min)
Sets the minimum time in seconds that a session must be idle before it can be swapped out of memory due to maxActiveSession. Set it to -1 if it can be swapped out at any time.

setRejectedSessions

public void setRejectedSessions(int rejectedSessions)

setSaveOnRestart

public void setSaveOnRestart(boolean saveOnRestart)
Set the option to save sessions to the Store when the Manager is shut down, then loaded when the Manager starts again. If set to false, any sessions found in the Store may still be picked up when the Manager is started again.

Parameters: saveOnRestart true if sessions should be saved on restart, false if they should be ignored.

setStarted

protected void setStarted(boolean started)
Set the started flag

setStore

public void setStore(Store store)
Set the Store object which will manage persistent Session storage for this Manager.

Parameters: store the associated Store

start

public void start()
Prepare for the beginning of active use of the public methods of this component. This method should be called after configure(), and before any of the public methods of the component are utilized.

Throws: LifecycleException if this component detects a fatal error that prevents this component from being used

stop

public void stop()
Gracefully terminate the active use of the public methods of this component. This method should be the last one called on a given instance of this component.

Throws: LifecycleException if this component detects a fatal error that needs to be reported

swapIn

protected Session swapIn(String id)
Look for a session in the Store and, if found, restore it in the Manager's list of active sessions if appropriate. The session will be removed from the Store after swapping in, but will not be added to the active session list if it is invalid or past its expiration.

swapOut

protected void swapOut(Session session)
Remove the session from the Manager's list of active sessions and write it out to the Store. If the session is past its expiration or invalid, this method does nothing.

Parameters: session The Session to write out.

unload

public void unload()
Save all currently active sessions in the appropriate persistence mechanism, if any. If persistence is not supported, this method returns without doing anything.

Note that by default, this method is not called by the MiddleManager class. In order to use it, a subclass must specifically call it, for example in the stop() and/or processPersistenceChecks() methods.

writeSession

protected void writeSession(Session session)
Write the provided session to the Store without modifying the copy in memory or triggering passivation events. Does nothing if the session is invalid or past its expiration.
Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.