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 2021/01/23 11:12:25 UTC

[camel] 04/09: CAMEL-15844: Camel components creating consumer should not do init/start logic in their constructors.

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

commit 6d39724f34f68da07ad4a705e45a189e3f32aeff
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Jan 22 16:57:23 2021 +0100

    CAMEL-15844: Camel components creating consumer should not do init/start logic in their constructors.
---
 .../camel/component/ehcache/EhcacheConsumer.java   | 24 +++++++++++++---------
 .../guava/eventbus/GuavaEventBusEndpoint.java      |  4 ++--
 2 files changed, 16 insertions(+), 12 deletions(-)

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 b7551c5..7bf5f70 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
@@ -26,15 +26,24 @@ import org.ehcache.event.CacheEventListener;
 
 public class EhcacheConsumer extends DefaultConsumer implements CacheEventListener<Object, Object> {
     private final EhcacheConfiguration configuration;
-    private final EhcacheManager manager;
-    private final Cache cache;
+    private final String cacheName;
+    private Cache cache;
 
     public EhcacheConsumer(EhcacheEndpoint endpoint, String cacheName, EhcacheConfiguration configuration,
                            Processor processor) throws Exception {
         super(endpoint, processor);
-
         this.configuration = configuration;
-        this.manager = endpoint.getManager();
+        this.cacheName = cacheName;
+    }
+
+    @Override
+    public EhcacheEndpoint getEndpoint() {
+        return (EhcacheEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
 
         Class<?> kt = null;
         if (configuration.getKeyType() != null) {
@@ -44,12 +53,7 @@ public class EhcacheConsumer extends DefaultConsumer implements CacheEventListen
         if (configuration.getValueType() != null) {
             vt = getEndpoint().getCamelContext().getClassResolver().resolveClass(configuration.getValueType());
         }
-        this.cache = manager.getCache(cacheName, kt, vt);
-    }
-
-    @Override
-    protected void doStart() throws Exception {
-        super.doStart();
+        this.cache = getEndpoint().getManager().getCache(cacheName, kt, vt);
 
         this.cache.getRuntimeConfiguration().registerCacheEventListener(
                 this,
diff --git a/components/camel-guava-eventbus/src/main/java/org/apache/camel/component/guava/eventbus/GuavaEventBusEndpoint.java b/components/camel-guava-eventbus/src/main/java/org/apache/camel/component/guava/eventbus/GuavaEventBusEndpoint.java
index f8b0619..d246eb6 100644
--- a/components/camel-guava-eventbus/src/main/java/org/apache/camel/component/guava/eventbus/GuavaEventBusEndpoint.java
+++ b/components/camel-guava-eventbus/src/main/java/org/apache/camel/component/guava/eventbus/GuavaEventBusEndpoint.java
@@ -125,8 +125,8 @@ public class GuavaEventBusEndpoint extends DefaultEndpoint implements MultipleCo
     }
 
     @Override
-    protected void doStart() throws Exception {
-        super.doStart();
+    protected void doInit() throws Exception {
+        super.doInit();
 
         if (eventBusRef != null && eventBus == null) {
             eventBus = CamelContextHelper.mandatoryLookup(getCamelContext(), eventBusRef, EventBus.class);