[jboss-cvs] JBoss Messaging SVN: r7923 - branches/Branch_1_4/src/main/org/jboss/jms/client/remoting.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Dec 18 06:11:06 EST 2009


Author: gaohoward
Date: 2009-12-18 06:11:05 -0500 (Fri, 18 Dec 2009)
New Revision: 7923

Modified:
   branches/Branch_1_4/src/main/org/jboss/jms/client/remoting/ConsolidatedRemotingConnectionListener.java
Log:
JBMESSAGING-1776


Modified: branches/Branch_1_4/src/main/org/jboss/jms/client/remoting/ConsolidatedRemotingConnectionListener.java
===================================================================
--- branches/Branch_1_4/src/main/org/jboss/jms/client/remoting/ConsolidatedRemotingConnectionListener.java	2009-12-18 10:54:12 UTC (rev 7922)
+++ branches/Branch_1_4/src/main/org/jboss/jms/client/remoting/ConsolidatedRemotingConnectionListener.java	2009-12-18 11:11:05 UTC (rev 7923)
@@ -45,6 +45,8 @@
       
    private boolean started;
 
+   private JMSException jmsException;
+
    // Constructors ---------------------------------------------------------------------------------
 
    public ConsolidatedRemotingConnectionListener()
@@ -53,50 +55,38 @@
 
    // ConnectionListener implementation ------------------------------------------------------------
 
-   public void handleConnectionException(Throwable throwable, Client client)
+   //for simplicity, we changed from partial synchronization to full synchronization. This
+   //method is not supposed to have high concurrent access.
+   public synchronized void handleConnectionException(Throwable throwable, Client client)
    {
       if (!started)
       {
          return;
       }
             
-      // forward the exception to delegate listener and JMS ExceptionListeners; synchronize
-      // to avoid race conditions
-
-      ExceptionListener jmsExceptionListenerCopy;
-  
-      ConnectionFailureListener remotingListenerCopy;
-
-      synchronized(this)
-      {
-         jmsExceptionListenerCopy = jmsExceptionListener;
-
-         remotingListenerCopy = remotingListener;
-      }
-      
+      // forward the exception to delegate listener and JMS ExceptionListeners;
       boolean forwardToJMSListener = true;
 
-      if (remotingListenerCopy != null)
+      if (remotingListener != null)
       {
          try
          {
-            log.trace(this + " forwarding remoting failure \"" + throwable + "\" to " + remotingListenerCopy);
+            log.trace(this + " forwarding remoting failure \"" + throwable + "\" to " + remotingListener);
             
             //We only forward to the JMS listener if failover did not successfully handle the exception
             //If failover handled the exception transparently then there is effectively no problem
             //with the logical connection that the client needs to be aware of
-            forwardToJMSListener = !remotingListenerCopy.handleConnectionException(throwable, client);
+            forwardToJMSListener = !remotingListener.handleConnectionException(throwable, client);
          }
          catch(Exception e)
          {
-            log.warn("Failed to forward " + throwable + " to " + remotingListenerCopy, e);
+            log.warn("Failed to forward " + throwable + " to " + remotingListener, e);
          }
       }
       
-      if (forwardToJMSListener && jmsExceptionListenerCopy != null)
+      if (forwardToJMSListener)
       {
-         JMSException jmsException = null;
-
+         //the connection is broken, we need to remember this
          if (throwable instanceof Error)
          {
             final String msg = "Caught Error on underlying remoting connection";
@@ -116,8 +106,13 @@
             log.error(this + ": " + msg, throwable);
             jmsException = new JMSException(msg + ": " + throwable.getMessage());
          }
-
-         jmsExceptionListenerCopy.onException(jmsException);
+         
+         //if a listener already set, notify it instantly and forget the exception.
+         if (jmsExceptionListener != null)
+         {
+            jmsExceptionListener.onException(jmsException);
+            jmsException = null;
+         }
       }
    }
 
@@ -135,11 +130,33 @@
       remotingListener = l;
    }
 
-   public synchronized void addJMSExceptionListener(ExceptionListener jmsExceptionListener)
+   public synchronized void addJMSExceptionListener(ExceptionListener listener)
    {
-      log.trace(this + " adding JMS exception listener " + jmsExceptionListener);
-      this.jmsExceptionListener = jmsExceptionListener;
+      log.trace(this + " adding JMS exception listener " + listener + " current exception: " + jmsException);
+      
+      this.jmsExceptionListener = listener;
+      
+      //if a connection failure event already happened, we notify it.
+      //https://jira.jboss.org/jira/browse/JBMESSAGING-1776
+      if (jmsException != null)
+      {
+         final JMSException copy = jmsException;
+         jmsException = null;
+         notifyJMSExceptionListener(listener, copy);
+      }
    }
+   
+   //Called when the listener is added after the connection exception.
+   private void notifyJMSExceptionListener(final ExceptionListener l, final JMSException ex)
+   {
+      new Thread()
+      {
+         public void run()
+         {
+            l.onException(ex);
+         }
+      }.start();
+   }
 
    public synchronized ExceptionListener getJMSExceptionListener()
    {




More information about the jboss-cvs-commits mailing list