Author: ron.sigal(a)jboss.com
Date: 2009-05-14 22:13:24 -0400 (Thu, 14 May 2009)
New Revision: 5218
Modified:
remoting2/branches/2.x/src/main/org/jboss/remoting/LeasePinger.java
Log:
(1) JBREM-1132: Adopted use of leasePingerId; (2) JBREM-1133: adopted use of time stamp.
Modified: remoting2/branches/2.x/src/main/org/jboss/remoting/LeasePinger.java
===================================================================
--- remoting2/branches/2.x/src/main/org/jboss/remoting/LeasePinger.java 2009-05-15
02:06:55 UTC (rev 5217)
+++ remoting2/branches/2.x/src/main/org/jboss/remoting/LeasePinger.java 2009-05-15
02:13:24 UTC (rev 5218)
@@ -2,7 +2,6 @@
import org.jboss.logging.Logger;
import org.jboss.remoting.transport.ClientInvoker;
-
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -27,6 +26,9 @@
public static final int DEFAULT_DISCONNECT_TIMEOUT = -1;
public static final String LEASE_PINGER_TIMEOUT = "leasePingerTimeout";
+ static final String LEASE_PINGER_ID = "leasePingerId";
+ static final String TIME_STAMP = "timeStamp";
+
// Static
---------------------------------------------------------------------------------------
private static boolean trace = log.isTraceEnabled();
@@ -40,6 +42,7 @@
private ClientInvoker invoker = null;
private String invokerSessionID = null;
+ private Map clientSessionIds = new ConcurrentHashMap();
private Map clients = new ConcurrentHashMap();
private TimerTask timerTask = null;
@@ -50,6 +53,9 @@
// The following variables exist for testing purposes.
private boolean pingInvoked;
private boolean pingSucceeded;
+
+ private String leasePingerId;
+ private boolean useClientConnectionIdentity;
// Constructors
---------------------------------------------------------------------------------
@@ -118,6 +124,18 @@
timerTask.cancel();
timerTask = null;
+ if (useClientConnectionIdentity)
+ {
+ Iterator it = clients.values().iterator();
+ while (it.hasNext())
+ {
+ Client client = (Client) it.next();
+ if (trace) log.trace(this + " calling " + client +
".notifyAndDisconnect()");
+ client.notifyListeners();
+ it.remove();
+ }
+ }
+
try
{
// sending request map with no ClientHolders will indicate to server
@@ -125,7 +143,7 @@
HashMap metadata = null;
// If disconnectTimeout == 0, skip network i/o.
- log.debug(this + ": disconnectTimeout: " + disconnectTimeout);
+ if (trace) log.trace(this + ": disconnectTimeout: " +
disconnectTimeout);
if (disconnectTimeout != 0)
{
if (disconnectTimeout > 0)
@@ -144,6 +162,25 @@
e.initCause(throwable);
throw e;
}
+
+ if (trace)
+ {
+ log.trace(this + " shut down");
+ if (!clientSessionIds.isEmpty())
+ {
+ log.trace(this + " " + clientSessionIds.size() + "
remaining clients:");
+ Iterator it = clientSessionIds.keySet().iterator();
+ while (it.hasNext())
+ {
+ log.trace(this + ": " + it.next());
+ }
+ clientSessionIds.clear();
+ }
+ else
+ {
+ log.trace(this + " No remaining clients");
+ }
+ }
}
}
@@ -156,8 +193,17 @@
if(trace) { log.trace(this + " adding new client with session ID " +
sessionID + " and lease period " + leasePeriod); }
+ if (useClientConnectionIdentity)
+ {
+ Client client = (Client) configuration.remove(Client.CLIENT);
+ if (client != null)
+ {
+ clients.put(sessionID, client);
+ }
+ }
+
ClientHolder newClient = new ClientHolder(sessionID, configuration, leasePeriod);
- clients.put(sessionID, newClient);
+ clientSessionIds.put(sessionID, newClient);
sendClientPing();
@@ -182,7 +228,9 @@
if(trace) { log.trace(this + " removing client with session ID " +
sessionID); }
- ClientHolder holder = (ClientHolder)clients.remove(sessionID);
+ // Don't remove holder until after client has been removed from server side
Lease, to
+ // avoid a race with LeaseTimerTask sending a PING without the Client being
removed.
+ ClientHolder holder = (ClientHolder)clientSessionIds.get(sessionID);
if (holder != null)
{
@@ -210,14 +258,20 @@
log.debug(this + " failed sending disconnect for client lease for "
+
"client with session ID " + sessionID);
}
+
+ clientSessionIds.remove(sessionID);
+ if (useClientConnectionIdentity)
+ {
+ clients.remove(sessionID);
+ }
}
else
{
log.debug(this + " tried to remove lease for client with session ID "
+ sessionID +
- ", but no such lease was found");
+ ", but no such lease was found: probably it was registered with
an older LeasePinger");
}
- if (clients.isEmpty())
+ if (clientSessionIds.isEmpty())
{
isLastClientLease = true;
if(trace) { log.trace(this + " has no more client leases"); }
@@ -229,7 +283,7 @@
long tempPingPeriod = defaultPingPeriod;
- for (Iterator i = clients.values().iterator(); i.hasNext(); )
+ for (Iterator i = clientSessionIds.values().iterator(); i.hasNext(); )
{
ClientHolder clientHolder = (ClientHolder)i.next();
long clientHolderLeasePeriod = clientHolder.getLeasePeriod();
@@ -265,7 +319,7 @@
}
// look to see if the client is still amont those serviced by this lease pinger
- if (clients.containsKey(sessionID))
+ if (clientSessionIds.containsKey(sessionID))
{
return pingPeriod;
}
@@ -277,7 +331,7 @@
public String toString()
{
- return "LeasePinger[" + invoker + "(" + invokerSessionID +
")]";
+ return "LeasePinger[" + leasePingerId + ":" + invoker +
"(" + invokerSessionID + ")]";
}
// Package protected
----------------------------------------------------------------------------
@@ -293,9 +347,29 @@
protected void setDisconnectTimeout(int disconnectTimeout)
{
this.disconnectTimeout = disconnectTimeout;
- log.debug(this + " setting disconnect timeout to: " +
disconnectTimeout);
+ if (trace) log.trace(this + " setting disconnect timeout to: " +
disconnectTimeout);
}
+ protected String getLeasePingerId()
+ {
+ return leasePingerId;
+ }
+
+ protected void setLeasePingerId(String leasePingerId)
+ {
+ this.leasePingerId = leasePingerId;
+ }
+
+ boolean isUseClientConnectionIdentity()
+ {
+ return useClientConnectionIdentity;
+ }
+
+ void setUseClientConnectionIdentity(boolean useClientConnectionIdentity)
+ {
+ this.useClientConnectionIdentity = useClientConnectionIdentity;
+ }
+
// Private
--------------------------------------------------------------------------------------
private void sendClientPing()
@@ -305,9 +379,9 @@
if(trace)
{
StringBuffer sb = new StringBuffer();
- if(clients != null)
+ if(clientSessionIds != null)
{
- for(Iterator i = clients.values().iterator(); i.hasNext(); )
+ for(Iterator i = clientSessionIds.values().iterator(); i.hasNext(); )
{
ClientHolder h = (ClientHolder)i.next();
sb.append("
").append(h.getSessionId()).append('\n');
@@ -315,20 +389,21 @@
}
log.trace(this + " sending ping to server. Currently managing lease
" +
- "for following clients:\n" + sb.toString());
+ "for following clients:\n" + sb.toString());
}
- Map clientsClone = new ConcurrentHashMap(clients);
+ Map clientsClone = new ConcurrentHashMap(clientSessionIds);
Map requestClients = new ConcurrentHashMap();
requestClients.put(ClientHolder.CLIENT_HOLDER_KEY, clientsClone);
+ requestClients.put(LeasePinger.LEASE_PINGER_ID, leasePingerId);
+ requestClients.put(TIME_STAMP, Long.toString(System.currentTimeMillis()));
if (leasePingerTimeout >= 0)
{
requestClients.put(ServerInvoker.TIMEOUT,
Integer.toString(leasePingerTimeout));
}
- InvocationRequest ir =
- new InvocationRequest(invokerSessionID, null, "$PING$",
requestClients, null, null);
+ InvocationRequest ir = new InvocationRequest(invokerSessionID, null,
"$PING$", requestClients, null, null);
pingSucceeded = false;
pingInvoked = true;
@@ -342,6 +417,7 @@
{
pingInvoked = false;
log.debug(this + " failed to ping to server", t);
+ log.warn(this + " failed to ping to server: " + t.getMessage());
}
}