This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch camel-4.14.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.14.x by this push:
new 69dea8e15187 CAMEL-22485: add a way to clean up resources
69dea8e15187 is described below
commit 69dea8e15187b3aeead04816da05ec6f8d996864
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Thu Oct 2 15:59:20 2025 +0200
CAMEL-22485: add a way to clean up resources
---
.../org/apache/camel/spi/ContextServicePlugin.java | 9 +++++++
.../engine/DefaultContextServiceLoaderPlugin.java | 30 ++++++++++++++++++++--
2 files changed, 37 insertions(+), 2 deletions(-)
diff --git
a/core/camel-api/src/main/java/org/apache/camel/spi/ContextServicePlugin.java
b/core/camel-api/src/main/java/org/apache/camel/spi/ContextServicePlugin.java
index 22e6553f6c2a..3dc07b7c2268 100644
---
a/core/camel-api/src/main/java/org/apache/camel/spi/ContextServicePlugin.java
+++
b/core/camel-api/src/main/java/org/apache/camel/spi/ContextServicePlugin.java
@@ -66,4 +66,13 @@ public interface ContextServicePlugin {
* @param camelContext the CamelContext being initialized, never {@code
null}
*/
void load(CamelContext camelContext);
+
+ /**
+ * Called during CamelContext stop. Use it to free allocated resources.
+ *
+ * @param camelContext the CamelContext being uninitialized, never {@code
null}
+ */
+ default void unload(CamelContext camelContext) {
+ // NO-OP
+ }
}
diff --git
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextServiceLoaderPlugin.java
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextServiceLoaderPlugin.java
index 41840bd68ebb..8104a43bf260 100644
---
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextServiceLoaderPlugin.java
+++
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextServiceLoaderPlugin.java
@@ -23,6 +23,8 @@ import org.apache.camel.CamelContextAware;
import org.apache.camel.spi.ContextServiceLoaderPluginResolver;
import org.apache.camel.spi.ContextServicePlugin;
import org.apache.camel.support.service.ServiceSupport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Default implementation that automatically discovers and loads {@link
ContextServicePlugin} implementations using the
@@ -42,7 +44,10 @@ import org.apache.camel.support.service.ServiceSupport;
* @see ServiceLoader
*/
public class DefaultContextServiceLoaderPlugin extends ServiceSupport
implements ContextServiceLoaderPluginResolver {
+ private static final Logger LOG =
LoggerFactory.getLogger(DefaultContextServiceLoaderPlugin.class);
+
private CamelContext camelContext;
+ private ServiceLoader<ContextServicePlugin> contextServicePlugins;
/**
* Discovers and loads all {@link ContextServicePlugin} implementations
found on the classpath.
@@ -58,10 +63,31 @@ public class DefaultContextServiceLoaderPlugin extends
ServiceSupport implements
*/
@Override
protected void doStart() throws Exception {
- ServiceLoader<ContextServicePlugin> contextServicePlugins =
ServiceLoader.load(ContextServicePlugin.class,
+ contextServicePlugins = ServiceLoader.load(ContextServicePlugin.class,
camelContext.getApplicationContextClassLoader());
for (ContextServicePlugin plugin : contextServicePlugins) {
- plugin.load(camelContext);
+ try {
+ plugin.load(camelContext);
+ } catch (Exception e) {
+ LOG.warn(
+ "Loading of plugin {} failed, however the exception
will be ignored so others plugins can be initialized. Reason: {}",
+ plugin.getClass().getName(), e.getMessage(), e);
+ }
+ }
+ }
+
+ @Override
+ protected void doStop() throws Exception {
+ if (contextServicePlugins != null) {
+ for (ContextServicePlugin plugin : contextServicePlugins) {
+ try {
+ plugin.unload(camelContext);
+ } catch (Exception e) {
+ LOG.warn(
+ "Unloading of plugin {} failed, however the
exception will be ignored so shutdown can continue. Reason: {}",
+ plugin.getClass().getName(), e.getMessage(), e);
+ }
+ }
}
}