Author: ritchiem
Date: Thu Feb  1 07:50:52 2007
New Revision: 502249

URL: http://svn.apache.org/viewvc?view=rev&rev=502249
Log:
QPID-330 Clients occasionally fail to notice connect

The AMQConnection.java constructor now deals with the full connection process. 
The failover thread should not be started. This allows the connection method to 
be simplified and not Thread.sleep waiting for the connection.

Modified:
    
incubator/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/AMQConnection.java

Modified: 
incubator/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/AMQConnection.java
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/AMQConnection.java?view=diff&rev=502249&r1=502248&r2=502249
==============================================================================
--- 
incubator/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/AMQConnection.java
 (original)
+++ 
incubator/qpid/trunk/qpid/java/client/src/main/java/org/apache/qpid/client/AMQConnection.java
 Thu Feb  1 07:50:52 2007
@@ -215,12 +215,13 @@
         Exception lastException = new Exception();
         lastException.initCause(new ConnectException());
 
-        while (lastException != null && checkException(lastException) && 
_failoverPolicy.failoverAllowed())
+        while (!_connected && _failoverPolicy.failoverAllowed())
         {
             try
             {
                 makeBrokerConnection(_failoverPolicy.getNextBrokerDetails());
                 lastException = null;
+                _connected = true;
             }
             catch (Exception e)
             {
@@ -232,34 +233,7 @@
 
         _logger.debug("Are we connected:" + _connected);
 
-        // Then the Failover Thread will handle conneciton
-        if (_failoverPolicy.failoverAllowed())
-        {
-            //TODO this needs to be redone so that we are not spinning.
-            // A suitable object should be set that is then waited on
-            // and only notified when a connection is made or when
-            // the AMQConnection gets closed.
-            while (!_connected && !_closed.get())
-            {
-                try
-                {
-                    _logger.debug("Sleeping.");
-                    Thread.sleep(100);
-                }
-                catch (InterruptedException ie)
-                {
-                    _logger.debug("Woken up.");
-                }
-            }
-            if (!_failoverPolicy.failoverAllowed() || 
_failoverPolicy.getCurrentBrokerDetails() == null)
-            {
-                if (_lastAMQException != null)
-                {
-                    throw _lastAMQException;
-                }
-            }
-        }
-        else
+        if (!_connected)
         {
             String message = null;
 
@@ -318,7 +292,7 @@
 
     private void setVirtualHost(String virtualHost)
     {
-        if(virtualHost.startsWith("/"))
+        if (virtualHost.startsWith("/"))
         {
             virtualHost = virtualHost.substring(1);
         }
@@ -403,7 +377,14 @@
 
     public boolean failoverAllowed()
     {
-        return _failoverPolicy.failoverAllowed();
+        if (!_connected)
+        {
+            return false;
+        }
+        else
+        {
+            return _failoverPolicy.failoverAllowed();
+        }
     }
 
     public Session createSession(final boolean transacted, final int 
acknowledgeMode) throws JMSException
@@ -815,6 +796,11 @@
         return _protocolHandler;
     }
 
+    public boolean started()
+    {
+        return _started;
+    }
+
     public void bytesSent(long writtenBytes)
     {
         if (_connectionListener != null)
@@ -1031,4 +1017,5 @@
                 AMQConnectionFactory.class.getName(),
                 null);          // factory location
     }
+
 }


Reply via email to