Repository: camel
Updated Branches:
  refs/heads/master 05cbb33c0 -> 351ee03be


CAMEL-10912: camel-sjms - default pool should test connection before use so its 
valid. Expose options to configure pool on component level to make this easier.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/351ee03b
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/351ee03b
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/351ee03b

Branch: refs/heads/master
Commit: 351ee03be0a93aed7130cb997f8c90e04f550a88
Parents: 05cbb33
Author: Claus Ibsen <davscl...@apache.org>
Authored: Tue Mar 7 14:42:27 2017 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Tue Mar 7 14:42:35 2017 +0100

----------------------------------------------------------------------
 .../src/main/docs/sjms-component.adoc           |  7 +-
 .../camel/component/sjms/SjmsComponent.java     | 66 +++++++++++++++++++
 .../camel/component/sjms/SjmsEndpoint.java      |  4 +-
 .../sjms/jms/ConnectionFactoryResource.java     | 20 ++++++
 .../springboot/SjmsComponentConfiguration.java  | 68 ++++++++++++++++++++
 5 files changed, 163 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/351ee03b/components/camel-sjms/src/main/docs/sjms-component.adoc
----------------------------------------------------------------------
diff --git a/components/camel-sjms/src/main/docs/sjms-component.adoc 
b/components/camel-sjms/src/main/docs/sjms-component.adoc
index 83ff13d..ee8093c 100644
--- a/components/camel-sjms/src/main/docs/sjms-component.adoc
+++ b/components/camel-sjms/src/main/docs/sjms-component.adoc
@@ -83,7 +83,7 @@ You append query options to the URI using the following 
format,
 
 
 // component options: START
-The Simple JMS component supports 10 options which are listed below.
+The Simple JMS component supports 15 options which are listed below.
 
 
 
@@ -98,6 +98,11 @@ The Simple JMS component supports 10 options which are 
listed below.
 | destinationCreationStrategy | advanced |  | DestinationCreationStrategy | To 
use a custom DestinationCreationStrategy.
 | timedTaskManager | advanced |  | TimedTaskManager | To use a custom 
TimedTaskManager
 | messageCreatedStrategy | advanced |  | MessageCreatedStrategy | To use the 
given MessageCreatedStrategy which are invoked when Camel creates new instances 
of javax.jms.Message objects when Camel is sending a JMS message.
+| connectionTestOnBorrow | advanced | true | boolean | When using the default 
org.apache.camel.component.sjms.jms.ConnectionFactoryResource then should each 
javax.jms.Connection be tested (calling start) before returned from the pool.
+| connectionUsername | security |  | String | The username to use when 
creating javax.jms.Connection when using the default 
org.apache.camel.component.sjms.jms.ConnectionFactoryResource.
+| connectionPassword | security |  | String | The password to use when 
creating javax.jms.Connection when using the default 
org.apache.camel.component.sjms.jms.ConnectionFactoryResource.
+| connectionClientId | advanced |  | String | The client ID to use when 
creating javax.jms.Connection when using the default 
org.apache.camel.component.sjms.jms.ConnectionFactoryResource.
+| connectionMaxWait | advanced | 5000 | long | The max wait time in millis to 
block and wait on free connection when the pool is exhausted when using the 
default org.apache.camel.component.sjms.jms.ConnectionFactoryResource.
 | headerFilterStrategy | filter |  | HeaderFilterStrategy | To use a custom 
org.apache.camel.spi.HeaderFilterStrategy to filter header to and from Camel 
message.
 | resolvePropertyPlaceholders | advanced | true | boolean | Whether the 
component should resolve property placeholders on itself when starting. Only 
properties which are of String type can use property placeholders.
 |=======================================================================

http://git-wip-us.apache.org/repos/asf/camel/blob/351ee03b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsComponent.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsComponent.java
 
b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsComponent.java
index 01918a1..1fe6511 100644
--- 
a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsComponent.java
+++ 
b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsComponent.java
@@ -55,6 +55,16 @@ public class SjmsComponent extends 
HeaderFilterStrategyComponent {
     private DestinationCreationStrategy destinationCreationStrategy;
     @Metadata(label = "advanced")
     private MessageCreatedStrategy messageCreatedStrategy;
+    @Metadata(label = "advanced", defaultValue = "true")
+    private boolean connectionTestOnBorrow = true;
+    @Metadata(label = "security", secret = true)
+    private String connectionUsername;
+    @Metadata(label = "security", secret = true)
+    private String connectionPassword;
+    @Metadata(label = "advanced")
+    private String connectionClientId;
+    @Metadata(label = "advanced", defaultValue = "5000")
+    private long connectionMaxWait = 5000;
 
     public SjmsComponent() {
         super(SjmsEndpoint.class);
@@ -235,4 +245,60 @@ public class SjmsComponent extends 
HeaderFilterStrategyComponent {
         this.messageCreatedStrategy = messageCreatedStrategy;
     }
 
+    public boolean isConnectionTestOnBorrow() {
+        return connectionTestOnBorrow;
+    }
+
+    /**
+     * When using the default {@link 
org.apache.camel.component.sjms.jms.ConnectionFactoryResource} then should each 
{@link javax.jms.Connection}
+     * be tested (calling start) before returned from the pool.
+     */
+    public void setConnectionTestOnBorrow(boolean connectionTestOnBorrow) {
+        this.connectionTestOnBorrow = connectionTestOnBorrow;
+    }
+
+    public String getConnectionUsername() {
+        return connectionUsername;
+    }
+
+    /**
+     * The username to use when creating {@link javax.jms.Connection} when 
using the default {@link 
org.apache.camel.component.sjms.jms.ConnectionFactoryResource}.
+     */
+    public void setConnectionUsername(String connectionUsername) {
+        this.connectionUsername = connectionUsername;
+    }
+
+    public String getConnectionPassword() {
+        return connectionPassword;
+    }
+
+    /**
+     * The password to use when creating {@link javax.jms.Connection} when 
using the default {@link 
org.apache.camel.component.sjms.jms.ConnectionFactoryResource}.
+     */
+    public void setConnectionPassword(String connectionPassword) {
+        this.connectionPassword = connectionPassword;
+    }
+
+    public String getConnectionClientId() {
+        return connectionClientId;
+    }
+
+    /**
+     * The client ID to use when creating {@link javax.jms.Connection} when 
using the default {@link 
org.apache.camel.component.sjms.jms.ConnectionFactoryResource}.
+     */
+    public void setConnectionClientId(String connectionClientId) {
+        this.connectionClientId = connectionClientId;
+    }
+
+    public long getConnectionMaxWait() {
+        return connectionMaxWait;
+    }
+
+    /**
+     * The max wait time in millis to block and wait on free connection when 
the pool is exhausted
+     * when using the default {@link 
org.apache.camel.component.sjms.jms.ConnectionFactoryResource}.
+     */
+    public void setConnectionMaxWait(long connectionMaxWait) {
+        this.connectionMaxWait = connectionMaxWait;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/351ee03b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsEndpoint.java
 
b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsEndpoint.java
index ff3d014..26a599a 100644
--- 
a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsEndpoint.java
+++ 
b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/SjmsEndpoint.java
@@ -220,7 +220,9 @@ public class SjmsEndpoint extends DefaultEndpoint 
implements AsyncEndpoint, Mult
         try {
             logger.debug("Creating ConnectionResource with connectionCount: {} 
using ConnectionFactory", getConnectionCount(), getConnectionFactory());
             // We always use a connection pool, even for a pool of 1
-            ConnectionFactoryResource connections = new 
ConnectionFactoryResource(getConnectionCount(), getConnectionFactory());
+            ConnectionFactoryResource connections = new 
ConnectionFactoryResource(getConnectionCount(), getConnectionFactory(),
+                getComponent().getConnectionUsername(), 
getComponent().getConnectionPassword(), getComponent().getConnectionClientId(),
+                getComponent().getConnectionMaxWait(), 
getComponent().isConnectionTestOnBorrow());
             if (exceptionListener != null) {
                 connections.setExceptionListener(exceptionListener);
             } else {

http://git-wip-us.apache.org/repos/asf/camel/blob/351ee03b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/jms/ConnectionFactoryResource.java
----------------------------------------------------------------------
diff --git 
a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/jms/ConnectionFactoryResource.java
 
b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/jms/ConnectionFactoryResource.java
index be4159b..9f4ade1 100644
--- 
a/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/jms/ConnectionFactoryResource.java
+++ 
b/components/camel-sjms/src/main/java/org/apache/camel/component/sjms/jms/ConnectionFactoryResource.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.sjms.jms;
 import javax.jms.Connection;
 import javax.jms.ConnectionFactory;
 import javax.jms.ExceptionListener;
+import javax.jms.Session;
 
 import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.pool.BasePoolableObjectFactory;
@@ -58,6 +59,11 @@ public class ConnectionFactoryResource extends 
BasePoolableObjectFactory<Connect
     }
 
     public ConnectionFactoryResource(int poolSize, ConnectionFactory 
connectionFactory, String username, String password, String connectionId, long 
maxWait) {
+        this(poolSize, connectionFactory, username, password, connectionId, 
DEFAULT_WAIT_TIMEOUT, true);
+    }
+
+    public ConnectionFactoryResource(int poolSize, ConnectionFactory 
connectionFactory, String username, String password, String connectionId,
+                                     long maxWait, boolean testOnBorrow) {
         this.connectionFactory = connectionFactory;
         this.username = username;
         this.password = password;
@@ -68,6 +74,20 @@ public class ConnectionFactoryResource extends 
BasePoolableObjectFactory<Connect
         this.connections.setMaxIdle(poolSize);
         this.connections.setMinIdle(poolSize);
         this.connections.setLifo(false);
+        this.connections.setTestOnBorrow(testOnBorrow);
+    }
+
+    @Override
+    public boolean validateObject(Connection connection) {
+        try {
+            // ensure connection works so we need to start it
+            connection.start();
+            return true;
+        } catch (Throwable e) {
+            // ignore
+        }
+
+        return false;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/351ee03b/platforms/spring-boot/components-starter/camel-sjms-starter/src/main/java/org/apache/camel/component/sjms/springboot/SjmsComponentConfiguration.java
----------------------------------------------------------------------
diff --git 
a/platforms/spring-boot/components-starter/camel-sjms-starter/src/main/java/org/apache/camel/component/sjms/springboot/SjmsComponentConfiguration.java
 
b/platforms/spring-boot/components-starter/camel-sjms-starter/src/main/java/org/apache/camel/component/sjms/springboot/SjmsComponentConfiguration.java
index 5885f67..20e3401 100644
--- 
a/platforms/spring-boot/components-starter/camel-sjms-starter/src/main/java/org/apache/camel/component/sjms/springboot/SjmsComponentConfiguration.java
+++ 
b/platforms/spring-boot/components-starter/camel-sjms-starter/src/main/java/org/apache/camel/component/sjms/springboot/SjmsComponentConfiguration.java
@@ -87,6 +87,34 @@ public class SjmsComponentConfiguration {
     @NestedConfigurationProperty
     private MessageCreatedStrategy messageCreatedStrategy;
     /**
+     * When using the default
+     * org.apache.camel.component.sjms.jms.ConnectionFactoryResource then 
should
+     * each javax.jms.Connection be tested (calling start) before returned from
+     * the pool.
+     */
+    private Boolean connectionTestOnBorrow = true;
+    /**
+     * The username to use when creating javax.jms.Connection when using the
+     * default org.apache.camel.component.sjms.jms.ConnectionFactoryResource.
+     */
+    private String connectionUsername;
+    /**
+     * The password to use when creating javax.jms.Connection when using the
+     * default org.apache.camel.component.sjms.jms.ConnectionFactoryResource.
+     */
+    private String connectionPassword;
+    /**
+     * The client ID to use when creating javax.jms.Connection when using the
+     * default org.apache.camel.component.sjms.jms.ConnectionFactoryResource.
+     */
+    private String connectionClientId;
+    /**
+     * The max wait time in millis to block and wait on free connection when 
the
+     * pool is exhausted when using the default
+     * org.apache.camel.component.sjms.jms.ConnectionFactoryResource.
+     */
+    private Long connectionMaxWait = 5000L;
+    /**
      * To use a custom org.apache.camel.spi.HeaderFilterStrategy to filter
      * header to and from Camel message.
      */
@@ -168,6 +196,46 @@ public class SjmsComponentConfiguration {
         this.messageCreatedStrategy = messageCreatedStrategy;
     }
 
+    public Boolean getConnectionTestOnBorrow() {
+        return connectionTestOnBorrow;
+    }
+
+    public void setConnectionTestOnBorrow(Boolean connectionTestOnBorrow) {
+        this.connectionTestOnBorrow = connectionTestOnBorrow;
+    }
+
+    public String getConnectionUsername() {
+        return connectionUsername;
+    }
+
+    public void setConnectionUsername(String connectionUsername) {
+        this.connectionUsername = connectionUsername;
+    }
+
+    public String getConnectionPassword() {
+        return connectionPassword;
+    }
+
+    public void setConnectionPassword(String connectionPassword) {
+        this.connectionPassword = connectionPassword;
+    }
+
+    public String getConnectionClientId() {
+        return connectionClientId;
+    }
+
+    public void setConnectionClientId(String connectionClientId) {
+        this.connectionClientId = connectionClientId;
+    }
+
+    public Long getConnectionMaxWait() {
+        return connectionMaxWait;
+    }
+
+    public void setConnectionMaxWait(Long connectionMaxWait) {
+        this.connectionMaxWait = connectionMaxWait;
+    }
+
     public HeaderFilterStrategy getHeaderFilterStrategy() {
         return headerFilterStrategy;
     }

Reply via email to