Author: ron.sigal(a)jboss.com
Date: 2009-05-04 21:42:48 -0400 (Mon, 04 May 2009)
New Revision: 5125
Modified:
remoting2/branches/2.2.2-SP11_JBREM-1112/src/main/org/jboss/remoting/Client.java
Log:
JBREM-1112 (and others to be named): (1) Added new version of connect(); (2) added
disconnectAndNotify(); (3) changed synchronization.
Modified:
remoting2/branches/2.2.2-SP11_JBREM-1112/src/main/org/jboss/remoting/Client.java
===================================================================
---
remoting2/branches/2.2.2-SP11_JBREM-1112/src/main/org/jboss/remoting/Client.java 2009-05-02
00:37:05 UTC (rev 5124)
+++
remoting2/branches/2.2.2-SP11_JBREM-1112/src/main/org/jboss/remoting/Client.java 2009-05-05
01:42:48 UTC (rev 5125)
@@ -157,6 +157,9 @@
private static Map connectionValidators = new HashMap();
private static Object connectionValidatorLock = new Object();
+ static final String CLIENT = "client";
+ static final String CONNECTION_LISTENER = "connectionListener";
+
private static final Logger log = Logger.getLogger(Client.class);
private static final long serialVersionUID = 5679279425009837934L;
@@ -375,12 +378,12 @@
*
* @see org.jboss.remoting.ConnectionValidator
*/
- public void addConnectionListener(ConnectionListener listener, Map metadata)
+ public synchronized void addConnectionListener(ConnectionListener listener, Map
metadata)
{
if (invoker == null)
{
throw new RuntimeException("Can not add connection listener to remoting
client " +
- "until client has been connected.");
+ "while client is not connected.");
}
else
{
@@ -391,8 +394,8 @@
}
}
- synchronized (connectionValidatorLock)
- {
+// synchronized (connectionValidatorLock)
+// {
if (connectionValidator == null)
{
Map map = new HashMap(configuration);
@@ -415,6 +418,7 @@
}
else
{
+ log.debug(this + ": unable to reuse existing ConnectionValidator
in static map: " + connectionValidator);
connectionValidator = new ConnectionValidator(this, metadata);
connectionValidators.put(connectionValidatorKey, new
WeakReference(connectionValidator));
connectionValidator.addConnectionListener(this, listener);
@@ -430,6 +434,7 @@
}
else
{
+ log.debug(this + ": unable to reuse ConnectionValidator from local
reference: " + connectionValidator);
connectionValidator = new ConnectionValidator(this, metadata);
connectionValidators.put(connectionValidatorKey, new
WeakReference(connectionValidator));
connectionValidator.addConnectionListener(this, listener);
@@ -438,17 +443,17 @@
}
connectionListeners.add(listener);
- }
+// }
}
/**
* Removes specified connection listener. Will return true if it has already been
registered,
* false otherwise.
*/
- public boolean removeConnectionListener(ConnectionListener listener)
+ public synchronized boolean removeConnectionListener(ConnectionListener listener)
{
boolean isRemoved = false;
- synchronized (connectionValidatorLock)
+// synchronized (connectionValidatorLock)
{
if (connectionValidator == null)
{
@@ -515,6 +520,39 @@
*/
public void connect() throws Exception
{
+ connect(null, null);
+ }
+
+ /**
+ * Will cause the underlying transport to make connection to the target server. This
is
+ * important for any stateful transports, like socket or multiplex. This is also when
a client
+ * lease with the server is started. If listener is not null, it will be registered
to
+ * receive a callback if the connection fails.
+ */
+ public void connect(ConnectionListener listener) throws Exception
+ {
+ connect(listener, null);
+ }
+
+ /**
+ * Will cause the underlying transport to make connection to the target server. This
is
+ * important for any stateful transports, like socket or multiplex. This is also when
a client
+ * lease with the server is started. If listener is not null, it will be registered
to
+ * receive a callback if the connection fails.
+ * <p>
+ *
+ * If this version of connect() is used, and leasing is enabled, the concept of
"connection
+ * identity" is enforced. That is, the ConnectionValidator used by this Client
will be
+ * tied to the LeasePinger currently used by the MicroRemoteClientInvoker created or
reused
+ * in this method, and that LeasePinger will be tied to this Client and its
ConnectionValidator.
+ * If the ConnectionValidator used by any of the Clients associated with the
MicroRemoteClientInvoker
+ * used by this Client detects a broken connection, it will shut down that
LeasePinger.
+ * Moreover, each ConnectionValidator associated with that LeasePinger will notify
its
+ * ConnectionListeners of the broken connection. At that point, the LeasePinger will
be
+ * destroyed, and all of the associated Clients will be disconnected.
+ */
+ public void connect(ConnectionListener listener, Map metadata) throws Exception
+ {
if (isConnected())
return;
@@ -533,7 +571,7 @@
invoker = InvokerRegistry.createClientInvoker(locator, configuration);
}
- connect(invoker);
+ connect(invoker, listener, metadata);
connected = true;
}
@@ -544,14 +582,14 @@
* remoting client. Otherwise resource will not be cleaned up and if the target
server requires
* a lease, it will be maintained in the background.
*/
- public void disconnect()
+ public synchronized void disconnect()
{
if (invoker != null)
{
// this is a noop if no lease is active
invoker.terminateLease(sessionId, disconnectTimeout);
- synchronized (connectionValidatorLock)
+// synchronized (connectionValidatorLock)
{
if (connectionValidator != null)
{
@@ -1600,18 +1638,30 @@
// Package protected
----------------------------------------------------------------------------
+ synchronized void disconnectAndNotify()
+ {
+// synchronized (connectionValidatorLock)
+ {
+ disconnect();
+ if (connectionValidator != null)
+ {
+ connectionValidator.notifyListeners(new Exception("Could not connect to
server!"));
+ }
+ }
+ }
+
// Protected
------------------------------------------------------------------------------------
// Private
--------------------------------------------------------------------------------------
- private void connect(ClientInvoker invoker)
+ private void connect(ClientInvoker invoker, ConnectionListener listener, Map
metadata)
{
if (invoker != null)
{
invoker.connect();
try
{
- setupClientLease(invoker);
+ setupClientLease(invoker, listener, metadata);
}
catch (Throwable throwable)
{
@@ -1629,7 +1679,7 @@
}
}
- private void setupClientLease(ClientInvoker invoker) throws Throwable
+ private void setupClientLease(ClientInvoker invoker, ConnectionListener listener, Map
metadata) throws Throwable
{
long leasePeriod = -1;
boolean enableLease = false;
@@ -1712,8 +1762,19 @@
if (enableLease)
{
- invoker.establishLease(sessionId, configuration, leasePeriod);
+ Map temp = new HashMap(configuration);
+ if (metadata != null)
+ {
+ temp.putAll(metadata);
+ }
+ temp.put(CLIENT, this);
+ temp.put(CONNECTION_LISTENER, listener);
+ invoker.establishLease(sessionId, temp, leasePeriod);
}
+ else if (listener != null)
+ {
+ addConnectionListener(listener, configuration);
+ }
}
private Object invoke(Object param, Map metadata, InvokerLocator
callbackServerLocator)