diff options
author | Matthew Newhook <matthew@zeroc.com> | 2014-07-23 15:06:02 -0230 |
---|---|---|
committer | Matthew Newhook <matthew@zeroc.com> | 2014-07-23 15:06:02 -0230 |
commit | 866f9ff17391176b836f9bb49f6da40c2c938441 (patch) | |
tree | 7366963294ef3356c7b887cd89af753988c21beb /java/demo/Ice/session | |
parent | adding ACM tests for Python/Ruby/PHP (diff) | |
download | ice-866f9ff17391176b836f9bb49f6da40c2c938441.tar.bz2 ice-866f9ff17391176b836f9bb49f6da40c2c938441.tar.xz ice-866f9ff17391176b836f9bb49f6da40c2c938441.zip |
ICE-4234 - Update Ice to use current Java threading constructs
- Use ScheduledThreadPoolDispatcher not IceUtilInternal.Timer.
- Use Ice timer in glacier2, Freeze impl.
- Align C++, C# with java changes.
- Database demo now supports mariadb.
Diffstat (limited to 'java/demo/Ice/session')
-rw-r--r-- | java/demo/Ice/session/Client.java | 87 | ||||
-rw-r--r-- | java/demo/Ice/session/ReapTask.java | 69 | ||||
-rw-r--r-- | java/demo/Ice/session/ReapThread.java | 87 | ||||
-rw-r--r-- | java/demo/Ice/session/Server.java | 14 | ||||
-rw-r--r-- | java/demo/Ice/session/SessionFactoryI.java | 4 |
5 files changed, 97 insertions, 164 deletions
diff --git a/java/demo/Ice/session/Client.java b/java/demo/Ice/session/Client.java index 0b9bc151e23..060e7a20ddf 100644 --- a/java/demo/Ice/session/Client.java +++ b/java/demo/Ice/session/Client.java @@ -28,55 +28,6 @@ public class Client extends Ice.Application } } - static private class SessionRefreshThread extends Thread - { - SessionRefreshThread(Ice.Logger logger, long timeout, SessionPrx session) - { - _logger = logger; - _session = session; - _timeout = timeout; - } - - synchronized public void - run() - { - while(!_terminated) - { - try - { - wait(_timeout); - } - catch(InterruptedException e) - { - } - if(!_terminated) - { - try - { - _session.refresh(); - } - catch(Ice.LocalException ex) - { - _logger.warning("SessionRefreshThread: " + ex); - _terminated = true; - } - } - } - } - - synchronized private void - terminate() - { - _terminated = true; - notify(); - } - - final private Ice.Logger _logger; - final private SessionPrx _session; - final private long _timeout; - private boolean _terminated = false; - } - private static void menu() { @@ -135,8 +86,24 @@ public class Client extends Ice.Application synchronized(this) { _session = factory.create(name); - _refresh = new SessionRefreshThread(communicator().getLogger(), 5000, _session); - _refresh.start(); + _executor.scheduleAtFixedRate(new Runnable() + { + public void + run() + { + try + { + _session.refresh(); + } + catch(Ice.LocalException ex) + { + communicator().getLogger().warning("SessionRefreshThread: " + ex); + // Exceptions thrown from the executor task supress subsequent execution + // of the task. + throw ex; + } + } + }, 5, 5, java.util.concurrent.TimeUnit.SECONDS); } java.util.ArrayList<HelloPrx> hellos = new java.util.ArrayList<HelloPrx>(); @@ -235,22 +202,10 @@ public class Client extends Ice.Application cleanup(boolean destroy) { // - // The refresher thread must be terminated before destroy is + // The refresher task must be terminated before destroy is // called, otherwise it might get ObjectNotExistException. // - if(_refresh != null) - { - _refresh.terminate(); - try - { - _refresh.join(); - } - catch(InterruptedException e) - { - } - _refresh = null; - } - + _executor.shutdown(); if(destroy && _session != null) { _session.destroy(); @@ -266,6 +221,6 @@ public class Client extends Ice.Application System.exit(status); } - private SessionRefreshThread _refresh = null; + private java.util.concurrent.ScheduledExecutorService _executor = java.util.concurrent.Executors.newScheduledThreadPool(1); private SessionPrx _session = null; } diff --git a/java/demo/Ice/session/ReapTask.java b/java/demo/Ice/session/ReapTask.java new file mode 100644 index 00000000000..39b8bb1fad5 --- /dev/null +++ b/java/demo/Ice/session/ReapTask.java @@ -0,0 +1,69 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +import Demo.*; + +class ReapTask implements Runnable +{ + static class SessionProxyPair + { + SessionProxyPair(Demo.SessionPrx p, SessionI s) + { + proxy = p; + session = s; + } + + Demo.SessionPrx proxy; + SessionI session; + } + + synchronized public void + run() + { + java.util.Iterator<SessionProxyPair> p = _sessions.iterator(); + while(p.hasNext()) + { + SessionProxyPair s = p.next(); + try + { + // + // Session destruction may take time in a + // real-world example. Therefore the current time + // is computed for each iteration. + // + if((System.currentTimeMillis() - s.session.timestamp()) > _timeout) + { + String name = s.proxy.getName(); + s.proxy.destroy(); + System.out.println("The session " + name + " has timed out."); + p.remove(); + } + } + catch(Ice.ObjectNotExistException e) + { + p.remove(); + } + } + } + + synchronized public void + terminate() + { + _sessions.clear(); + } + + synchronized public void + add(SessionPrx proxy, SessionI session) + { + _sessions.add(new SessionProxyPair(proxy, session)); + } + + private final long _timeout = 10 * 1000; // 10 seconds. + private java.util.List<SessionProxyPair> _sessions = new java.util.LinkedList<SessionProxyPair>(); +} diff --git a/java/demo/Ice/session/ReapThread.java b/java/demo/Ice/session/ReapThread.java deleted file mode 100644 index f29e589df70..00000000000 --- a/java/demo/Ice/session/ReapThread.java +++ /dev/null @@ -1,87 +0,0 @@ -// ********************************************************************** -// -// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. -// -// This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. -// -// ********************************************************************** - -import Demo.*; - -class ReapThread extends Thread -{ - static class SessionProxyPair - { - SessionProxyPair(Demo.SessionPrx p, SessionI s) - { - proxy = p; - session = s; - } - - Demo.SessionPrx proxy; - SessionI session; - } - - synchronized public void - run() - { - while(!_terminated) - { - try - { - wait(1000); - } - catch(InterruptedException e) - { - } - - if(!_terminated) - { - java.util.Iterator<SessionProxyPair> p = _sessions.iterator(); - while(p.hasNext()) - { - SessionProxyPair s = p.next(); - try - { - // - // Session destruction may take time in a - // real-world example. Therefore the current time - // is computed for each iteration. - // - if((System.currentTimeMillis() - s.session.timestamp()) > _timeout) - { - String name = s.proxy.getName(); - s.proxy.destroy(); - System.out.println("The session " + name + " has timed out."); - p.remove(); - } - } - catch(Ice.ObjectNotExistException e) - { - p.remove(); - } - } - } - } - } - - synchronized public void - terminate() - { - _terminated = true; - notify(); - - _sessions.clear(); - } - - synchronized public void - add(SessionPrx proxy, SessionI session) - { - _sessions.add(new SessionProxyPair(proxy, session)); - } - - private final long _timeout = 10 * 1000; // 10 seconds. - private boolean _terminated = false; - private java.util.List<SessionProxyPair> _sessions = new java.util.LinkedList<SessionProxyPair>(); -} diff --git a/java/demo/Ice/session/Server.java b/java/demo/Ice/session/Server.java index 558fc472aef..4f8582a0775 100644 --- a/java/demo/Ice/session/Server.java +++ b/java/demo/Ice/session/Server.java @@ -21,21 +21,17 @@ public class Server extends Ice.Application } Ice.ObjectAdapter adapter = communicator().createObjectAdapter("SessionFactory"); - ReapThread reaper = new ReapThread(); - reaper.start(); + + java.util.concurrent.ScheduledExecutorService executor = java.util.concurrent.Executors.newScheduledThreadPool(1); + ReapTask reaper = new ReapTask(); + executor.scheduleAtFixedRate(reaper, 1, 1, java.util.concurrent.TimeUnit.SECONDS); adapter.add(new SessionFactoryI(reaper), communicator().stringToIdentity("SessionFactory")); adapter.activate(); communicator().waitForShutdown(); + executor.shutdown(); reaper.terminate(); - try - { - reaper.join(); - } - catch(InterruptedException e) - { - } return 0; } diff --git a/java/demo/Ice/session/SessionFactoryI.java b/java/demo/Ice/session/SessionFactoryI.java index 95e6be954b2..0695713b271 100644 --- a/java/demo/Ice/session/SessionFactoryI.java +++ b/java/demo/Ice/session/SessionFactoryI.java @@ -11,7 +11,7 @@ import Demo.*; class SessionFactoryI extends _SessionFactoryDisp { - SessionFactoryI(ReapThread reaper) + SessionFactoryI(ReapTask reaper) { _reaper = reaper; } @@ -32,5 +32,5 @@ class SessionFactoryI extends _SessionFactoryDisp c.adapter.getCommunicator().shutdown(); } - private ReapThread _reaper; + private ReapTask _reaper; } |