diff options
author | Matthew Newhook <matthew@zeroc.com> | 2008-08-26 15:32:09 -0230 |
---|---|---|
committer | Matthew Newhook <matthew@zeroc.com> | 2008-08-26 15:32:09 -0230 |
commit | d2d1ae0ca7d03f0b67f5c2086bff18f78331682d (patch) | |
tree | 966bdc697be9249585c02dd9dbbb27bfc1695c6b | |
parent | move the query objects to the library. (diff) | |
download | ice-d2d1ae0ca7d03f0b67f5c2086bff18f78331682d.tar.bz2 ice-d2d1ae0ca7d03f0b67f5c2086bff18f78331682d.tar.xz ice-d2d1ae0ca7d03f0b67f5c2086bff18f78331682d.zip |
Minor cleanup, added session timeout.
-rw-r--r-- | java/demo/Database/library/Parser.java | 33 | ||||
-rw-r--r-- | java/demo/Database/library/ReapThread.java | 9 | ||||
-rw-r--r-- | java/demo/Database/library/RunParser.java | 9 | ||||
-rw-r--r-- | java/demo/Database/library/Server.java | 6 | ||||
-rw-r--r-- | java/demo/Database/library/Session.ice | 10 | ||||
-rw-r--r-- | java/demo/Database/library/SessionFactoryI.java | 10 | ||||
-rw-r--r-- | java/demo/Database/library/SessionI.java | 10 | ||||
-rw-r--r-- | java/demo/Database/library/config.server | 6 |
8 files changed, 63 insertions, 30 deletions
diff --git a/java/demo/Database/library/Parser.java b/java/demo/Database/library/Parser.java index 33f15e80162..281e3762489 100644 --- a/java/demo/Database/library/Parser.java +++ b/java/demo/Database/library/Parser.java @@ -170,37 +170,30 @@ class Parser _current = next.get(0); } } + else + { + _current = null; + } printCurrent(); } void printCurrent() { - try + if(_current != null) { - if(_current != null) + System.out.println("current book is:"); + System.out.println("isbn: " + _current.isbn); + System.out.println("title: " + _current.title); + System.out.println("authors: " + _current.authors); + if(_current.rentedBy.length() > 0) { - System.out.println("current book is:"); - System.out.println("isbn: " + _current.isbn); - System.out.println("title: " + _current.title); - System.out.println("authors: " + _current.authors); - if(_current.rentedBy.length() > 0) - { - System.out.println("rented: " + _current.rentedBy); - } + System.out.println("rented: " + _current.rentedBy); } - else - { - System.out.println("no current book"); - } - } - catch(Ice.ObjectNotExistException ex) - { - System.out.println("current book no longer exists"); } - catch(Ice.LocalException ex) + else { - error(ex.toString()); + System.out.println("no current book"); } } diff --git a/java/demo/Database/library/ReapThread.java b/java/demo/Database/library/ReapThread.java index cf38b79a132..790abcd8350 100644 --- a/java/demo/Database/library/ReapThread.java +++ b/java/demo/Database/library/ReapThread.java @@ -32,9 +32,10 @@ class ReapThread extends Thread SessionI session; } - ReapThread(Ice.Logger logger) + ReapThread(Ice.Logger logger, long timeout) { _logger = logger; + _timeout = timeout; } synchronized public void @@ -44,7 +45,7 @@ class ReapThread extends Thread { try { - wait(1000); + wait((_timeout / 2) * 1000); } catch(InterruptedException e) { @@ -63,7 +64,7 @@ class ReapThread extends Thread // real-world example. Therefore the current time // is computed for each iteration. // - if((System.currentTimeMillis() - s.session.timestamp()) > _timeout) + if((System.currentTimeMillis() - s.session.timestamp()) > _timeout * 1000) { _logger.trace("ReapThread", "The session " + s.proxy.ice_getCommunicator().identityToString(s.proxy.ice_getIdentity()) + @@ -119,7 +120,7 @@ class ReapThread extends Thread _sessions.add(new SessionProxyPair(proxy, session)); } - private final long _timeout = 10 * 1000; // 10 seconds. + private final long _timeout; // Seconds. private Ice.Logger _logger; private boolean _terminated = false; private java.util.List<SessionProxyPair> _sessions = new java.util.LinkedList<SessionProxyPair>(); diff --git a/java/demo/Database/library/RunParser.java b/java/demo/Database/library/RunParser.java index a963dfff194..778c34063ba 100644 --- a/java/demo/Database/library/RunParser.java +++ b/java/demo/Database/library/RunParser.java @@ -77,7 +77,7 @@ class RunParser { _logger = logger; _session = session; - _timeout = timeout; + _timeout = timeout; // seconds. } synchronized public void @@ -87,7 +87,7 @@ class RunParser { try { - wait(_timeout); + wait(_timeout * 1000); } catch(InterruptedException e) { @@ -125,6 +125,7 @@ class RunParser { SessionAdapter session; Glacier2.RouterPrx router = Glacier2.RouterPrxHelper.uncheckedCast(communicator.getDefaultRouter()); + long timeout; if(router != null) { Glacier2.SessionPrx glacier2session = null; @@ -148,6 +149,7 @@ class RunParser try { glacier2session = router.createSession(id, pw); + timeout = router.getSessionTimeout() / 2; break; } catch(Glacier2.PermissionDeniedException ex) @@ -177,8 +179,9 @@ class RunParser } session = new DemoSessionAdapter(factory.create()); + timeout = factory.getSessionTimeout(); } - SessionRefreshThread refresh = new SessionRefreshThread(communicator.getLogger(), 5000, session); + SessionRefreshThread refresh = new SessionRefreshThread(communicator.getLogger(), timeout, session); refresh.start(); LibraryPrx library = session.getLibrary(); diff --git a/java/demo/Database/library/Server.java b/java/demo/Database/library/Server.java index 106ffa41df1..ba0929442ab 100644 --- a/java/demo/Database/library/Server.java +++ b/java/demo/Database/library/Server.java @@ -90,7 +90,9 @@ class LibraryServer extends Ice.Application return 1; } - ReapThread reaper = new ReapThread(logger); + long timeout = properties.getPropertyAsIntWithDefault("SessionTimeout", 30); + + ReapThread reaper = new ReapThread(logger, timeout); reaper.start(); // @@ -101,7 +103,7 @@ class LibraryServer extends Ice.Application SQLRequestContext.initialize(logger, pool); adapter.addServantLocator(new LocatorI(new BookI()), "book"); - adapter.add(new SessionFactoryI(logger, reaper), communicator().stringToIdentity("SessionFactory")); + adapter.add(new SessionFactoryI(logger, reaper, timeout), communicator().stringToIdentity("SessionFactory")); adapter.add(new Glacier2SessionManagerI(logger, reaper), communicator().stringToIdentity("LibrarySessionManager")); diff --git a/java/demo/Database/library/Session.ice b/java/demo/Database/library/Session.ice index f833c9d0394..7570f921b5f 100644 --- a/java/demo/Database/library/Session.ice +++ b/java/demo/Database/library/Session.ice @@ -65,6 +65,16 @@ interface SessionFactory * **/ Session* create(); + + /** + * + * Get the value of the session timeout. Sessions are destroyed + * if they see no activity for this period of time. + * + * @return The timeout (in seconds). + * + **/ + ["nonmutating", "cpp:const"] idempotent long getSessionTimeout(); }; }; diff --git a/java/demo/Database/library/SessionFactoryI.java b/java/demo/Database/library/SessionFactoryI.java index e2cb487e716..7d127d587d7 100644 --- a/java/demo/Database/library/SessionFactoryI.java +++ b/java/demo/Database/library/SessionFactoryI.java @@ -27,12 +27,20 @@ class SessionFactoryI extends _SessionFactoryDisp return proxy; } - SessionFactoryI(Ice.Logger logger, ReapThread reaper) + public long + getSessionTimeout(Ice.Current c) + { + return _timeout; + } + + SessionFactoryI(Ice.Logger logger, ReapThread reaper, long timeout) { _logger = logger; _reaper = reaper; + _timeout = timeout; } private Ice.Logger _logger; private ReapThread _reaper; + private long _timeout; } diff --git a/java/demo/Database/library/SessionI.java b/java/demo/Database/library/SessionI.java index fac1b73385e..4bf4a89d0cb 100644 --- a/java/demo/Database/library/SessionI.java +++ b/java/demo/Database/library/SessionI.java @@ -31,6 +31,16 @@ class SessionI implements _SessionOperations, _Glacier2SessionOperations _timestamp = System.currentTimeMillis(); } + synchronized public long + getSessionTimeout(Ice.Current c) + { + if(_destroyed) + { + throw new Ice.ObjectNotExistException(); + } + return 5000; + } + synchronized public void destroy(Ice.Current c) { diff --git a/java/demo/Database/library/config.server b/java/demo/Database/library/config.server index 8d3ec99b6db..591f2bf8346 100644 --- a/java/demo/Database/library/config.server +++ b/java/demo/Database/library/config.server @@ -17,6 +17,12 @@ JDBC.NumConnections=5 Ice.ThreadPool.Server.Size=5 # +# How long to keep sessions alive with no activity. Its best to use +# the same value as config.glacier2. +# +SessionTimeout=30 + +# # Warn about connection exceptions # Ice.Warn.Connections=1 |