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/Database/library | |
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/Database/library')
-rw-r--r-- | java/demo/Database/library/Glacier2SessionManagerI.java | 4 | ||||
-rw-r--r-- | java/demo/Database/library/README | 23 | ||||
-rw-r--r-- | java/demo/Database/library/ReapTask.java (renamed from java/demo/Database/library/ReapThread.java) | 66 | ||||
-rw-r--r-- | java/demo/Database/library/RunParser.java | 120 | ||||
-rw-r--r-- | java/demo/Database/library/Server.java | 17 | ||||
-rw-r--r-- | java/demo/Database/library/SessionFactoryI.java | 4 | ||||
-rw-r--r-- | java/demo/Database/library/config.server | 11 |
7 files changed, 110 insertions, 135 deletions
diff --git a/java/demo/Database/library/Glacier2SessionManagerI.java b/java/demo/Database/library/Glacier2SessionManagerI.java index 3d6ab50db88..e1632ba66f1 100644 --- a/java/demo/Database/library/Glacier2SessionManagerI.java +++ b/java/demo/Database/library/Glacier2SessionManagerI.java @@ -27,12 +27,12 @@ class Glacier2SessionManagerI extends Glacier2._SessionManagerDisp return proxy; } - Glacier2SessionManagerI(Ice.Logger logger, ReapThread reaper) + Glacier2SessionManagerI(Ice.Logger logger, ReapTask reaper) { _logger = logger; _reaper = reaper; } private Ice.Logger _logger; - private ReapThread _reaper; + private ReapTask _reaper; } diff --git a/java/demo/Database/library/README b/java/demo/Database/library/README index a690d4dc6a6..286a16008d7 100644 --- a/java/demo/Database/library/README +++ b/java/demo/Database/library/README @@ -1,8 +1,8 @@ MySQL JDBC Demo =============== -This demo shows how to implement an Ice server that uses mysql through -a JDBC API and demonstrates the following techniques: +This demo shows how to implement an Ice server that uses MariaDB or mysql +through a JDBC API and demonstrates the following techniques: - Mapping relational data to Ice objects, and in particular the conversion between Ice and JDBC types. @@ -15,15 +15,24 @@ a JDBC API and demonstrates the following techniques: Building the demo ----------------- -1. Install mysql if necessary. +1. Install mysql or MariaDB as necessary. -2. Download version 5.1.23 of the mysql JDBC connector here: +2. Download version 5.1.31 of the mysql JDBC connector here: http://dev.mysql.com/downloads/connector/j/5.1.html - After extracting the archive, add mysql-connector-java-5.1.23-bin.jar + After extracting the archive, add mysql-connector-java-5.1.31-bin.jar to your CLASSPATH. + Alternatively, for MariaDB Download version 1.1.7 of the MariaDB Client + Library for Java Applications here: + + https://downloads.mariadb.org/client-java/+releases/ + + After downloading the jar, add mariadb-java-client-1.1.7.jar to your + CLASSPATH. You must also edit config.server and change the JDBC.Url and + JDBC.DriverClassName to the MariaDB configurations. + 3. Create a database named "library" and grant privileges to a user. In the commands below, replace USER with the name you have chosen and PASSWORD with a suitable password: @@ -56,8 +65,8 @@ Building the demo NOTES: These instructions assume that the demo server runs on the same - host as the mysql server. If you intend to run the demo server on - a different host than the mysql server, you will need to revise + host as the database server. If you intend to run the demo server on + a different host than the database server, you will need to revise the mysql privileges as well as the JDBC URL in config.server. In order to get correct results when using multiple concurrent diff --git a/java/demo/Database/library/ReapThread.java b/java/demo/Database/library/ReapTask.java index a5b43dc135d..3ded348c1a6 100644 --- a/java/demo/Database/library/ReapThread.java +++ b/java/demo/Database/library/ReapTask.java @@ -9,7 +9,7 @@ import Demo.*; -class ReapThread extends Thread +class ReapTask implements Runnable { static class SessionProxyPair { @@ -32,7 +32,7 @@ class ReapThread extends Thread SessionI session; } - ReapThread(Ice.Logger logger, long timeout) + ReapTask(Ice.Logger logger, long timeout) { _logger = logger; _timeout = timeout; @@ -41,60 +41,43 @@ class ReapThread extends Thread synchronized public void run() { - while(!_terminated) + java.util.Iterator<SessionProxyPair> p = _sessions.iterator(); + while(p.hasNext()) { + SessionProxyPair s = p.next(); try { - wait((_timeout / 2) * 1000); - } - catch(InterruptedException e) - { - } - - if(!_terminated) - { - java.util.Iterator<SessionProxyPair> p = _sessions.iterator(); - while(p.hasNext()) + // + // 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 * 1000) { - SessionProxyPair s = p.next(); - try + _logger.trace("ReapTask", "The session " + + s.proxy.ice_getCommunicator().identityToString(s.proxy.ice_getIdentity()) + + " has timed out."); + if(s.proxy != null) { - // - // 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 * 1000) - { - _logger.trace("ReapThread", "The session " + - s.proxy.ice_getCommunicator().identityToString(s.proxy.ice_getIdentity()) + - " has timed out."); - if(s.proxy != null) - { - s.proxy.destroy(); - } - else - { - s.glacier2proxy.destroy(); - } - p.remove(); - } + s.proxy.destroy(); } - catch(Ice.ObjectNotExistException e) + else { - p.remove(); + s.glacier2proxy.destroy(); } + p.remove(); } } + catch(Ice.ObjectNotExistException e) + { + p.remove(); + } } } synchronized public void terminate() { - _terminated = true; - notify(); - // Destroy each of the sessions, releasing any resources they // may hold. This calls directly on the session, not via the // proxy since terminate() is called after the communicator is @@ -119,8 +102,7 @@ class ReapThread extends Thread _sessions.add(new SessionProxyPair(proxy, session)); } - private final long _timeout; // Seconds. + private final long _timeout; 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 736dcb95267..691eda79a00 100644 --- a/java/demo/Database/library/RunParser.java +++ b/java/demo/Database/library/RunParser.java @@ -19,71 +19,21 @@ class RunParser public LibraryPrx getLibrary(); public void destroy(); public void refresh(); + public long getTimeout(); } - static private class SessionRefreshThread extends Thread - { - SessionRefreshThread(Ice.Logger logger, long timeout, SessionAdapter session) - { - _logger = logger; - _session = session; - _timeout = timeout; // seconds. - } - - synchronized public void - run() - { - while(!_terminated) - { - try - { - wait(_timeout * 1000); - } - 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 SessionAdapter _session; - final private long _timeout; - private boolean _terminated = false; - } - - static int - runParser(String appName, String[] args, Ice.Communicator communicator) - { + static SessionAdapter + createSession(String appName, Ice.Communicator communicator) { SessionAdapter session; final Glacier2.RouterPrx router = Glacier2.RouterPrxHelper.uncheckedCast(communicator.getDefaultRouter()); - long timeout; if(router != null) { Glacier2.SessionPrx glacier2session = null; + long timeout; java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); while(true) { System.out.println("This demo accepts any user-id / password combination."); - try { String id; @@ -116,6 +66,7 @@ class RunParser ex.printStackTrace(); } } + final long to = timeout; final Glacier2SessionPrx sess = Glacier2SessionPrxHelper.uncheckedCast(glacier2session); session = new SessionAdapter() { @@ -145,6 +96,11 @@ class RunParser { sess.refresh(); } + + public long getTimeout() + { + return to; + } }; } else @@ -154,10 +110,11 @@ class RunParser if(factory == null) { System.err.println(appName + ": invalid object reference"); - return 1; + return null; } final SessionPrx sess = factory.create(); + final long timeout = factory.getSessionTimeout()/2; session = new SessionAdapter() { public LibraryPrx getLibrary() @@ -174,11 +131,44 @@ class RunParser { sess.refresh(); } + + public long getTimeout() + { + return timeout; + } }; - timeout = factory.getSessionTimeout()/2; } - SessionRefreshThread refresh = new SessionRefreshThread(communicator.getLogger(), timeout, session); - refresh.start(); + return session; + } + + static int + runParser(String appName, String[] args, final Ice.Communicator communicator) + { + final SessionAdapter session = createSession(appName, communicator); + if(session == null) + { + return 1; + } + + java.util.concurrent.ScheduledExecutorService executor = java.util.concurrent.Executors.newScheduledThreadPool(1); + 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; + } + } + }, session.getTimeout(), session.getTimeout(), java.util.concurrent.TimeUnit.SECONDS); LibraryPrx library = session.getLibrary(); @@ -196,19 +186,7 @@ class RunParser rc = parser.parse(); } - if(refresh != null) - { - refresh.terminate(); - try - { - refresh.join(); - } - catch(InterruptedException e) - { - } - refresh = null; - } - + executor.shutdown(); session.destroy(); return rc; diff --git a/java/demo/Database/library/Server.java b/java/demo/Database/library/Server.java index c5fb2f4f940..e1f7b6816a2 100644 --- a/java/demo/Database/library/Server.java +++ b/java/demo/Database/library/Server.java @@ -64,7 +64,7 @@ class Server extends Ice.Application try { - Class.forName("com.mysql.jdbc.Driver").newInstance(); + Class.forName(properties.getProperty("JDBC.DriverClassName")).newInstance(); } catch(Exception e) { @@ -92,9 +92,10 @@ class Server extends Ice.Application long timeout = properties.getPropertyAsIntWithDefault("SessionTimeout", 30); - ReapThread reaper = new ReapThread(logger, timeout); - reaper.start(); - + java.util.concurrent.ScheduledExecutorService executor = java.util.concurrent.Executors.newScheduledThreadPool(1); + ReapTask reaper = new ReapTask(logger, timeout); + executor.scheduleAtFixedRate(reaper, timeout/2, timeout/2, java.util.concurrent.TimeUnit.SECONDS); + // // Create an object adapter // @@ -116,14 +117,8 @@ class Server extends Ice.Application communicator().waitForShutdown(); defaultInterrupt(); + executor.shutdown(); reaper.terminate(); - try - { - reaper.join(); - } - catch(InterruptedException e) - { - } pool.destroy(); diff --git a/java/demo/Database/library/SessionFactoryI.java b/java/demo/Database/library/SessionFactoryI.java index bf954a89774..b3c3606719a 100644 --- a/java/demo/Database/library/SessionFactoryI.java +++ b/java/demo/Database/library/SessionFactoryI.java @@ -33,7 +33,7 @@ class SessionFactoryI extends _SessionFactoryDisp return _timeout; } - SessionFactoryI(Ice.Logger logger, ReapThread reaper, long timeout) + SessionFactoryI(Ice.Logger logger, ReapTask reaper, long timeout) { _logger = logger; _reaper = reaper; @@ -41,6 +41,6 @@ class SessionFactoryI extends _SessionFactoryDisp } private Ice.Logger _logger; - private ReapThread _reaper; + private ReapTask _reaper; private long _timeout; } diff --git a/java/demo/Database/library/config.server b/java/demo/Database/library/config.server index 3438cd2f142..b8a0555d661 100644 --- a/java/demo/Database/library/config.server +++ b/java/demo/Database/library/config.server @@ -6,7 +6,18 @@ SessionFactory.Endpoints=tcp -h localhost -p 10000:ssl -h localhost -p 10001 # JDBC configuration. JDBC.Username=USER JDBC.Password=PASSWORD + +# +# For mysql use the following configuration. +# JDBC.Url=jdbc:mysql://localhost/library +JDBC.DriverClassName=com.mysql.jdbc.Driver + +# +# For MariaDB use the following configuration. +# +#JDBC.Url=jdbc:mariadb://localhost/library +#JDBC.DriverClassName=org.mariadb.jdbc.Driver # The number of connections in the JDBC connection pool. This number # should be at least as big as the number of the threads in the server |