You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2021/03/02 08:34:04 UTC

[camel] 02/10: CAMEL-16222: camel-core - ExchangeFactory SPI to allow to use exchange pooling

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

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

commit fd299dc77b8154fd1847ad1c74a1d9e6fefdbf95
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Mar 2 09:14:26 2021 +0100

    CAMEL-16222: camel-core - ExchangeFactory SPI to allow to use exchange pooling
---
 .../apache/camel/impl/engine/PooledExchangeFactory.java    |  2 +-
 ...tExchangeFactory.java => PrototypeExchangeFactory.java} | 14 +++++++-------
 .../org/apache/camel/impl/engine/SimpleCamelContext.java   |  2 +-
 .../META-INF/camel-main-configuration-metadata.json        |  2 +-
 core/camel-main/src/main/docs/main.adoc                    |  2 +-
 .../apache/camel/main/DefaultConfigurationConfigurer.java  |  3 +++
 .../apache/camel/main/DefaultConfigurationProperties.java  |  7 ++++---
 7 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PooledExchangeFactory.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PooledExchangeFactory.java
index 78b2359..4079a46 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PooledExchangeFactory.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PooledExchangeFactory.java
@@ -31,7 +31,7 @@ import org.slf4j.LoggerFactory;
 /**
  * Pooled {@link ExchangeFactory} that reuses {@link Exchange} instance from a pool.
  */
-public final class PooledExchangeFactory extends DefaultExchangeFactory {
+public final class PooledExchangeFactory extends PrototypeExchangeFactory {
 
     private static final Logger LOG = LoggerFactory.getLogger(PooledExchangeFactory.class);
 
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultExchangeFactory.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PrototypeExchangeFactory.java
similarity index 93%
rename from core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultExchangeFactory.java
rename to core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PrototypeExchangeFactory.java
index efc595b..fb17cb0 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultExchangeFactory.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PrototypeExchangeFactory.java
@@ -32,11 +32,11 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Default {@link ExchangeFactory} that creates a new {@link Exchange} instance.
+ * {@link ExchangeFactory} that creates a new {@link Exchange} instance.
  */
-public class DefaultExchangeFactory extends ServiceSupport implements ExchangeFactory {
+public class PrototypeExchangeFactory extends ServiceSupport implements ExchangeFactory {
 
-    private static final Logger LOG = LoggerFactory.getLogger(DefaultExchangeFactory.class);
+    private static final Logger LOG = LoggerFactory.getLogger(PrototypeExchangeFactory.class);
 
     final UtilizationStatistics statistics = new UtilizationStatistics();
     final Consumer consumer;
@@ -44,11 +44,11 @@ public class DefaultExchangeFactory extends ServiceSupport implements ExchangeFa
     ExchangeFactoryManager exchangeFactoryManager;
     String routeId;
 
-    public DefaultExchangeFactory() {
+    public PrototypeExchangeFactory() {
         this.consumer = null;
     }
 
-    public DefaultExchangeFactory(Consumer consumer) {
+    public PrototypeExchangeFactory(Consumer consumer) {
         this.consumer = consumer;
     }
 
@@ -84,7 +84,7 @@ public class DefaultExchangeFactory extends ServiceSupport implements ExchangeFa
 
     @Override
     public ExchangeFactory newExchangeFactory(Consumer consumer) {
-        DefaultExchangeFactory answer = new DefaultExchangeFactory(consumer);
+        PrototypeExchangeFactory answer = new PrototypeExchangeFactory(consumer);
         answer.setStatisticsEnabled(statistics.isStatisticsEnabled());
         answer.setCapacity(getCapacity());
         answer.setCamelContext(camelContext);
@@ -167,7 +167,7 @@ public class DefaultExchangeFactory extends ServiceSupport implements ExchangeFa
         if (exchangeFactoryManager != null) {
             exchangeFactoryManager.removeExchangeFactory(this);
         }
-        logUsageSummary(LOG, "DefaultExchangeFactory", 0);
+        logUsageSummary(LOG, "PrototypeExchangeFactory", 0);
         statistics.reset();
     }
 
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
index 6a0137d..2cae07b 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/SimpleCamelContext.java
@@ -552,7 +552,7 @@ public class SimpleCamelContext extends AbstractCamelContext {
                 ExchangeFactory.FACTORY,
                 ExchangeFactory.class);
 
-        return result.orElseGet(DefaultExchangeFactory::new);
+        return result.orElseGet(PrototypeExchangeFactory::new);
     }
 
     @Override
diff --git a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
index 09d115c..4e8cdc9 100644
--- a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
+++ b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
@@ -33,7 +33,7 @@
     { "name": "camel.main.endpointBridgeErrorHandler", "description": "Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN\/ERROR level and ignored. The default va [...]
     { "name": "camel.main.endpointLazyStartProducer", "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first mes [...]
     { "name": "camel.main.endpointRuntimeStatisticsEnabled", "description": "Sets whether endpoint runtime statistics is enabled (gathers runtime usage of each incoming and outgoing endpoints). The default value is false.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean" },
-    { "name": "camel.main.exchangeFactory", "description": "Controls whether to pool (reuse) exchanges or create new fresh exchanges (default). Using pooled will reduce JVM garbage collection overhead by avoiding to re-create Exchange instances per message each consumer receives.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "string", "javaType": "java.lang.String", "defaultValue": "default", "enum": [ "default", "pooled" ] },
+    { "name": "camel.main.exchangeFactory", "description": "Controls whether to pool (reuse) exchanges or create new exchanges (prototype). Using pooled will reduce JVM garbage collection overhead by avoiding to re-create Exchange instances per message each consumer receives. The default is prototype mode.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "string", "javaType": "java.lang.String", "defaultValue": "default", "enum": [ "default", "prototype", " [...]
     { "name": "camel.main.exchangeFactoryCapacity", "description": "The capacity the pool (for each consumer) uses for storing exchanges. The default capacity is 100.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "integer", "javaType": "int", "defaultValue": 100 },
     { "name": "camel.main.exchangeFactoryStatisticsEnabled", "description": "Configures whether statistics is enabled on exchange factory.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean" },
     { "name": "camel.main.fileConfigurations", "description": "Directory to load additional configuration files that contains configuration values that takes precedence over any other configuration. This can be used to refer to files that may have secret configuration that has been mounted on the file system for containers. You can specify a pattern to load from sub directories and a name pattern such as \/var\/app\/secret\/.properties, multiple directories can be separated by comma.", " [...]
diff --git a/core/camel-main/src/main/docs/main.adoc b/core/camel-main/src/main/docs/main.adoc
index eb3b736..7cfb48e 100644
--- a/core/camel-main/src/main/docs/main.adoc
+++ b/core/camel-main/src/main/docs/main.adoc
@@ -45,7 +45,7 @@ The following table lists all the options:
 | *camel.main.endpointBridgeError{zwsp}Handler* | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN/ERROR level and ignored. The default value is false. |  | boolean
 | *camel.main.endpointLazyStart{zwsp}Producer* | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed the [...]
 | *camel.main.endpointRuntime{zwsp}StatisticsEnabled* | Sets whether endpoint runtime statistics is enabled (gathers runtime usage of each incoming and outgoing endpoints). The default value is false. |  | boolean
-| *camel.main.exchangeFactory* | Controls whether to pool (reuse) exchanges or create new fresh exchanges (default). Using pooled will reduce JVM garbage collection overhead by avoiding to re-create Exchange instances per message each consumer receives. | default | String
+| *camel.main.exchangeFactory* | Controls whether to pool (reuse) exchanges or create new exchanges (prototype). Using pooled will reduce JVM garbage collection overhead by avoiding to re-create Exchange instances per message each consumer receives. The default is prototype mode. | default | String
 | *camel.main.exchangeFactory{zwsp}Capacity* | The capacity the pool (for each consumer) uses for storing exchanges. The default capacity is 100. | 100 | int
 | *camel.main.exchangeFactory{zwsp}StatisticsEnabled* | Configures whether statistics is enabled on exchange factory. |  | boolean
 | *camel.main.fileConfigurations* | Directory to load additional configuration files that contains configuration values that takes precedence over any other configuration. This can be used to refer to files that may have secret configuration that has been mounted on the file system for containers. You can specify a pattern to load from sub directories and a name pattern such as /var/app/secret/.properties, multiple directories can be separated by comma. |  | String
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
index ad72ea7..73953bb 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
@@ -32,6 +32,7 @@ import org.apache.camel.health.HealthCheckRegistry;
 import org.apache.camel.health.HealthCheckRepository;
 import org.apache.camel.impl.debugger.BacklogTracer;
 import org.apache.camel.impl.engine.PooledExchangeFactory;
+import org.apache.camel.impl.engine.PrototypeExchangeFactory;
 import org.apache.camel.model.Model;
 import org.apache.camel.model.ModelCamelContext;
 import org.apache.camel.model.ModelLifecycleStrategy;
@@ -124,6 +125,8 @@ public final class DefaultConfigurationConfigurer {
 
         if ("pooled".equals(config.getExchangeFactory())) {
             ecc.setExchangeFactory(new PooledExchangeFactory());
+        } else if ("prototype".equals(config.getExchangeFactory())) {
+            ecc.setExchangeFactory(new PrototypeExchangeFactory());
         }
         ecc.getExchangeFactory().setStatisticsEnabled(config.isExchangeFactoryStatisticsEnabled());
         ecc.getExchangeFactory().setCapacity(config.getExchangeFactoryCapacity());
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
index 1707928..34957b0 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
@@ -87,7 +87,7 @@ public abstract class DefaultConfigurationProperties<T> {
     private String routesIncludePattern = "classpath:camel/*.xml,classpath:camel-template/*.xml,classpath:camel-rest/*.xml";
     private String routesExcludePattern;
     private boolean lightweight;
-    @Metadata(defaultValue = "default", enums = "default,pooled")
+    @Metadata(defaultValue = "default", enums = "default,prototype,pooled")
     private String exchangeFactory = "default";
     private int exchangeFactoryCapacity = 100;
     private boolean exchangeFactoryStatisticsEnabled;
@@ -932,8 +932,9 @@ public abstract class DefaultConfigurationProperties<T> {
     }
 
     /**
-     * Controls whether to pool (reuse) exchanges or create new fresh exchanges (default). Using pooled will reduce JVM
-     * garbage collection overhead by avoiding to re-create Exchange instances per message each consumer receives.
+     * Controls whether to pool (reuse) exchanges or create new exchanges (prototype). Using pooled will reduce JVM
+     * garbage collection overhead by avoiding to re-create Exchange instances per message each consumer receives. The
+     * default is prototype mode.
      */
     public void setExchangeFactory(String exchangeFactory) {
         this.exchangeFactory = exchangeFactory;