This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 77ffe2ccae7 CAMEL-21543: camel-main - MainListenerClasses loaded from
application.properties is not activated
77ffe2ccae7 is described below
commit 77ffe2ccae786012ee8fff93fc92dae6c59b65dc
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Dec 13 10:08:45 2024 +0100
CAMEL-21543: camel-main - MainListenerClasses loaded from
application.properties is not activated
---
.../java/org/apache/camel/main/BaseMainSupport.java | 19 ++++++++++++++-----
.../main/java/org/apache/camel/main/MainListener.java | 4 ++++
2 files changed, 18 insertions(+), 5 deletions(-)
diff --git
a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
index 966e062dfa8..e10ca74ef01 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
@@ -294,7 +294,9 @@ public abstract class BaseMainSupport extends BaseService {
* @param listener the listener
*/
public void addMainListener(MainListener listener) {
- listeners.add(listener);
+ if (!listeners.contains(listener)) {
+ listeners.add(listener);
+ }
}
/**
@@ -551,6 +553,9 @@ public abstract class BaseMainSupport extends BaseService {
recorder.endStep(step);
}
+ // configure main listener
+ configureMainListener(camelContext);
+
// configure startup conditions
step = recorder.beginStep(BaseMainSupport.class,
"autoConfigurationStartupConditions", "Auto Configure");
autoConfigurationStartupConditions(camelContext,
autoConfiguredProperties);
@@ -693,10 +698,14 @@ public abstract class BaseMainSupport extends BaseService
{
mainConfigurationProperties.getMainListeners().forEach(this::addMainListener);
if (mainConfigurationProperties.getMainListenerClasses() != null) {
for (String fqn :
mainConfigurationProperties.getMainListenerClasses().split(",")) {
- fqn = fqn.trim();
- Class<? extends MainListener> clazz
- =
camelContext.getClassResolver().resolveMandatoryClass(fqn, MainListener.class);
- addMainListener(camelContext.getInjector().newInstance(clazz));
+ String target = fqn.trim();
+ // the class may already be added so avoid creating duplicate
classes
+ boolean added = listeners.stream().map(l ->
l.getClass().getName()).anyMatch(l -> l.equals(target));
+ if (!added) {
+ Class<? extends MainListener> clazz
+ =
camelContext.getClassResolver().resolveMandatoryClass(fqn, MainListener.class);
+
addMainListener(camelContext.getInjector().newInstance(clazz));
+ }
}
}
}
diff --git
a/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java
b/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java
index d02190a16c7..6b1410554fe 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java
@@ -18,6 +18,10 @@ package org.apache.camel.main;
/**
* A lifecycle listener to receive callbacks when the Main is started and
stopped.
+ *
+ * Beware that if you use MainListener then depending on how Camel is started
and these main listener is configured then
+ * the beforeInitialize and beforeConfigure events may already have been
triggered. So depending on your use-cases then
+ * favour using the later stage events to trigger your code.
*/
public interface MainListener {