001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: StandardSessionIdleTimer.java,v 1.2 2006-06-15 13:44:07 sinisa Exp $
022: */
023:
024: package com.lutris.appserver.server.sessionEnhydra;
025:
026: import com.lutris.appserver.server.Application;
027: import com.lutris.appserver.server.Enhydra;
028: import com.lutris.logging.LogChannel;
029: import com.lutris.logging.Logger;
030:
031: /**
032: * The idle timer thread for <CODE>StandardSessionManager</CODE>.
033: * This thread sleeps in the background, waking up periodically to
034: * check for inactive sessions and to terminate any session that has been
035: * inactive for too long.
036: *
037: * @version $Revision: 1.2 $
038: * @author John Marco
039: * @author Shawn McMurdo
040: */
041: public class StandardSessionIdleTimer extends Thread {
042:
043: private StandardSessionIdleHandler idleHandler;
044: private long scanInterval; // In milliseconds.
045: private Application app; // The application context.
046:
047: /*
048: * Constructor an idle timer object. Sets the
049: * internal session manager reference and check period,
050: * but doesn't start the thread.
051: *
052: * @param manager The session manager to be checked periodically for
053: * idle sessions.
054: * @param app the application context.
055: * @param scanIntervalSec Time interval, in seconds, to scan for idle
056: * session.
057: */
058: public StandardSessionIdleTimer(StandardSessionIdleHandler manager,
059: Application app, long scanIntervalSec) {
060: idleHandler = manager;
061: this .app = app;
062: // Convert idle timer scanning interval milliseconds.
063: scanInterval = scanIntervalSec * 1000;
064: setPriority(MIN_PRIORITY); // For performance
065: setDaemon(true); // Don't require this thread to exit.
066: }
067:
068: /**
069: * The main code body of the Idle Timer Thread. Enters an endless
070: * loop that sleeps for a configurable period, periodically waking
071: * up to check the Session Manager for idle sessions.
072: */
073: public void run() {
074: if (app != null) {
075: Enhydra.register(app);
076: }
077: while (true) {
078: try {
079: sleep(scanInterval);
080: } catch (InterruptedException e) {
081: // Ignore.
082: }
083: try {
084: idleHandler.cleanUpIdleSessions();
085: } catch (Exception e) {
086: LogChannel log = Enhydra.getLogChannel();
087: log.write(Logger.ALERT,
088: "Failed to clean up idle sessions: " + e);
089: // TODO - print to log
090: e.printStackTrace();
091: }
092: }
093: // TODO - Enhydra.unregister();
094: }
095:
096: /**
097: * Shutdown the thread associated with this object.
098: */
099: public void shutdown() {
100: stop();
101: }
102: }
|