[jboss-cvs] JBoss Messaging SVN: r7939 - in branches/Branch_JBossMessaging_1_4_0_SP3_CP08.patch01_JBMESSAGING_1776: tests/src/org/jboss/test/messaging/jms and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Feb 8 12:49:52 EST 2010
Author: jbertram at redhat.com
Date: 2010-02-08 12:49:51 -0500 (Mon, 08 Feb 2010)
New Revision: 7939
Modified:
branches/Branch_JBossMessaging_1_4_0_SP3_CP08.patch01_JBMESSAGING_1776/src/main/org/jboss/jms/client/remoting/ConsolidatedRemotingConnectionListener.java
branches/Branch_JBossMessaging_1_4_0_SP3_CP08.patch01_JBMESSAGING_1776/tests/src/org/jboss/test/messaging/jms/ConnectionTest.java
Log:
JBPAPP-3295
Modified: branches/Branch_JBossMessaging_1_4_0_SP3_CP08.patch01_JBMESSAGING_1776/src/main/org/jboss/jms/client/remoting/ConsolidatedRemotingConnectionListener.java
===================================================================
--- branches/Branch_JBossMessaging_1_4_0_SP3_CP08.patch01_JBMESSAGING_1776/src/main/org/jboss/jms/client/remoting/ConsolidatedRemotingConnectionListener.java 2010-02-08 06:38:56 UTC (rev 7938)
+++ branches/Branch_JBossMessaging_1_4_0_SP3_CP08.patch01_JBMESSAGING_1776/src/main/org/jboss/jms/client/remoting/ConsolidatedRemotingConnectionListener.java 2010-02-08 17:49:51 UTC (rev 7939)
@@ -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()
{
Modified: branches/Branch_JBossMessaging_1_4_0_SP3_CP08.patch01_JBMESSAGING_1776/tests/src/org/jboss/test/messaging/jms/ConnectionTest.java
===================================================================
--- branches/Branch_JBossMessaging_1_4_0_SP3_CP08.patch01_JBMESSAGING_1776/tests/src/org/jboss/test/messaging/jms/ConnectionTest.java 2010-02-08 06:38:56 UTC (rev 7938)
+++ branches/Branch_JBossMessaging_1_4_0_SP3_CP08.patch01_JBMESSAGING_1776/tests/src/org/jboss/test/messaging/jms/ConnectionTest.java 2010-02-08 17:49:51 UTC (rev 7939)
@@ -39,6 +39,7 @@
import org.jboss.jms.tx.ResourceManager;
import org.jboss.jms.tx.ResourceManagerFactory;
import org.jboss.logging.Logger;
+import org.jboss.test.messaging.tools.ServerManagement;
/**
@@ -356,6 +357,78 @@
conn.close();
}
+
+ /**
+ * setExceptionListener should throw exception when
+ * the connection is bad.
+ * https://jira.jboss.org/jira/browse/JBMESSAGING-1776
+ */
+ public void testExceptionListener2() throws Exception
+ {
+ if (!this.isRemote()) return;
+
+ Connection conn1 = null;
+ Connection conn2 = null;
+
+ try
+ {
+ MyExceptionListener listener1 = new MyExceptionListener();
+ MyExceptionListener listener2 = new MyExceptionListener();
+
+ conn1 = cf.createConnection();
+ conn2 = cf.createConnection();
+
+ conn1.setExceptionListener(listener1);
+
+ //kill the server
+ ServerManagement.kill(0);
+
+ while (listener1.exceptionReceived == null)
+ {
+ try
+ {
+ log.info("sleeping");
+ Thread.sleep(1000);
+ }
+ catch (InterruptedException e)
+ {
+ //ignore
+ }
+ }
+
+ //sleep 5 more sec to make sure conn2's listener having been called
+ try
+ {
+ log.info("sleeping");
+ Thread.sleep(5000);
+ }
+ catch (InterruptedException e)
+ {
+ //ignore
+ }
+
+ //should throw exception
+ log.debug("------------------------seting listener 2");
+ conn2.setExceptionListener(listener2);
+ log.debug("--------------------listener 2 set");
+
+ listener2.waitForException(5000);
+
+ assertNotNull(listener2.exceptionReceived);
+
+ }
+ finally
+ {
+ if (conn1 != null)
+ {
+ conn1.close();
+ }
+ if (conn2 != null)
+ {
+ conn2.close();
+ }
+ }
+ }
/*
* See http://jira.jboss.com/jira/browse/JBMESSAGING-635
@@ -424,10 +497,27 @@
{
JMSException exceptionReceived;
- public void onException(JMSException exception)
+ public synchronized void onException(JMSException exception)
{
this.exceptionReceived = exception;
log.trace("Received exception");
+ log.debug(this + "--------------------------exception got: " + exceptionReceived);
+ notify();
}
+
+ public synchronized void waitForException(long i)
+ {
+ if (exceptionReceived == null)
+ {
+ try
+ {
+ wait(i);
+ }
+ catch(InterruptedException e)
+ {
+ //ignore
+ }
+ }
+ }
}
}
More information about the jboss-cvs-commits
mailing list