You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2016/06/23 15:03:33 UTC

activemq git commit: https://issues.apache.org/jira/browse/AMQ-6334

Repository: activemq
Updated Branches:
  refs/heads/master d563e9019 -> 03785a4d5


https://issues.apache.org/jira/browse/AMQ-6334

Fix error handling for connection establishment on the bridge.

Project: http://git-wip-us.apache.org/repos/asf/activemq/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/03785a4d
Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/03785a4d
Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/03785a4d

Branch: refs/heads/master
Commit: 03785a4d53877bb4088e0d83558d09147e1f0124
Parents: d563e90
Author: Timothy Bish <ta...@gmail.com>
Authored: Thu Jun 23 11:03:06 2016 -0400
Committer: Timothy Bish <ta...@gmail.com>
Committed: Thu Jun 23 11:03:06 2016 -0400

----------------------------------------------------------------------
 .../network/jms/SimpleJmsQueueConnector.java    | 182 ++++++++++---------
 .../network/jms/SimpleJmsTopicConnector.java    | 182 ++++++++++---------
 2 files changed, 202 insertions(+), 162 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq/blob/03785a4d/activemq-broker/src/main/java/org/apache/activemq/network/jms/SimpleJmsQueueConnector.java
----------------------------------------------------------------------
diff --git a/activemq-broker/src/main/java/org/apache/activemq/network/jms/SimpleJmsQueueConnector.java b/activemq-broker/src/main/java/org/apache/activemq/network/jms/SimpleJmsQueueConnector.java
index 47961e9..34607d2 100755
--- a/activemq-broker/src/main/java/org/apache/activemq/network/jms/SimpleJmsQueueConnector.java
+++ b/activemq-broker/src/main/java/org/apache/activemq/network/jms/SimpleJmsQueueConnector.java
@@ -158,122 +158,142 @@ public class SimpleJmsQueueConnector extends JmsConnector {
     @Override
     protected void initializeForeignConnection() throws NamingException, JMSException {
 
-        final QueueConnection newConnection;
-
-        if (foreignConnection.get() == null) {
-            // get the connection factories
-            if (outboundQueueConnectionFactory == null) {
-                // look it up from JNDI
-                if (outboundQueueConnectionFactoryName != null) {
-                    outboundQueueConnectionFactory = jndiOutboundTemplate
-                        .lookup(outboundQueueConnectionFactoryName, QueueConnectionFactory.class);
+        QueueConnection newConnection = null;
+
+        try {
+            if (foreignConnection.get() == null) {
+                // get the connection factories
+                if (outboundQueueConnectionFactory == null) {
+                    // look it up from JNDI
+                    if (outboundQueueConnectionFactoryName != null) {
+                        outboundQueueConnectionFactory = jndiOutboundTemplate
+                            .lookup(outboundQueueConnectionFactoryName, QueueConnectionFactory.class);
+                        if (outboundUsername != null) {
+                            newConnection = outboundQueueConnectionFactory
+                                .createQueueConnection(outboundUsername, outboundPassword);
+                        } else {
+                            newConnection = outboundQueueConnectionFactory.createQueueConnection();
+                        }
+                    } else {
+                        throw new JMSException("Cannot create foreignConnection - no information");
+                    }
+                } else {
                     if (outboundUsername != null) {
                         newConnection = outboundQueueConnectionFactory
                             .createQueueConnection(outboundUsername, outboundPassword);
                     } else {
                         newConnection = outboundQueueConnectionFactory.createQueueConnection();
                     }
-                } else {
-                    throw new JMSException("Cannot create foreignConnection - no information");
                 }
             } else {
-                if (outboundUsername != null) {
-                    newConnection = outboundQueueConnectionFactory
-                        .createQueueConnection(outboundUsername, outboundPassword);
-                } else {
-                    newConnection = outboundQueueConnectionFactory.createQueueConnection();
-                }
+                // Clear if for now in case something goes wrong during the init.
+                newConnection = (QueueConnection) foreignConnection.getAndSet(null);
             }
-        } else {
-            // Clear if for now in case something goes wrong during the init.
-            newConnection = (QueueConnection) foreignConnection.getAndSet(null);
-        }
 
-        if (outboundClientId != null && outboundClientId.length() > 0) {
-            newConnection.setClientID(getOutboundClientId());
-        }
-        newConnection.start();
+            // Register for any async error notifications now so we can reset in the
+            // case where there's not a lot of activity and a connection drops.
+            newConnection.setExceptionListener(new ExceptionListener() {
+                @Override
+                public void onException(JMSException exception) {
+                    handleConnectionFailure(foreignConnection.get());
+                }
+            });
+
+            if (outboundClientId != null && outboundClientId.length() > 0) {
+                newConnection.setClientID(getOutboundClientId());
+            }
+            newConnection.start();
 
-        outboundMessageConvertor.setConnection(newConnection);
+            outboundMessageConvertor.setConnection(newConnection);
 
-        // Configure the bridges with the new Outbound connection.
-        initializeInboundDestinationBridgesOutboundSide(newConnection);
-        initializeOutboundDestinationBridgesOutboundSide(newConnection);
+            // Configure the bridges with the new Outbound connection.
+            initializeInboundDestinationBridgesOutboundSide(newConnection);
+            initializeOutboundDestinationBridgesOutboundSide(newConnection);
 
-        // Register for any async error notifications now so we can reset in the
-        // case where there's not a lot of activity and a connection drops.
-        newConnection.setExceptionListener(new ExceptionListener() {
-            @Override
-            public void onException(JMSException exception) {
-                handleConnectionFailure(newConnection);
+            // At this point all looks good, so this our current connection now.
+            foreignConnection.set(newConnection);
+        } catch (Exception ex) {
+            if (newConnection != null) {
+                try {
+                    newConnection.close();
+                } catch (Exception ignore) {}
             }
-        });
 
-        // At this point all looks good, so this our current connection now.
-        foreignConnection.set(newConnection);
+            throw ex;
+        }
     }
 
     @Override
     protected void initializeLocalConnection() throws NamingException, JMSException {
 
-        final QueueConnection newConnection;
-
-        if (localConnection.get() == null) {
-            // get the connection factories
-            if (localQueueConnectionFactory == null) {
-                if (embeddedConnectionFactory == null) {
-                    // look it up from JNDI
-                    if (localConnectionFactoryName != null) {
-                        localQueueConnectionFactory = jndiLocalTemplate
-                            .lookup(localConnectionFactoryName, QueueConnectionFactory.class);
-                        if (localUsername != null) {
-                            newConnection = localQueueConnectionFactory
-                                .createQueueConnection(localUsername, localPassword);
+        QueueConnection newConnection = null;
+
+        try {
+            if (localConnection.get() == null) {
+                // get the connection factories
+                if (localQueueConnectionFactory == null) {
+                    if (embeddedConnectionFactory == null) {
+                        // look it up from JNDI
+                        if (localConnectionFactoryName != null) {
+                            localQueueConnectionFactory = jndiLocalTemplate
+                                .lookup(localConnectionFactoryName, QueueConnectionFactory.class);
+                            if (localUsername != null) {
+                                newConnection = localQueueConnectionFactory
+                                    .createQueueConnection(localUsername, localPassword);
+                            } else {
+                                newConnection = localQueueConnectionFactory.createQueueConnection();
+                            }
                         } else {
-                            newConnection = localQueueConnectionFactory.createQueueConnection();
+                            throw new JMSException("Cannot create localConnection - no information");
                         }
                     } else {
-                        throw new JMSException("Cannot create localConnection - no information");
+                        newConnection = embeddedConnectionFactory.createQueueConnection();
                     }
                 } else {
-                    newConnection = embeddedConnectionFactory.createQueueConnection();
+                    if (localUsername != null) {
+                        newConnection = localQueueConnectionFactory.
+                                createQueueConnection(localUsername, localPassword);
+                    } else {
+                        newConnection = localQueueConnectionFactory.createQueueConnection();
+                    }
                 }
+
             } else {
-                if (localUsername != null) {
-                    newConnection = localQueueConnectionFactory.
-                            createQueueConnection(localUsername, localPassword);
-                } else {
-                    newConnection = localQueueConnectionFactory.createQueueConnection();
-                }
+                // Clear if for now in case something goes wrong during the init.
+                newConnection = (QueueConnection) localConnection.getAndSet(null);
             }
 
-        } else {
-            // Clear if for now in case something goes wrong during the init.
-            newConnection = (QueueConnection) localConnection.getAndSet(null);
-        }
+            // Register for any async error notifications now so we can reset in the
+            // case where there's not a lot of activity and a connection drops.
+            newConnection.setExceptionListener(new ExceptionListener() {
+                @Override
+                public void onException(JMSException exception) {
+                    handleConnectionFailure(localConnection.get());
+                }
+            });
 
-        if (localClientId != null && localClientId.length() > 0) {
-            newConnection.setClientID(getLocalClientId());
-        }
-        newConnection.start();
+            if (localClientId != null && localClientId.length() > 0) {
+                newConnection.setClientID(getLocalClientId());
+            }
+            newConnection.start();
 
-        inboundMessageConvertor.setConnection(newConnection);
+            inboundMessageConvertor.setConnection(newConnection);
 
-        // Configure the bridges with the new Local connection.
-        initializeInboundDestinationBridgesLocalSide(newConnection);
-        initializeOutboundDestinationBridgesLocalSide(newConnection);
+            // Configure the bridges with the new Local connection.
+            initializeInboundDestinationBridgesLocalSide(newConnection);
+            initializeOutboundDestinationBridgesLocalSide(newConnection);
 
-        // Register for any async error notifications now so we can reset in the
-        // case where there's not a lot of activity and a connection drops.
-        newConnection.setExceptionListener(new ExceptionListener() {
-            @Override
-            public void onException(JMSException exception) {
-                handleConnectionFailure(newConnection);
+            // At this point all looks good, so this our current connection now.
+            localConnection.set(newConnection);
+        } catch (Exception ex) {
+            if (newConnection != null) {
+                try {
+                    newConnection.close();
+                } catch (Exception ignore) {}
             }
-        });
 
-        // At this point all looks good, so this our current connection now.
-        localConnection.set(newConnection);
+            throw ex;
+        }
     }
 
     protected void initializeInboundDestinationBridgesOutboundSide(QueueConnection connection) throws JMSException {

http://git-wip-us.apache.org/repos/asf/activemq/blob/03785a4d/activemq-broker/src/main/java/org/apache/activemq/network/jms/SimpleJmsTopicConnector.java
----------------------------------------------------------------------
diff --git a/activemq-broker/src/main/java/org/apache/activemq/network/jms/SimpleJmsTopicConnector.java b/activemq-broker/src/main/java/org/apache/activemq/network/jms/SimpleJmsTopicConnector.java
index b451ad7..420a4af 100755
--- a/activemq-broker/src/main/java/org/apache/activemq/network/jms/SimpleJmsTopicConnector.java
+++ b/activemq-broker/src/main/java/org/apache/activemq/network/jms/SimpleJmsTopicConnector.java
@@ -157,122 +157,142 @@ public class SimpleJmsTopicConnector extends JmsConnector {
     @Override
     protected void initializeForeignConnection() throws NamingException, JMSException {
 
-        final TopicConnection newConnection;
-
-        if (foreignConnection.get() == null) {
-            // get the connection factories
-            if (outboundTopicConnectionFactory == null) {
-                // look it up from JNDI
-                if (outboundTopicConnectionFactoryName != null) {
-                    outboundTopicConnectionFactory = jndiOutboundTemplate
-                        .lookup(outboundTopicConnectionFactoryName, TopicConnectionFactory.class);
+        TopicConnection newConnection = null;
+
+        try {
+            if (foreignConnection.get() == null) {
+                // get the connection factories
+                if (outboundTopicConnectionFactory == null) {
+                    // look it up from JNDI
+                    if (outboundTopicConnectionFactoryName != null) {
+                        outboundTopicConnectionFactory = jndiOutboundTemplate
+                            .lookup(outboundTopicConnectionFactoryName, TopicConnectionFactory.class);
+                        if (outboundUsername != null) {
+                            newConnection = outboundTopicConnectionFactory
+                                .createTopicConnection(outboundUsername, outboundPassword);
+                        } else {
+                            newConnection = outboundTopicConnectionFactory.createTopicConnection();
+                        }
+                    } else {
+                        throw new JMSException("Cannot create foreignConnection - no information");
+                    }
+                } else {
                     if (outboundUsername != null) {
                         newConnection = outboundTopicConnectionFactory
                             .createTopicConnection(outboundUsername, outboundPassword);
                     } else {
                         newConnection = outboundTopicConnectionFactory.createTopicConnection();
                     }
-                } else {
-                    throw new JMSException("Cannot create foreignConnection - no information");
                 }
             } else {
-                if (outboundUsername != null) {
-                    newConnection = outboundTopicConnectionFactory
-                        .createTopicConnection(outboundUsername, outboundPassword);
-                } else {
-                    newConnection = outboundTopicConnectionFactory.createTopicConnection();
-                }
+                // Clear if for now in case something goes wrong during the init.
+                newConnection = (TopicConnection) foreignConnection.getAndSet(null);
             }
-        } else {
-            // Clear if for now in case something goes wrong during the init.
-            newConnection = (TopicConnection) foreignConnection.getAndSet(null);
-        }
 
-        if (outboundClientId != null && outboundClientId.length() > 0) {
-            newConnection.setClientID(getOutboundClientId());
-        }
-        newConnection.start();
+            // Register for any async error notifications now so we can reset in the
+            // case where there's not a lot of activity and a connection drops.
+            newConnection.setExceptionListener(new ExceptionListener() {
+                @Override
+                public void onException(JMSException exception) {
+                    handleConnectionFailure(foreignConnection.get());
+                }
+            });
+
+            if (outboundClientId != null && outboundClientId.length() > 0) {
+                newConnection.setClientID(getOutboundClientId());
+            }
+            newConnection.start();
 
-        outboundMessageConvertor.setConnection(newConnection);
+            outboundMessageConvertor.setConnection(newConnection);
 
-        // Configure the bridges with the new Outbound connection.
-        initializeInboundDestinationBridgesOutboundSide(newConnection);
-        initializeOutboundDestinationBridgesOutboundSide(newConnection);
+            // Configure the bridges with the new Outbound connection.
+            initializeInboundDestinationBridgesOutboundSide(newConnection);
+            initializeOutboundDestinationBridgesOutboundSide(newConnection);
 
-        // Register for any async error notifications now so we can reset in the
-        // case where there's not a lot of activity and a connection drops.
-        newConnection.setExceptionListener(new ExceptionListener() {
-            @Override
-            public void onException(JMSException exception) {
-                handleConnectionFailure(newConnection);
+            // At this point all looks good, so this our current connection now.
+            foreignConnection.set(newConnection);
+        } catch (Exception ex) {
+            if (newConnection != null) {
+                try {
+                    newConnection.close();
+                } catch (Exception ignore) {}
             }
-        });
 
-        // At this point all looks good, so this our current connection now.
-        foreignConnection.set(newConnection);
+            throw ex;
+        }
     }
 
     @Override
     protected void initializeLocalConnection() throws NamingException, JMSException {
 
-        final TopicConnection newConnection;
-
-        if (localConnection.get() == null) {
-            // get the connection factories
-            if (localTopicConnectionFactory == null) {
-                if (embeddedConnectionFactory == null) {
-                    // look it up from JNDI
-                    if (localConnectionFactoryName != null) {
-                        localTopicConnectionFactory = jndiLocalTemplate
-                            .lookup(localConnectionFactoryName, TopicConnectionFactory.class);
-                        if (localUsername != null) {
-                            newConnection = localTopicConnectionFactory
-                                .createTopicConnection(localUsername, localPassword);
+        TopicConnection newConnection = null;
+
+        try {
+            if (localConnection.get() == null) {
+                // get the connection factories
+                if (localTopicConnectionFactory == null) {
+                    if (embeddedConnectionFactory == null) {
+                        // look it up from JNDI
+                        if (localConnectionFactoryName != null) {
+                            localTopicConnectionFactory = jndiLocalTemplate
+                                .lookup(localConnectionFactoryName, TopicConnectionFactory.class);
+                            if (localUsername != null) {
+                                newConnection = localTopicConnectionFactory
+                                    .createTopicConnection(localUsername, localPassword);
+                            } else {
+                                newConnection = localTopicConnectionFactory.createTopicConnection();
+                            }
                         } else {
-                            newConnection = localTopicConnectionFactory.createTopicConnection();
+                            throw new JMSException("Cannot create localConnection - no information");
                         }
                     } else {
-                        throw new JMSException("Cannot create localConnection - no information");
+                        newConnection = embeddedConnectionFactory.createTopicConnection();
                     }
                 } else {
-                    newConnection = embeddedConnectionFactory.createTopicConnection();
+                    if (localUsername != null) {
+                        newConnection = localTopicConnectionFactory.
+                                createTopicConnection(localUsername, localPassword);
+                    } else {
+                        newConnection = localTopicConnectionFactory.createTopicConnection();
+                    }
                 }
+
             } else {
-                if (localUsername != null) {
-                    newConnection = localTopicConnectionFactory.
-                            createTopicConnection(localUsername, localPassword);
-                } else {
-                    newConnection = localTopicConnectionFactory.createTopicConnection();
-                }
+                // Clear if for now in case something goes wrong during the init.
+                newConnection = (TopicConnection) localConnection.getAndSet(null);
             }
 
-        } else {
-            // Clear if for now in case something goes wrong during the init.
-            newConnection = (TopicConnection) localConnection.getAndSet(null);
-        }
+            // Register for any async error notifications now so we can reset in the
+            // case where there's not a lot of activity and a connection drops.
+            newConnection.setExceptionListener(new ExceptionListener() {
+                @Override
+                public void onException(JMSException exception) {
+                    handleConnectionFailure(localConnection.get());
+                }
+            });
 
-        if (localClientId != null && localClientId.length() > 0) {
-            newConnection.setClientID(getLocalClientId());
-        }
-        newConnection.start();
+            if (localClientId != null && localClientId.length() > 0) {
+                newConnection.setClientID(getLocalClientId());
+            }
+            newConnection.start();
 
-        inboundMessageConvertor.setConnection(newConnection);
+            inboundMessageConvertor.setConnection(newConnection);
 
-        // Configure the bridges with the new Local connection.
-        initializeInboundDestinationBridgesLocalSide(newConnection);
-        initializeOutboundDestinationBridgesLocalSide(newConnection);
+            // Configure the bridges with the new Local connection.
+            initializeInboundDestinationBridgesLocalSide(newConnection);
+            initializeOutboundDestinationBridgesLocalSide(newConnection);
 
-        // Register for any async error notifications now so we can reset in the
-        // case where there's not a lot of activity and a connection drops.
-        newConnection.setExceptionListener(new ExceptionListener() {
-            @Override
-            public void onException(JMSException exception) {
-                handleConnectionFailure(newConnection);
+            // At this point all looks good, so this our current connection now.
+            localConnection.set(newConnection);
+        } catch (Exception ex) {
+            if (newConnection != null) {
+                try {
+                    newConnection.close();
+                } catch (Exception ignore) {}
             }
-        });
 
-        // At this point all looks good, so this our current connection now.
-        localConnection.set(newConnection);
+            throw ex;
+        }
     }
 
     protected void initializeInboundDestinationBridgesOutboundSide(TopicConnection connection) throws JMSException {