activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dej...@apache.org
Subject svn commit: r1494142 - in /activemq/trunk/activemq-camel/src/main/java/org/apache/activemq/camel/component: ActiveMQComponent.java CamelEndpointLoader.java
Date Tue, 18 Jun 2013 14:16:41 GMT
Author: dejanb
Date: Tue Jun 18 14:16:40 2013
New Revision: 1494142

URL: http://svn.apache.org/r1494142
Log:
https://issues.apache.org/jira/browse/AMQ-4574 - better initialization of destination source
for camel endpoint completer

Modified:
    activemq/trunk/activemq-camel/src/main/java/org/apache/activemq/camel/component/ActiveMQComponent.java
    activemq/trunk/activemq-camel/src/main/java/org/apache/activemq/camel/component/CamelEndpointLoader.java

Modified: activemq/trunk/activemq-camel/src/main/java/org/apache/activemq/camel/component/ActiveMQComponent.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-camel/src/main/java/org/apache/activemq/camel/component/ActiveMQComponent.java?rev=1494142&r1=1494141&r2=1494142&view=diff
==============================================================================
--- activemq/trunk/activemq-camel/src/main/java/org/apache/activemq/camel/component/ActiveMQComponent.java
(original)
+++ activemq/trunk/activemq-camel/src/main/java/org/apache/activemq/camel/component/ActiveMQComponent.java
Tue Jun 18 14:16:40 2013
@@ -33,10 +33,11 @@ import org.apache.camel.spi.EndpointComp
 import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.URISupport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.jms.connection.SingleConnectionFactory;
 
 import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
 
 /**
  * The <a href="http://activemq.apache.org/camel/activemq.html">ActiveMQ Component</a>
@@ -48,12 +49,13 @@ public class ActiveMQComponent extends J
         new CopyOnWriteArrayList<SingleConnectionFactory>();
     private final CopyOnWriteArrayList<Service> pooledConnectionFactoryServiceList
=
         new CopyOnWriteArrayList<Service>();
+    private static final transient Logger LOG = LoggerFactory.getLogger(ActiveMQComponent.class);
     private boolean exposeAllQueues;
     private CamelEndpointLoader endpointLoader;
 
     private EnhancedConnection connection;
-    private ConnectionFactory connectionFactory;
     DestinationSource source;
+    boolean sourceInitialized = false;
 
     /**
      * Creates an <a href="http://camel.apache.org/activemq.html">ActiveMQ Component</a>
@@ -173,26 +175,38 @@ public class ActiveMQComponent extends J
     protected void doStart() throws Exception {
         super.doStart();
 
-        if (connection == null) {
-            Connection value = getConnectionFactory().createConnection();
-            if (value instanceof EnhancedConnection) {
-                connection = (EnhancedConnection) value;
-            }
-            else {
-                throw new IllegalArgumentException("Created JMS Connection is not an EnhancedConnection:
" + value);
-            }
-        }
-        connection.start();
-        source = connection.getDestinationSource();
-
         if (isExposeAllQueues()) {
+            createDestinationSource();
             endpointLoader = new CamelEndpointLoader(getCamelContext(), source);
             endpointLoader.afterPropertiesSet();
         }
     }
 
+    protected void createDestinationSource() {
+        try {
+            if (source == null) {
+                if (connection == null) {
+                    Connection value = getConfiguration().getConnectionFactory().createConnection();
+                    if (value instanceof EnhancedConnection) {
+                        connection = (EnhancedConnection) value;
+                    } else {
+                        throw new IllegalArgumentException("Created JMS Connection is not
an EnhancedConnection: " + value);
+                    }
+                    connection.start();
+                }
+                source = connection.getDestinationSource();
+            }
+        } catch (Throwable t) {
+            LOG.info("Can't get destination source, endpoint completer will not work", t);
+        }
+    }
+
     @Override
     protected void doStop() throws Exception {
+        if (source != null) {
+            source.stop();
+            source = null;
+        }
         if (connection != null) {
             connection.close();
             connection = null;
@@ -223,32 +237,32 @@ public class ActiveMQComponent extends J
         return answer;
     }
 
-    public ConnectionFactory getConnectionFactory() {
-        if (connectionFactory == null
-                && getConfiguration() instanceof ActiveMQConfiguration) {
-            connectionFactory = ((ActiveMQConfiguration)getConfiguration()).createConnectionFactory();
-        }
-        return connectionFactory;
-    }
-
     @Override
     public List<String> completeEndpointPath(ComponentConfiguration componentConfiguration,
String completionText) {
-        Set candidates = source.getQueues();
-        String destinationName = completionText;
-        if (completionText.startsWith("topic:")) {
-            candidates = source.getTopics();
-            destinationName = completionText.substring(6);
-        } else if (completionText.startsWith("queue:")) {
-            destinationName = completionText.substring(6);
+        // try to initialize destination source only the first time
+        if (!sourceInitialized) {
+            createDestinationSource();
+            sourceInitialized = true;
         }
-
-        Iterator it = candidates.iterator();
         ArrayList<String> answer = new ArrayList<String>();
-        while (it.hasNext()) {
-           ActiveMQDestination destination = (ActiveMQDestination)it.next();
-           if (destination.getPhysicalName().startsWith(destinationName)) {
-               answer.add(destination.getPhysicalName());
-           }
+        if (source != null) {
+            Set candidates = source.getQueues();
+            String destinationName = completionText;
+            if (completionText.startsWith("topic:")) {
+                candidates = source.getTopics();
+                destinationName = completionText.substring(6);
+            } else if (completionText.startsWith("queue:")) {
+                destinationName = completionText.substring(6);
+            }
+
+            Iterator it = candidates.iterator();
+
+            while (it.hasNext()) {
+                ActiveMQDestination destination = (ActiveMQDestination) it.next();
+                if (destination.getPhysicalName().startsWith(destinationName)) {
+                    answer.add(destination.getPhysicalName());
+                }
+            }
         }
         return answer;
     }

Modified: activemq/trunk/activemq-camel/src/main/java/org/apache/activemq/camel/component/CamelEndpointLoader.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-camel/src/main/java/org/apache/activemq/camel/component/CamelEndpointLoader.java?rev=1494142&r1=1494141&r2=1494142&view=diff
==============================================================================
--- activemq/trunk/activemq-camel/src/main/java/org/apache/activemq/camel/component/CamelEndpointLoader.java
(original)
+++ activemq/trunk/activemq-camel/src/main/java/org/apache/activemq/camel/component/CamelEndpointLoader.java
Tue Jun 18 14:16:40 2013
@@ -67,43 +67,41 @@ public class CamelEndpointLoader impleme
      */
     @PostConstruct
     public void afterPropertiesSet() throws Exception {
-        source.setDestinationListener(new DestinationListener() {
-            public void onDestinationEvent(DestinationEvent event) {
-                try {
-                    ActiveMQDestination destination = event.getDestination();
-                    if (destination instanceof ActiveMQQueue) {
-                        ActiveMQQueue queue = (ActiveMQQueue) destination;
-                        if (event.isAddOperation()) {
-                            addQueue(queue);
+        if (source != null) {
+            source.setDestinationListener(new DestinationListener() {
+                public void onDestinationEvent(DestinationEvent event) {
+                    try {
+                        ActiveMQDestination destination = event.getDestination();
+                        if (destination instanceof ActiveMQQueue) {
+                            ActiveMQQueue queue = (ActiveMQQueue) destination;
+                            if (event.isAddOperation()) {
+                                addQueue(queue);
+                            } else {
+                                removeQueue(queue);
+                            }
+                        } else if (destination instanceof ActiveMQTopic) {
+                            ActiveMQTopic topic = (ActiveMQTopic) destination;
+                            if (event.isAddOperation()) {
+                                addTopic(topic);
+                            } else {
+                                removeTopic(topic);
+                            }
                         }
-                        else {
-                            removeQueue(queue);
-                        }
-                    }
-                    else if (destination instanceof ActiveMQTopic) {
-                      ActiveMQTopic topic = (ActiveMQTopic) destination;
-                      if (event.isAddOperation()) {
-                          addTopic(topic);
-                      }
-                      else {
-                          removeTopic(topic);
-                      }
+                    } catch (Exception e) {
+                        LOG.warn("Caught: " + e, e);
                     }
                 }
-                catch (Exception e) {
-                    LOG.warn("Caught: " + e, e);
-                }
-            }
-        });
+            });
 
-        Set<ActiveMQQueue> queues = source.getQueues();
-        for (ActiveMQQueue queue : queues) {
-            addQueue(queue);
-        }
+            Set<ActiveMQQueue> queues = source.getQueues();
+            for (ActiveMQQueue queue : queues) {
+                addQueue(queue);
+            }
 
-        Set<ActiveMQTopic> topics = source.getTopics();
-        for (ActiveMQTopic topic : topics) {
-            addTopic(topic);
+            Set<ActiveMQTopic> topics = source.getTopics();
+            for (ActiveMQTopic topic : topics) {
+                addTopic(topic);
+            }
         }
     }
 



Mime
View raw message