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 2023/05/18 09:27:44 UTC

[camel] branch camel-3.x updated: CAMEL-19368: camel-core - Auto detect EventNotifer from registry to register on startup. Allow camel-jbang to have custom event notifier as well that can be reloaded.

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

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


The following commit(s) were added to refs/heads/camel-3.x by this push:
     new e64b9513db7 CAMEL-19368: camel-core - Auto detect EventNotifer from registry to register on startup. Allow camel-jbang to have custom event notifier as well that can be reloaded.
e64b9513db7 is described below

commit e64b9513db7c08f7e279faa46d1a2e2212f2e1b1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu May 18 11:27:06 2023 +0200

    CAMEL-19368: camel-core - Auto detect EventNotifer from registry to register on startup. Allow camel-jbang to have custom event notifier as well that can be reloaded.
---
 .../impl/engine/DefaultManagementStrategy.java     |  2 ++
 .../engine/DefaultRuntimeEndpointRegistry.java     | 18 ++----------
 .../camel/main/DefaultConfigurationConfigurer.java |  4 +++
 .../camel/management/PublishEventNotifier.java     | 17 ++---------
 .../apache/camel/support/EventNotifierSupport.java | 16 ++++++++++-
 .../injection/AnnotationDependencyInjection.java   | 33 ++++++++++++++++++++++
 6 files changed, 59 insertions(+), 31 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultManagementStrategy.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultManagementStrategy.java
index 9740118aa09..62e20bf7d7e 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultManagementStrategy.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultManagementStrategy.java
@@ -87,6 +87,8 @@ public class DefaultManagementStrategy extends ServiceSupport implements Managem
             this.startedEventNotifiers.add(eventNotifier);
         }
         if (getCamelContext() != null) {
+            // inject camel context if needed
+            CamelContextAware.trySetCamelContext(eventNotifier, getCamelContext());
             // okay we have an event notifier that accepts exchange events so its applicable
             if (!eventNotifier.isIgnoreExchangeEvents()) {
                 getCamelContext().adapt(ExtendedCamelContext.class).setEventNotificationApplicable(true);
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRuntimeEndpointRegistry.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRuntimeEndpointRegistry.java
index 2483b6bd034..516b9c7b9ea 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRuntimeEndpointRegistry.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRuntimeEndpointRegistry.java
@@ -24,8 +24,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.camel.CamelContext;
-import org.apache.camel.CamelContextAware;
 import org.apache.camel.Endpoint;
 import org.apache.camel.spi.CamelEvent;
 import org.apache.camel.spi.CamelEvent.ExchangeCreatedEvent;
@@ -43,12 +41,10 @@ import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class DefaultRuntimeEndpointRegistry extends EventNotifierSupport implements CamelContextAware, RuntimeEndpointRegistry {
+public class DefaultRuntimeEndpointRegistry extends EventNotifierSupport implements RuntimeEndpointRegistry {
 
     private static final Logger LOG = LoggerFactory.getLogger(DefaultRuntimeEndpointRegistry.class);
 
-    private CamelContext camelContext;
-
     // route id -> endpoint urls
     private Map<String, Set<String>> inputs;
     private Map<String, Map<String, String>> outputs;
@@ -58,16 +54,6 @@ public class DefaultRuntimeEndpointRegistry extends EventNotifierSupport impleme
     private EndpointUtilizationStatistics inputUtilization;
     private EndpointUtilizationStatistics outputUtilization;
 
-    @Override
-    public CamelContext getCamelContext() {
-        return camelContext;
-    }
-
-    @Override
-    public void setCamelContext(CamelContext camelContext) {
-        this.camelContext = camelContext;
-    }
-
     @Override
     public boolean isEnabled() {
         return enabled;
@@ -188,7 +174,7 @@ public class DefaultRuntimeEndpointRegistry extends EventNotifierSupport impleme
 
     @Override
     protected void doInit() throws Exception {
-        ObjectHelper.notNull(camelContext, "camelContext", this);
+        ObjectHelper.notNull(getCamelContext(), "camelContext", this);
 
         if (inputs == null) {
             inputs = new HashMap<>();
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 f3470ddc7d2..eff98206d8d 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
@@ -468,6 +468,10 @@ public final class DefaultConfigurationConfigurer {
         if (!tcs.isEmpty()) {
             tcs.forEach(t -> camelContext.getTypeConverterRegistry().addTypeConverters(t));
         }
+        Set<EventNotifier> ens = registry.findByType(EventNotifier.class);
+        if (!ens.isEmpty()) {
+            ens.forEach(n -> camelContext.getManagementStrategy().addEventNotifier(n));
+        }
         Set<EndpointStrategy> ess = registry.findByType(EndpointStrategy.class);
         if (!ess.isEmpty()) {
             ess.forEach(ecc::registerEndpointCallback);
diff --git a/core/camel-management/src/main/java/org/apache/camel/management/PublishEventNotifier.java b/core/camel-management/src/main/java/org/apache/camel/management/PublishEventNotifier.java
index 93489442f5e..c4818efdcfa 100644
--- a/core/camel-management/src/main/java/org/apache/camel/management/PublishEventNotifier.java
+++ b/core/camel-management/src/main/java/org/apache/camel/management/PublishEventNotifier.java
@@ -44,7 +44,6 @@ public class PublishEventNotifier extends EventNotifierSupport implements CamelC
 
     private static final Logger LOG = LoggerFactory.getLogger(PublishEventNotifier.class);
 
-    private CamelContext camelContext;
     private Endpoint endpoint;
     private String endpointUri;
     private Producer producer;
@@ -58,7 +57,7 @@ public class PublishEventNotifier extends EventNotifierSupport implements CamelC
         }
 
         // only notify when camel context is running
-        if (!camelContext.getStatus().isStarted()) {
+        if (!getCamelContext().getStatus().isStarted()) {
             LOG.debug("Cannot publish event as CamelContext is not started: {}", event);
             return;
         }
@@ -83,16 +82,6 @@ public class PublishEventNotifier extends EventNotifierSupport implements CamelC
         return true;
     }
 
-    @Override
-    public CamelContext getCamelContext() {
-        return camelContext;
-    }
-
-    @Override
-    public void setCamelContext(CamelContext camelContext) {
-        this.camelContext = camelContext;
-    }
-
     public Endpoint getEndpoint() {
         return endpoint;
     }
@@ -111,13 +100,13 @@ public class PublishEventNotifier extends EventNotifierSupport implements CamelC
 
     @Override
     protected void doStart() throws Exception {
-        ObjectHelper.notNull(camelContext, "camelContext", this);
+        ObjectHelper.notNull(getCamelContext(), "camelContext", this);
         if (endpoint == null && endpointUri == null) {
             throw new IllegalArgumentException("Either endpoint or endpointUri must be configured");
         }
 
         if (endpoint == null) {
-            endpoint = camelContext.getEndpoint(endpointUri);
+            endpoint = getCamelContext().getEndpoint(endpointUri);
         }
 
         producer = endpoint.createProducer();
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/EventNotifierSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/EventNotifierSupport.java
index ff6b997300f..7785c35cd62 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/EventNotifierSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/EventNotifierSupport.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.support;
 
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
 import org.apache.camel.Exchange;
 import org.apache.camel.spi.CamelEvent;
 import org.apache.camel.spi.EventNotifier;
@@ -28,12 +30,24 @@ import org.apache.camel.spi.EventNotifier;
  *
  * @see SimpleEventNotifierSupport
  */
-public abstract class EventNotifierSupport extends SimpleEventNotifierSupport {
+public abstract class EventNotifierSupport extends SimpleEventNotifierSupport implements CamelContextAware {
+
+    private CamelContext camelContext;
 
     public EventNotifierSupport() {
         setupIgnore(false);
     }
 
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
     @Override
     public boolean isIgnoreCamelContextEvents() {
         return ignoreCamelContextEvents;
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/injection/AnnotationDependencyInjection.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/injection/AnnotationDependencyInjection.java
index 17d670229d7..c719cf527ca 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/injection/AnnotationDependencyInjection.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/injection/AnnotationDependencyInjection.java
@@ -18,6 +18,8 @@ package org.apache.camel.main.injection;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.inject.Produces;
@@ -39,6 +41,8 @@ import org.apache.camel.impl.engine.CamelPostProcessorHelper;
 import org.apache.camel.spi.CamelBeanPostProcessor;
 import org.apache.camel.spi.CamelBeanPostProcessorInjector;
 import org.apache.camel.spi.CompilePostProcessor;
+import org.apache.camel.spi.EventNotifier;
+import org.apache.camel.spi.ManagementStrategy;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.spi.TypeConverterRegistry;
 import org.apache.camel.util.ObjectHelper;
@@ -65,6 +69,7 @@ public final class AnnotationDependencyInjection {
 
         // camel / common
         registry.bind("CamelTypeConverterCompilePostProcessor", new TypeConverterCompilePostProcessor());
+        registry.bind("CamelEventNotifierCompilePostProcessor", new EventNotifierCompilePostProcessor());
         registry.bind("CamelBindToRegistryCompilePostProcessor", new BindToRegistryCompilePostProcessor());
         // spring
         registry.bind("SpringAnnotationCompilePostProcessor", new SpringAnnotationCompilePostProcessor());
@@ -97,6 +102,34 @@ public final class AnnotationDependencyInjection {
 
     }
 
+    private static class EventNotifierCompilePostProcessor implements CompilePostProcessor {
+
+        private final Map<String, EventNotifier> notifiers = new HashMap<>();
+
+        @Override
+        public void postCompile(CamelContext camelContext, String name, Class<?> clazz, byte[] byteCode, Object instance)
+                throws Exception {
+            if (instance == null) {
+                return;
+            }
+
+            if (instance instanceof EventNotifier) {
+                ManagementStrategy ms = camelContext.getManagementStrategy();
+                if (ms != null) {
+                    // remove previous instance
+                    EventNotifier old = notifiers.get(name);
+                    if (old != null) {
+                        ms.removeEventNotifier(old);
+                    }
+                    // and new notifier
+                    EventNotifier en = (EventNotifier) instance;
+                    ms.addEventNotifier(en);
+                    notifiers.put(name, en);
+                }
+            }
+        }
+    }
+
     private static class BindToRegistryCompilePostProcessor implements CompilePostProcessor {
 
         @Override