summaryrefslogtreecommitdiff
path: root/java/demo/IceGrid/sessionActivation/Client.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/demo/IceGrid/sessionActivation/Client.java')
-rw-r--r--java/demo/IceGrid/sessionActivation/Client.java70
1 files changed, 58 insertions, 12 deletions
diff --git a/java/demo/IceGrid/sessionActivation/Client.java b/java/demo/IceGrid/sessionActivation/Client.java
index 7b89d4b16f9..3449068b79a 100644
--- a/java/demo/IceGrid/sessionActivation/Client.java
+++ b/java/demo/IceGrid/sessionActivation/Client.java
@@ -11,6 +11,23 @@ import Demo.*;
public class Client extends Ice.Application
{
+ class ShutdownHook extends Thread
+ {
+ public void
+ run()
+ {
+ cleanup();
+ try
+ {
+ communicator().destroy();
+ }
+ catch(Ice.LocalException ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+ }
+
static private class SessionKeepAliveThread extends Thread
{
SessionKeepAliveThread(IceGrid.SessionPrx session, long timeout)
@@ -72,6 +89,13 @@ public class Client extends Ice.Application
public int
run(String[] args)
{
+ //
+ // Since this is an interactive demo we want to clear the
+ // Application installed interrupt callback and install our
+ // own shutdown hook.
+ //
+ setInterruptHook(new ShutdownHook());
+
int status = 0;
IceGrid.RegistryPrx registry =
IceGrid.RegistryPrxHelper.checkedCast(communicator().stringToProxy("DemoIceGrid/Registry"));
@@ -82,7 +106,6 @@ public class Client extends Ice.Application
}
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
- IceGrid.SessionPrx session;
while(true)
{
System.out.println("This demo accepts any user-id / password combination.");
@@ -101,7 +124,10 @@ public class Client extends Ice.Application
try
{
- session = registry.createSession(id, pw);
+ synchronized(this)
+ {
+ _session = registry.createSession(id, pw);
+ }
break;
}
catch(IceGrid.PermissionDeniedException ex)
@@ -115,13 +141,16 @@ public class Client extends Ice.Application
}
}
- SessionKeepAliveThread keepAlive = new SessionKeepAliveThread(session, registry.getSessionTimeout() / 2);
- keepAlive.start();
+ synchronized(this)
+ {
+ _keepAlive = new SessionKeepAliveThread(_session, registry.getSessionTimeout() / 2);
+ _keepAlive.start();
+ }
try
{
HelloPrx hello = HelloPrxHelper.checkedCast(
- session.allocateObjectById(communicator().stringToIdentity("hello")));
+ _session.allocateObjectById(communicator().stringToIdentity("hello")));
menu();
@@ -182,22 +211,36 @@ public class Client extends Ice.Application
status = 1;
}
+ cleanup();
+
+ return status;
+ }
+
+ synchronized void
+ cleanup()
+ {
//
// Destroy the keepAlive thread and the sesion object otherwise
// the session will be kept allocated until the timeout occurs.
// Destroying the session will release all allocated objects.
//
- keepAlive.terminate();
- try
+ if(_keepAlive != null)
{
- keepAlive.join();
+ _keepAlive.terminate();
+ try
+ {
+ _keepAlive.join();
+ }
+ catch(InterruptedException e)
+ {
+ }
+ _keepAlive = null;
}
- catch(InterruptedException e)
+ if(_session != null)
{
+ _session.destroy();
+ _session = null;
}
- session.destroy();
-
- return status;
}
public static void
@@ -207,4 +250,7 @@ public class Client extends Ice.Application
int status = app.main("Client", args, "config.client");
System.exit(status);
}
+
+ SessionKeepAliveThread _keepAlive;
+ IceGrid.SessionPrx _session;
}