Author: ron.sigal(a)jboss.com
Date: 2008-02-25 19:48:33 -0500 (Mon, 25 Feb 2008)
New Revision: 3503
Modified:
remoting2/branches/2.2/src/main/org/jboss/remoting/ConnectionValidator.java
Log:
JBREM-892: Added facility for tying ConnectionValidator to Lease and LeasePinger.
Modified: remoting2/branches/2.2/src/main/org/jboss/remoting/ConnectionValidator.java
===================================================================
--- remoting2/branches/2.2/src/main/org/jboss/remoting/ConnectionValidator.java 2008-02-26
00:40:52 UTC (rev 3502)
+++ remoting2/branches/2.2/src/main/org/jboss/remoting/ConnectionValidator.java 2008-02-26
00:48:33 UTC (rev 3503)
@@ -68,6 +68,17 @@
*/
public static final String DEFAULT_NUMBER_OF_CONNECTION_RETRIES = "1";
+ /**
+ * Key to determine if ConnectionValidator should tie failure to presence
+ * of active lease on server side. Default value is "true".
+ */
+ public static final String TIE_TO_LEASE = "tieToLease";
+ /**
+ * Key to determine whether to stop ConnectionValidator when PING fails.
+ * Default value is "true".
+ */
+ public static final String STOP_LEASE_ON_FAILURE = "stopLeaseOnFailure";
+
// Static
---------------------------------------------------------------------------------------
private static boolean trace = log.isTraceEnabled();
@@ -197,6 +208,9 @@
private ClientInvoker clientInvoker;
private Object lock = new Object();
private volatile boolean stopped;
+ private String invokerSessionId;
+ private boolean tieToLease = true;
+ private boolean stopLeaseOnFailure = true;
// Constructors
---------------------------------------------------------------------------------
@@ -211,7 +225,7 @@
this.pingPeriod = pingPeriod;
this.listeners = new ArrayList();
this.stopped = false;
-
+ getParameters(client, new HashMap());
log.debug(this + " created");
}
@@ -221,60 +235,8 @@
this.pingPeriod = DEFAULT_PING_PERIOD;
this.listeners = new ArrayList();
this.stopped = false;
-
- Map config = client.getConfiguration();
- if (config != null)
- {
- Object o = config.get(VALIDATOR_PING_PERIOD);
- if (o != null)
- {
- if (o instanceof String)
- {
- try
- {
- pingPeriod = Long.parseLong((String)o);
- }
- catch (Exception e)
- {
- log.warn(this + " could not convert " + VALIDATOR_PING_PERIOD
+
- " value of " + o + " to a long value");
- }
- }
- else
- {
- log.warn(this + " could not convert " + VALIDATOR_PING_PERIOD +
- " value of " + o + " to a long value: must be a
String");
- }
- }
- }
-
- if (metadata != null)
- {
- this.metadata = new HashMap(metadata);
-
- Object o = metadata.get(VALIDATOR_PING_PERIOD);
- if (o != null)
- {
- if (o instanceof String)
- {
- try
- {
- pingPeriod = Long.parseLong((String)o);
- }
- catch (Exception e)
- {
- log.warn(this + " could not convert " + VALIDATOR_PING_PERIOD
+
- " value of " + o + " to a long value");
- }
- }
- else
- {
- log.warn(this + " could not convert " + VALIDATOR_PING_PERIOD +
- " value of " + o + " to a long value: must be a
String");
- }
- }
- }
-
+ this.metadata = new HashMap(metadata);
+ getParameters(client, metadata);
log.debug(this + " created");
}
@@ -304,20 +266,44 @@
try
{
if (trace) { log.trace(this + " pinging ..."); }
+
+ boolean isValid = false;
- boolean isValid = doCheckConnection(clientInvoker);
+ if (tieToLease && client.getLeasePeriod() > 0)
+ {
+ if (trace) log.trace(this + " sending PING tied to lease");
+ isValid = doCheckConnectionWithLease();
+ }
+ else
+ {
+ isValid = doCheckConnection(clientInvoker);
+ }
if (!isValid)
{
log.debug(this + "'s connections is invalid");
notifyListeners(new Exception("Could not connect to
server!"));
+
+ if (stopLeaseOnFailure)
+ {
+ log.debug(this + " detected connection failure: stopping
LeasePinger");
+ MicroRemoteClientInvoker invoker = (MicroRemoteClientInvoker)
client.getInvoker();
+ invoker.terminateLease(null, client.getDisconnectTimeout());
+ log.debug(this + " shut down lease pinger");
+ }
}
}
catch (Throwable thr)
{
log.debug(this + " got throwable while pinging", thr);
notifyListeners(thr);
+
+ if (stopLeaseOnFailure)
+ {
+ log.debug(this + " detected connection failure: stopping");
+ cancel();
+ }
}
}
}
@@ -383,6 +369,94 @@
// Private
--------------------------------------------------------------------------------------
+ private void getParameters(Client client, Map metadata)
+ {
+ getParametersFromMap(client.getConfiguration());
+ getParametersFromMap(metadata);
+
+ ClientInvoker clientInvoker = client.getInvoker();
+ if (clientInvoker instanceof MicroRemoteClientInvoker)
+ {
+ invokerSessionId = ((MicroRemoteClientInvoker) clientInvoker).getSessionId();
+ }
+ else
+ {
+ throw new RuntimeException("creating a ConnectionValidator on a local
connection");
+ }
+ }
+
+ private void getParametersFromMap(Map config)
+ {
+ if (config != null)
+ {
+ Object o = config.get(VALIDATOR_PING_PERIOD);
+ if (o != null)
+ {
+ if (o instanceof String)
+ {
+ try
+ {
+ pingPeriod = Long.parseLong((String)o);
+ }
+ catch (Exception e)
+ {
+ log.warn(this + " could not convert " + VALIDATOR_PING_PERIOD
+
+ " value of " + o + " to a long value");
+ }
+ }
+ else
+ {
+ log.warn(this + " could not convert " + VALIDATOR_PING_PERIOD +
+ " value of " + o + " to a long value: must be a
String");
+ }
+ }
+
+ o = config.get(TIE_TO_LEASE);
+ if (o != null)
+ {
+ if (o instanceof String)
+ {
+ try
+ {
+ tieToLease = Boolean.valueOf(((String) o)).booleanValue();
+ }
+ catch (Exception e)
+ {
+ log.warn(this + " could not convert " + TIE_TO_LEASE + "
value" +
+ " to a boolean: " + o);
+ }
+ }
+ else
+ {
+ log.warn(this + " could not convert " + TIE_TO_LEASE + "
value" +
+ " to a boolean: must be a String");
+ }
+ }
+
+ o = config.get(STOP_LEASE_ON_FAILURE);
+ if (o != null)
+ {
+ if (o instanceof String)
+ {
+ try
+ {
+ stopLeaseOnFailure = Boolean.valueOf(((String) o)).booleanValue();
+ }
+ catch (Exception e)
+ {
+ log.warn(this + " could not convert " + STOP_LEASE_ON_FAILURE
+ " value" +
+ " to a boolean: " + o);
+ }
+ }
+ else
+ {
+ log.warn(this + " could not convert " + STOP_LEASE_ON_FAILURE +
" value" +
+ " to a boolean: must be a String");
+ }
+ }
+ }
+ }
+
private void start()
{
configMap = createPingConfig(client.getConfiguration(), metadata);
@@ -412,11 +486,46 @@
log.debug(this + " started");
}
+
+ private boolean doCheckConnectionWithLease() throws Throwable
+ {
+ boolean pingWorked = false;
+ try
+ {
+ Map metadata = new HashMap();
+ metadata.put(ServerInvoker.INVOKER_SESSION_ID, this.invokerSessionId);
+ InvocationRequest ir =
+ new InvocationRequest(null, Subsystem.SELF, "$PING$", metadata,
null, null);
+
+ if (trace) { log.trace("pinging, sending " + ir + " over " +
clientInvoker); }
+
+ Object o = clientInvoker.invoke(ir);
+ if (o instanceof Boolean && !((Boolean) o).booleanValue())
+ {
+ // Server indicates lease has stopped.
+ throw new Exception();
+ }
+
+ if (trace) { log.trace("ConnectionValidator got successful ping using
" + clientInvoker);}
+
+ pingWorked = true;
+ }
+ catch (Throwable t)
+ {
+ log.debug("ConnectionValidator failed to ping via " + clientInvoker,
t);
+ }
+
+ return pingWorked;
+ }
+
private boolean doStop()
{
synchronized(lock)
{
+ if (stopped)
+ return false;
+
if (!listeners.isEmpty())
{
listeners.clear();