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/12/11 08:13:06 UTC

[camel] branch master updated: CAMEL-14284: Configuring endpoint should set properties on endpoint and not configuration object - EHCache component

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 684fe1e  CAMEL-14284: Configuring endpoint should set properties on endpoint and not configuration object - EHCache component
684fe1e is described below

commit 684fe1e957b7e62f6033ddf64f64562c2cf014b2
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 11 09:12:47 2019 +0100

    CAMEL-14284: Configuring endpoint should set properties on endpoint and not configuration object - EHCache component
---
 .../src/main/docs/ehcache-component.adoc           |  2 +-
 .../camel/component/ehcache/EhcacheComponent.java  |  7 +++--
 .../component/ehcache/EhcacheConfiguration.java    | 36 +++++++++++++---------
 .../camel/component/ehcache/EhcacheConsumer.java   |  2 +-
 .../camel/component/ehcache/EhcacheEndpoint.java   | 28 +++++++++++++----
 .../component/ehcache/EhcacheManagerTest.java      |  8 ++---
 .../dsl/EhcacheEndpointBuilderFactory.java         | 32 +++----------------
 .../springboot/EhcacheComponentConfiguration.java  |  9 +++---
 8 files changed, 62 insertions(+), 62 deletions(-)

diff --git a/components/camel-ehcache/src/main/docs/ehcache-component.adoc b/components/camel-ehcache/src/main/docs/ehcache-component.adoc
index 04f8e08..efb73b9 100644
--- a/components/camel-ehcache/src/main/docs/ehcache-component.adoc
+++ b/components/camel-ehcache/src/main/docs/ehcache-component.adoc
@@ -95,7 +95,7 @@ with the following path and query parameters:
 | *bridgeErrorHandler* (consumer) | 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 or ERROR level and ignored. | false | boolean
 | *eventFiring* (consumer) | Set the delivery mode (synchronous, asynchronous) | ASYNCHRONOUS | EventFiring
 | *eventOrdering* (consumer) | Set the delivery mode (ordered, unordered) | ORDERED | EventOrdering
-| *eventTypes* (consumer) | Set the type of events to listen for | EVICTED,EXPIRED,REMOVED,CREATED,UPDATED | Set
+| *eventTypes* (consumer) | Set the type of events to listen for (EVICTED,EXPIRED,REMOVED,CREATED,UPDATED). You can specify multiple entries separated by comma. |  | String
 | *exceptionHandler* (consumer) | To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. |  | ExceptionHandler
 | *exchangePattern* (consumer) | Sets the exchange pattern when the consumer creates an exchange. |  | ExchangePattern
 | *action* (producer) | To configure the default cache action. If an action is set in the message header, then the operation from the header takes precedence. |  | String
diff --git a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
index f2b8343..2c8c1aa 100644
--- a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
+++ b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
@@ -60,16 +60,17 @@ public class EhcacheComponent extends DefaultComponent {
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
         EhcacheConfiguration configuration = this.configuration.copy();
-        setProperties(configuration, parameters);
 
-        return new EhcacheEndpoint(uri, this, remaining, createCacheManager(configuration), configuration);
+        EhcacheEndpoint endpoint = new EhcacheEndpoint(uri, this, remaining, configuration);
+        setProperties(endpoint, parameters);
+        return endpoint;
     }
 
     // ****************************
     // Helpers
     // ****************************
 
-    private EhcacheManager createCacheManager(EhcacheConfiguration configuration) throws IOException {
+    public EhcacheManager createCacheManager(EhcacheConfiguration configuration) throws IOException {
         ObjectHelper.notNull(configuration, "Camel Ehcache configuration");
 
         // Check if a cache manager has been configured
diff --git a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConfiguration.java b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConfiguration.java
index df368a8..ba9f9f0 100644
--- a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConfiguration.java
+++ b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConfiguration.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.ehcache;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Set;
 
@@ -61,8 +62,8 @@ public class EhcacheConfiguration implements Cloneable {
     private EventOrdering eventOrdering = EventOrdering.ORDERED;
     @UriParam(label = "consumer", defaultValue = "ASYNCHRONOUS")
     private EventFiring eventFiring = EventFiring.ASYNCHRONOUS;
-    @UriParam(label = "consumer", enums = "EVICTED,EXPIRED,REMOVED,CREATED,UPDATED", defaultValue = "EVICTED,EXPIRED,REMOVED,CREATED,UPDATED")
-    private Set<EventType> eventTypes = EnumSet.of(EventType.values()[0], EventType.values());
+    @UriParam(label = "consumer", enums = "EVICTED,EXPIRED,REMOVED,CREATED,UPDATED")
+    private String eventTypes;
 
     public EhcacheConfiguration() {
     }
@@ -197,25 +198,30 @@ public class EhcacheConfiguration implements Cloneable {
         this.eventFiring = eventFiring;
     }
 
-    public Set<EventType> getEventTypes() {
+    public String getEventTypes() {
         return eventTypes;
     }
 
-    /**
-     * Set the type of events to listen for
-     */
-    public void setEventTypes(String eventTypesString) {
-        Set<EventType> eventTypes = new HashSet<>();
-        String[] events = eventTypesString.split(",");
-        for (String event : events) {
-            eventTypes.add(EventType.valueOf(event));
-        }
+    public Set<EventType> getEventTypesSet() {
+        Set<EventType> answer = new LinkedHashSet<>();
 
-        setEventTypes(eventTypes);
+        String types = eventTypes;
+        if (types == null) {
+            types = "EVICTED,EXPIRED,REMOVED,CREATED,UPDATED";
+        }
+        String[] arr = types.split(",");
+        for (String s : arr) {
+            answer.add(EventType.valueOf(s));
+        }
+        return answer;
     }
 
-    public void setEventTypes(Set<EventType> eventTypes) {
-        this.eventTypes = new HashSet<>(eventTypes);
+    /**
+     * Set the type of events to listen for (EVICTED,EXPIRED,REMOVED,CREATED,UPDATED).
+     * You can specify multiple entries separated by comma.
+     */
+    public void setEventTypes(String eventTypes) {
+        this.eventTypes = eventTypes;
     }
 
     // ****************************
diff --git a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConsumer.java b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConsumer.java
index a6263c4..7431db3 100644
--- a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConsumer.java
+++ b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConsumer.java
@@ -54,7 +54,7 @@ public class EhcacheConsumer extends DefaultConsumer implements CacheEventListen
             this,
             configuration.getEventOrdering(),
             configuration.getEventFiring(),
-            configuration.getEventTypes()
+            configuration.getEventTypesSet()
         );
     }
 
diff --git a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
index b5816a9..9c314bd 100644
--- a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
+++ b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
@@ -24,25 +24,28 @@ import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriPath;
 import org.apache.camel.support.DefaultEndpoint;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * The ehcache component enables you to perform caching operations using <a href="http://www.ehcache.org">Ehcache</a> as cache implementation.
  */
 @UriEndpoint(firstVersion = "2.18.0", scheme = "ehcache", title = "Ehcache", syntax = "ehcache:cacheName", label = "cache,datagrid,clustering")
 public class EhcacheEndpoint extends DefaultEndpoint {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(EhcacheComponent.class);
+
     @UriPath(description = "the cache name")
     @Metadata(required = true)
     private final String cacheName;
     @UriParam
     private final EhcacheConfiguration configuration;
-    private final EhcacheManager cacheManager;
+    private EhcacheManager cacheManager;
 
-    EhcacheEndpoint(String uri, EhcacheComponent component,  String cacheName, EhcacheManager cacheManager, EhcacheConfiguration configuration) throws Exception {
+    EhcacheEndpoint(String uri, EhcacheComponent component,  String cacheName, EhcacheConfiguration configuration) throws Exception {
         super(uri, component);
-
         this.cacheName = cacheName;
         this.configuration = configuration;
-        this.cacheManager = cacheManager;
     }
 
     @Override
@@ -52,11 +55,21 @@ public class EhcacheEndpoint extends DefaultEndpoint {
 
     @Override
     public Consumer createConsumer(Processor processor) throws Exception {
-        return new EhcacheConsumer(this, this.cacheName, configuration, processor);
+        EhcacheConsumer consumer = new EhcacheConsumer(this, this.cacheName, configuration, processor);
+        configureConsumer(consumer);
+        return consumer;
+    }
+
+    @Override
+    public EhcacheComponent getComponent() {
+        return (EhcacheComponent) super.getComponent();
     }
 
     @Override
     protected void doStart() throws Exception {
+        if (cacheManager == null) {
+            cacheManager = getComponent().createCacheManager(configuration);
+        }
         cacheManager.start();
         super.doStart();
     }
@@ -64,7 +77,9 @@ public class EhcacheEndpoint extends DefaultEndpoint {
     @Override
     protected void doStop() throws Exception {
         super.doStop();
-        cacheManager.stop();
+        if (cacheManager != null) {
+            cacheManager.stop();
+        }
     }
 
     EhcacheManager getManager() {
@@ -74,4 +89,5 @@ public class EhcacheEndpoint extends DefaultEndpoint {
     EhcacheConfiguration getConfiguration() {
         return configuration;
     }
+
 }
diff --git a/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheManagerTest.java b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheManagerTest.java
index 483ac61..902ec23 100644
--- a/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheManagerTest.java
+++ b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheManagerTest.java
@@ -136,15 +136,15 @@ public class EhcacheManagerTest {
             EhcacheEndpoint e1 = context.getEndpoint("ehcache:myCache1?cacheManager=#myManager", EhcacheEndpoint.class);
             EhcacheEndpoint e2 = context.getEndpoint("ehcache:myCache2?cacheManager=#myManager", EhcacheEndpoint.class);
 
-            Assert.assertEquals(e1.getManager(), e2.getManager());
-            Assert.assertEquals(e1.getManager().getCacheManager(), e2.getManager().getCacheManager());
+            Assert.assertSame(e1.getManager(), e2.getManager());
+            Assert.assertSame(e1.getManager().getCacheManager(), e2.getManager().getCacheManager());
             Assert.assertEquals(2, e1.getManager().getReferenceCount().get());
             Assert.assertEquals(Status.AVAILABLE, e1.getManager().getCacheManager().getStatus());
 
             context.stop();
 
-            Assert.assertEquals(e1.getManager(), e2.getManager());
-            Assert.assertEquals(e1.getManager().getCacheManager(), e2.getManager().getCacheManager());
+            Assert.assertSame(e1.getManager(), e2.getManager());
+            Assert.assertSame(e1.getManager().getCacheManager(), e2.getManager().getCacheManager());
             Assert.assertEquals(0, e1.getManager().getReferenceCount().get());
             Assert.assertEquals(Status.AVAILABLE, e1.getManager().getCacheManager().getStatus());
 
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/EhcacheEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/EhcacheEndpointBuilderFactory.java
index 5d2e152..91aec1d 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/EhcacheEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/EhcacheEndpointBuilderFactory.java
@@ -17,7 +17,6 @@
 package org.apache.camel.builder.endpoint.dsl;
 
 import java.util.Map;
-import java.util.Set;
 import javax.annotation.Generated;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.builder.EndpointConsumerBuilder;
@@ -217,23 +216,11 @@ public interface EhcacheEndpointBuilderFactory {
             return this;
         }
         /**
-         * Set the type of events to listen for.
+         * Set the type of events to listen for
+         * (EVICTED,EXPIRED,REMOVED,CREATED,UPDATED). You can specify multiple
+         * entries separated by comma.
          * 
-         * The option is a:
-         * <code>java.util.Set&lt;org.ehcache.event.EventType&gt;</code> type.
-         * 
-         * Group: consumer
-         */
-        default EhcacheEndpointConsumerBuilder eventTypes(
-                Set<EventType> eventTypes) {
-            doSetProperty("eventTypes", eventTypes);
-            return this;
-        }
-        /**
-         * Set the type of events to listen for.
-         * 
-         * The option will be converted to a
-         * <code>java.util.Set&lt;org.ehcache.event.EventType&gt;</code> type.
+         * The option is a: <code>java.lang.String</code> type.
          * 
          * Group: consumer
          */
@@ -1006,17 +993,6 @@ public interface EhcacheEndpointBuilderFactory {
         UNORDERED,
         ORDERED;
     }
-
-    /**
-     * Proxy enum for <code>org.ehcache.event.EventType</code> enum.
-     */
-    enum EventType {
-        EVICTED,
-        EXPIRED,
-        REMOVED,
-        CREATED,
-        UPDATED;
-    }
     /**
      * Ehcache (camel-ehcache)
      * The ehcache component enables you to perform caching operations using
diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentConfiguration.java
index 455e006..f4d3a6c 100644
--- a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentConfiguration.java
@@ -17,7 +17,6 @@
 package org.apache.camel.component.ehcache.springboot;
 
 import java.util.Map;
-import java.util.Set;
 import javax.annotation.Generated;
 import org.apache.camel.spring.boot.ComponentConfigurationPropertiesCommon;
 import org.ehcache.CacheManager;
@@ -215,8 +214,10 @@ public class EhcacheComponentConfiguration
         private EventFiring eventFiring = EventFiring.ASYNCHRONOUS;
         /**
          * Set the type of events to listen for
+         * (EVICTED,EXPIRED,REMOVED,CREATED,UPDATED). You can specify multiple
+         * entries separated by comma.
          */
-        private Set eventTypes;
+        private String eventTypes;
         /**
          * The default cache configuration to be used to create caches.
          */
@@ -310,11 +311,11 @@ public class EhcacheComponentConfiguration
             this.eventFiring = eventFiring;
         }
 
-        public Set getEventTypes() {
+        public String getEventTypes() {
             return eventTypes;
         }
 
-        public void setEventTypes(Set eventTypes) {
+        public void setEventTypes(String eventTypes) {
             this.eventTypes = eventTypes;
         }