You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/11/21 17:52:57 UTC

[camel] branch master updated: CAMEL-14202: camel-activemq - Setting brokerURL should be used when using spring boot. Updated example to use camel-activemq-starter

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 2efb84b  CAMEL-14202: camel-activemq - Setting brokerURL should be used when using spring boot. Updated example to use camel-activemq-starter
2efb84b is described below

commit 2efb84bc0907b88612fe757ee88e703f1ef729f4
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Nov 21 18:52:03 2019 +0100

    CAMEL-14202: camel-activemq - Setting brokerURL should be used when using spring boot. Updated example to use camel-activemq-starter
---
 .../src/main/docs/activemq-component.adoc          |  8 ++--
 .../component/activemq/ActiveMQComponent.java      | 24 ++++++------
 .../component/activemq/ActiveMQConfiguration.java  | 45 +++++++++++++++-------
 .../camel-example-spring-boot-activemq/pom.xml     | 19 ++-------
 .../camel-example-spring-boot-activemq/readme.adoc |  5 ---
 .../src/main/resources/application.properties      | 10 ++++-
 .../springboot/ActiveMQComponentConfiguration.java | 10 +++--
 7 files changed, 65 insertions(+), 56 deletions(-)

diff --git a/components/camel-activemq/src/main/docs/activemq-component.adoc b/components/camel-activemq/src/main/docs/activemq-component.adoc
index 9bc7cc3..dcea7a7 100644
--- a/components/camel-activemq/src/main/docs/activemq-component.adoc
+++ b/components/camel-activemq/src/main/docs/activemq-component.adoc
@@ -55,10 +55,10 @@ The ActiveMQ component supports 87 options, which are listed below.
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
-| *brokerURL* (common) | Sets the broker URL to use to connect to ActiveMQ using the ActiveMQ URI format |  | String
-| *trustAllPackages* (common) | Define if all packages are trusted or not | false | boolean
-| *exposeAllQueues* (common) | If enabled this will cause all Queues in the ActiveMQ broker to be eagerly populated into the CamelContext so that they can be easily browsed by any Camel tooling. This option is disabled by default. | false | boolean
-| *usePooledConnection* (common) | Enables or disables whether a PooledConnectionFactory will be used so that when messages are sent to ActiveMQ from outside of a message consuming thread, pooling will be used rather than the default with the Spring JmsTemplate which will create a new connection, session, producer for each message then close them all down again. The default value is true. Note that this requires an extra dependency on commons-pool2. | false | boolean
+| *brokerURL* (common) | Sets the broker URL to use to connect to ActiveMQ |  | String
+| *trustAllPackages* (common) | Define if all Java packages are trusted or not (for Java object JMS message types). Notice its not recommended practice to send Java serialized objects over network. Setting this to true can expose security risks, so use this with care. | false | boolean
+| *exposeAllQueues* (advanced) | If enabled this will cause all Queues in the ActiveMQ broker to be eagerly populated into the CamelContext so that they can be easily browsed by any Camel tooling. This option is disabled by default. | false | boolean
+| *usePooledConnection* (common) | Enables or disables whether a PooledConnectionFactory will be used so that when messages are sent to ActiveMQ from outside of a message consuming thread, pooling will be used rather than the default with the Spring JmsTemplate which will create a new connection, session, producer for each message then close them all down again. The default value is true. | false | boolean
 | *useSingleConnection* (common) | Enables or disables whether a Spring SingleConnectionFactory will be used so that when messages are sent to ActiveMQ from outside of a message consuming thread, pooling will be used rather than the default with the Spring JmsTemplate which will create a new connection, session, producer for each message then close them all down again. The default value is false and a pooled connection is used by default. | false | boolean
 | *configuration* (common) | Configuration of ActiveMQ |  | JmsConfiguration
 | *acceptMessagesWhile Stopping* (consumer) | Specifies whether the consumer accept messages while it is stopping. You may consider enabling this option, if you start and stop JMS routes at runtime, while there are still messages enqueued on the queue. If this option is false, and you stop the JMS route, then messages may be rejected, and the JMS broker would have to attempt redeliveries, which yet again may be rejected, and eventually the message may be moved at a dead letter queue on t [...]
diff --git a/components/camel-activemq/src/main/java/org/apache/camel/component/activemq/ActiveMQComponent.java b/components/camel-activemq/src/main/java/org/apache/camel/component/activemq/ActiveMQComponent.java
index d162086..e6fc1f7 100644
--- a/components/camel-activemq/src/main/java/org/apache/camel/component/activemq/ActiveMQComponent.java
+++ b/components/camel-activemq/src/main/java/org/apache/camel/component/activemq/ActiveMQComponent.java
@@ -19,7 +19,6 @@ package org.apache.camel.component.activemq;
 import java.net.URISyntaxException;
 import java.util.Map;
 import java.util.concurrent.CopyOnWriteArrayList;
-
 import javax.jms.Connection;
 
 import org.apache.activemq.EnhancedConnection;
@@ -29,6 +28,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.jms.JmsComponent;
 import org.apache.camel.component.jms.JmsConfiguration;
+import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.annotations.Component;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.PropertiesHelper;
@@ -45,13 +45,14 @@ import org.springframework.jms.core.JmsTemplate;
 @Component("activemq")
 public class ActiveMQComponent extends JmsComponent {
     private static final transient Logger LOG = LoggerFactory.getLogger(ActiveMQComponent.class);
-    private DestinationSource source;
-    private boolean exposeAllQueues;
-    private CamelEndpointLoader endpointLoader;
-    private EnhancedConnection connection;
+    private volatile DestinationSource source;
+    private volatile EnhancedConnection connection;
+    private volatile CamelEndpointLoader endpointLoader;
     private final CopyOnWriteArrayList<SingleConnectionFactory> singleConnectionFactoryList = new CopyOnWriteArrayList<>();
     private final CopyOnWriteArrayList<Service> pooledConnectionFactoryServiceList = new CopyOnWriteArrayList<>();
-    
+    @Metadata(label = "advanced")
+    private boolean exposeAllQueues;
+
     public ActiveMQComponent() {
     }
     
@@ -92,9 +93,7 @@ public class ActiveMQComponent extends JmsComponent {
     }
 
     /**
-     * Sets the broker URL to use to connect to ActiveMQ using the
-     * <a href="http://activemq.apache.org/configuring-transports.html">ActiveMQ
-     * URI format</a>
+     * Sets the broker URL to use to connect to ActiveMQ
      */
     public void setBrokerURL(String brokerURL) {
         if (getConfiguration() instanceof ActiveMQConfiguration) {
@@ -103,7 +102,9 @@ public class ActiveMQComponent extends JmsComponent {
     }
 
     /**
-     * Define if all packages are trusted or not
+     * Define if all Java packages are trusted or not (for Java object JMS message types).
+     * Notice its not recommended practice to send Java serialized objects over network.
+     * Setting this to true can expose security risks, so use this with care.
      */
     public void setTrustAllPackages(boolean trustAllPackages) {
         if (getConfiguration() instanceof ActiveMQConfiguration) {
@@ -131,8 +132,7 @@ public class ActiveMQComponent extends JmsComponent {
      * Spring {@link JmsTemplate} which will create a new connection, session,
      * producer for each message then close them all down again.
      * <p/>
-     * The default value is true. Note that this requires an extra dependency on
-     * commons-pool2.
+     * The default value is true.
      */
     public void setUsePooledConnection(boolean usePooledConnection) {
         if (getConfiguration() instanceof ActiveMQConfiguration) {
diff --git a/components/camel-activemq/src/main/java/org/apache/camel/component/activemq/ActiveMQConfiguration.java b/components/camel-activemq/src/main/java/org/apache/camel/component/activemq/ActiveMQConfiguration.java
index f716278..4bf3627 100644
--- a/components/camel-activemq/src/main/java/org/apache/camel/component/activemq/ActiveMQConfiguration.java
+++ b/components/camel-activemq/src/main/java/org/apache/camel/component/activemq/ActiveMQConfiguration.java
@@ -17,12 +17,14 @@
 package org.apache.camel.component.activemq;
 
 import java.lang.reflect.Constructor;
-
 import javax.jms.ConnectionFactory;
 
+import org.apache.activemq.ActiveMQConnectionFactory;
 import org.apache.activemq.Service;
-import org.apache.activemq.spring.ActiveMQConnectionFactory;
 import org.apache.camel.component.jms.JmsConfiguration;
+import org.apache.camel.spi.Metadata;
+import org.springframework.jms.connection.CachingConnectionFactory;
+import org.springframework.jms.connection.DelegatingConnectionFactory;
 import org.springframework.jms.connection.JmsTransactionManager;
 import org.springframework.jms.connection.SingleConnectionFactory;
 import org.springframework.jms.core.JmsTemplate;
@@ -34,7 +36,9 @@ import org.springframework.transaction.PlatformTransactionManager;
 public class ActiveMQConfiguration extends JmsConfiguration {
     private ActiveMQComponent activeMQComponent;
     private String brokerURL = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
+    private volatile boolean customBrokerURL;
     private boolean useSingleConnection;
+    @Metadata(defaultValue = "true")
     private boolean usePooledConnection = true;
     private boolean trustAllPackages;
 
@@ -46,14 +50,11 @@ public class ActiveMQConfiguration extends JmsConfiguration {
     }
 
     /**
-     * Sets the broker URL to use to connect to ActiveMQ using the
-     * <a href="http://activemq.apache.org/configuring-transports.html">ActiveMQ
-     * URI format</a>
-     *
-     * @param brokerURL the URL of the broker.
+     * Sets the broker URL to use to connect to ActiveMQ broker.
      */
     public void setBrokerURL(String brokerURL) {
         this.brokerURL = brokerURL;
+        this.customBrokerURL = true;
     }
 
     public boolean isUseSingleConnection() {
@@ -102,8 +103,7 @@ public class ActiveMQConfiguration extends JmsConfiguration {
      * Spring {@link JmsTemplate} which will create a new connection, session,
      * producer for each message then close them all down again.
      * <p/>
-     * The default value is true. Note that this requires an extra dependency on
-     * commons-pool2.
+     * The default value is true.
      */
     public void setUsePooledConnection(boolean usePooledConnection) {
         this.usePooledConnection = usePooledConnection;
@@ -144,8 +144,30 @@ public class ActiveMQConfiguration extends JmsConfiguration {
     }
 
     @Override
+    public void setConnectionFactory(ConnectionFactory connectionFactory) {
+        if (customBrokerURL) {
+            // okay a custom broker url was configured which we want to ensure
+            // the real target connection factory knows about
+            ConnectionFactory target = connectionFactory;
+            if (target instanceof CachingConnectionFactory) {
+                CachingConnectionFactory ccf = (CachingConnectionFactory) target;
+                target = ccf.getTargetConnectionFactory();
+            }
+            if (target instanceof DelegatingConnectionFactory) {
+                DelegatingConnectionFactory dcf = (DelegatingConnectionFactory) target;
+                target = dcf.getTargetConnectionFactory();
+            }
+            if (target instanceof ActiveMQConnectionFactory) {
+                ActiveMQConnectionFactory acf = (ActiveMQConnectionFactory) target;
+                acf.setBrokerURL(brokerURL);
+            }
+        }
+        super.setConnectionFactory(connectionFactory);
+    }
+
+    @Override
     protected ConnectionFactory createConnectionFactory() {
-        ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory();
+        org.apache.activemq.spring.ActiveMQConnectionFactory answer = new org.apache.activemq.spring.ActiveMQConnectionFactory();
         answer.setTrustAllPackages(trustAllPackages);
         if (getUsername() != null) {
             answer.setUserName(getUsername());
@@ -175,9 +197,6 @@ public class ActiveMQConfiguration extends JmsConfiguration {
     }
 
     protected ConnectionFactory createPooledConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
-        // lets not use classes directly to avoid a runtime dependency on
-        // commons-pool2
-        // for folks not using this option
         try {
             Class type = loadClass("org.apache.activemq.pool.PooledConnectionFactory", getClass().getClassLoader());
             Constructor constructor = type.getConstructor(org.apache.activemq.ActiveMQConnectionFactory.class);
diff --git a/examples/camel-example-spring-boot-activemq/pom.xml b/examples/camel-example-spring-boot-activemq/pom.xml
index 9976643..1409e38 100644
--- a/examples/camel-example-spring-boot-activemq/pom.xml
+++ b/examples/camel-example-spring-boot-activemq/pom.xml
@@ -66,16 +66,9 @@
 
     <dependencies>
 
-        <!-- activemq -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-activemq</artifactId>
-            <exclusions>
-                <exclusion>
-                    <groupId>org.apache.activemq</groupId>
-                    <artifactId>activemq-broker</artifactId>
-                </exclusion>
-            </exclusions>
+            <artifactId>spring-boot-starter</artifactId>
         </dependency>
 
         <!-- Camel -->
@@ -84,14 +77,8 @@
             <artifactId>camel-spring-boot-starter</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.apache.camel</groupId>
-            <artifactId>camel-activemq</artifactId>
-            <exclusions>
-                <exclusion>
-                    <groupId>org.apache.activemq</groupId>
-                    <artifactId>activemq-broker</artifactId>
-                </exclusion>
-            </exclusions>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-activemq-starter</artifactId>
         </dependency>
 
         <!-- test -->
diff --git a/examples/camel-example-spring-boot-activemq/readme.adoc b/examples/camel-example-spring-boot-activemq/readme.adoc
index d16d1ba..9d18362 100644
--- a/examples/camel-example-spring-boot-activemq/readme.adoc
+++ b/examples/camel-example-spring-boot-activemq/readme.adoc
@@ -20,11 +20,6 @@ You can run this example using
 
     mvn spring-boot:run
 
-=== Using Camel components
-
-Apache Camel provides 200+ components which you can use to integrate and route messages between many systems
-and data formats. To use any of these Camel components, add the component as a dependency to your project.
-
 === Help and contributions
 
 If you hit any problem using Camel or have some feedback, then please
diff --git a/examples/camel-example-spring-boot-activemq/src/main/resources/application.properties b/examples/camel-example-spring-boot-activemq/src/main/resources/application.properties
index 67aff5c..feee4eb 100644
--- a/examples/camel-example-spring-boot-activemq/src/main/resources/application.properties
+++ b/examples/camel-example-spring-boot-activemq/src/main/resources/application.properties
@@ -15,6 +15,12 @@
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
 
-#You can use this property to override the default autowired broker-url
-spring.activemq.broker-url=tcp://localhost:61616
+# to keep the JVM running
 camel.springboot.main-run-controller = true
+
+# setup camel-activemq-starter with the url of the remote broker
+# you can change the port number to 61617 and reconfigure conf/activemq.xml to use port 61617 instead of 61616
+# to try using a different port than the default
+camel.component.activemq.broker-u-r-l=tcp://localhost:61616
+
+
diff --git a/platforms/spring-boot/components-starter/camel-activemq-starter/src/main/java/org/apache/camel/component/activemq/springboot/ActiveMQComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-activemq-starter/src/main/java/org/apache/camel/component/activemq/springboot/ActiveMQComponentConfiguration.java
index 99b8815..f416dfc 100644
--- a/platforms/spring-boot/components-starter/camel-activemq-starter/src/main/java/org/apache/camel/component/activemq/springboot/ActiveMQComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-activemq-starter/src/main/java/org/apache/camel/component/activemq/springboot/ActiveMQComponentConfiguration.java
@@ -42,12 +42,14 @@ public class ActiveMQComponentConfiguration
      */
     private Boolean enabled;
     /**
-     * Sets the broker URL to use to connect to ActiveMQ using the ActiveMQ URI
-     * format
+     * Sets the broker URL to use to connect to ActiveMQ
      */
     private String brokerURL;
     /**
-     * Define if all packages are trusted or not
+     * Define if all Java packages are trusted or not (for Java object JMS
+     * message types). Notice its not recommended practice to send Java
+     * serialized objects over network. Setting this to true can expose security
+     * risks, so use this with care.
      */
     private Boolean trustAllPackages = false;
     /**
@@ -62,7 +64,7 @@ public class ActiveMQComponentConfiguration
      * consuming thread, pooling will be used rather than the default with the
      * Spring JmsTemplate which will create a new connection, session, producer
      * for each message then close them all down again. The default value is
-     * true. Note that this requires an extra dependency on commons-pool2.
+     * true.
      */
     private Boolean usePooledConnection = false;
     /**