You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/10/17 16:46:58 UTC

[camel] branch main updated: CAMEL-19998: reduce coupling between CamelContext and the InternalServiceManager

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

orpiske 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 be5b9764703 CAMEL-19998: reduce coupling between CamelContext and the InternalServiceManager
be5b9764703 is described below

commit be5b97647034d345675ae906e05c23a0cf68a93e
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Tue Oct 17 14:42:52 2023 +0200

    CAMEL-19998: reduce coupling between CamelContext and the InternalServiceManager
---
 .../camel/impl/engine/AbstractCamelContext.java    | 18 ++++----
 .../impl/engine/DefaultCamelContextExtension.java  | 52 +++++++++++-----------
 .../camel/impl/engine/InternalServiceManager.java  | 31 +++++++------
 3 files changed, 51 insertions(+), 50 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 288309f36ef..43015772e97 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -305,7 +305,7 @@ public abstract class AbstractCamelContext extends BaseService
         // add the default bootstrap closer
         camelContextExtension.addBootstrap(new DefaultServiceBootstrapCloseable(this));
 
-        this.internalServiceManager = new InternalServiceManager(this, internalRouteStartupManager, startupListeners);
+        this.internalServiceManager = new InternalServiceManager(internalRouteStartupManager, startupListeners);
 
         initPlugins();
 
@@ -1315,12 +1315,12 @@ public abstract class AbstractCamelContext extends BaseService
 
     @Override
     public void addService(Object object, boolean stopOnShutdown, boolean forceStart) throws Exception {
-        internalServiceManager.doAddService(object, stopOnShutdown, forceStart, true);
+        internalServiceManager.doAddService(this, object, stopOnShutdown, forceStart, true);
     }
 
     @Override
     public void addPrototypeService(Object object) throws Exception {
-        internalServiceManager.addService(object, false, true, false);
+        internalServiceManager.addService(this, object, false, true, false);
     }
 
     @Override
@@ -1357,7 +1357,7 @@ public abstract class AbstractCamelContext extends BaseService
 
     @Override
     public void deferStartService(Object object, boolean stopOnShutdown) throws Exception {
-        internalServiceManager.deferStartService(object, stopOnShutdown, false);
+        internalServiceManager.deferStartService(this, object, stopOnShutdown, false);
     }
 
     protected List<StartupListener> getStartupListeners() {
@@ -1824,7 +1824,7 @@ public abstract class AbstractCamelContext extends BaseService
 
     @Override
     public void setRuntimeEndpointRegistry(RuntimeEndpointRegistry runtimeEndpointRegistry) {
-        this.runtimeEndpointRegistry = internalServiceManager.addService(runtimeEndpointRegistry);
+        this.runtimeEndpointRegistry = internalServiceManager.addService(this, runtimeEndpointRegistry);
     }
 
     @Override
@@ -2246,7 +2246,7 @@ public abstract class AbstractCamelContext extends BaseService
 
         // re-create endpoint registry as the cache size limit may be set after the constructor of this instance was called.
         // and we needed to create endpoints up-front as it may be accessed before this context is started
-        endpoints = internalServiceManager.addService(createEndpointRegistry(endpoints));
+        endpoints = internalServiceManager.addService(this, createEndpointRegistry(endpoints));
 
         // optimised to not include runtimeEndpointRegistry unless startServices
         // is enabled or JMX statistics is in extended mode
@@ -2813,7 +2813,7 @@ public abstract class AbstractCamelContext extends BaseService
         // consumer (eg @Consumer)
         // which we need to stop after the routes, as a POJO consumer is
         // essentially a route also
-        internalServiceManager.stopConsumers();
+        internalServiceManager.stopConsumers(this);
 
         // the stop order is important
 
@@ -2837,7 +2837,7 @@ public abstract class AbstractCamelContext extends BaseService
         languages.clear();
 
         // shutdown services as late as possible (except type converters as they may be needed during the remainder of the stopping)
-        internalServiceManager.shutdownServices();
+        internalServiceManager.shutdownServices(this);
 
         try {
             for (LifecycleStrategy strategy : lifecycleStrategies) {
@@ -3595,7 +3595,7 @@ public abstract class AbstractCamelContext extends BaseService
         if (isStartingOrStarted()) {
             throw new IllegalStateException("Cannot set debugger on a started CamelContext");
         }
-        this.debugger = internalServiceManager.addService(debugger, true, false, true);
+        this.debugger = internalServiceManager.addService(this, debugger, true, false, true);
     }
 
     @Override
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java
index 36a00c93d2d..ba5683459c5 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultCamelContextExtension.java
@@ -170,7 +170,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setNameStrategy(CamelContextNameStrategy nameStrategy) {
-        this.nameStrategy = camelContext.getInternalServiceManager().addService(nameStrategy);
+        this.nameStrategy = camelContext.getInternalServiceManager().addService(camelContext, nameStrategy);
     }
 
     ManagementNameStrategy getManagementNameStrategy() {
@@ -185,7 +185,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setManagementNameStrategy(ManagementNameStrategy managementNameStrategy) {
-        this.managementNameStrategy = camelContext.getInternalServiceManager().addService(managementNameStrategy);
+        this.managementNameStrategy = camelContext.getInternalServiceManager().addService(camelContext, managementNameStrategy);
     }
 
     PropertiesComponent getPropertiesComponent() {
@@ -200,7 +200,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setPropertiesComponent(PropertiesComponent propertiesComponent) {
-        this.propertiesComponent = camelContext.getInternalServiceManager().addService(propertiesComponent);
+        this.propertiesComponent = camelContext.getInternalServiceManager().addService(camelContext, propertiesComponent);
     }
 
     @Override
@@ -342,7 +342,8 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setManagementMBeanAssembler(ManagementMBeanAssembler managementMBeanAssembler) {
-        this.managementMBeanAssembler = camelContext.getInternalServiceManager().addService(managementMBeanAssembler, false);
+        this.managementMBeanAssembler
+                = camelContext.getInternalServiceManager().addService(camelContext, managementMBeanAssembler, false);
     }
 
     void stopRegistry() {
@@ -526,7 +527,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
 
     @Override
     public void setHeadersMapFactory(HeadersMapFactory headersMapFactory) {
-        this.headersMapFactory = camelContext.getInternalServiceManager().addService(headersMapFactory);
+        this.headersMapFactory = camelContext.getInternalServiceManager().addService(camelContext, headersMapFactory);
     }
 
     void initEagerMandatoryServices(boolean caseInsensitive, Supplier<HeadersMapFactory> headersMapFactorySupplier) {
@@ -579,7 +580,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
 
     @Override
     public void setExchangeFactoryManager(ExchangeFactoryManager exchangeFactoryManager) {
-        this.exchangeFactoryManager = camelContext.getInternalServiceManager().addService(exchangeFactoryManager);
+        this.exchangeFactoryManager = camelContext.getInternalServiceManager().addService(camelContext, exchangeFactoryManager);
     }
 
     @Override
@@ -617,7 +618,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     public void setReactiveExecutor(ReactiveExecutor reactiveExecutor) {
         // special for executorServiceManager as want to stop it manually so
         // false in stopOnShutdown
-        this.reactiveExecutor = camelContext.getInternalServiceManager().addService(reactiveExecutor, false);
+        this.reactiveExecutor = camelContext.getInternalServiceManager().addService(camelContext, reactiveExecutor, false);
     }
 
     RestRegistryFactory getRestRegistryFactory() {
@@ -632,7 +633,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setRestRegistryFactory(RestRegistryFactory restRegistryFactory) {
-        this.restRegistryFactory = camelContext.getInternalServiceManager().addService(restRegistryFactory);
+        this.restRegistryFactory = camelContext.getInternalServiceManager().addService(camelContext, restRegistryFactory);
     }
 
     RestRegistry getRestRegistry() {
@@ -647,7 +648,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setRestRegistry(RestRegistry restRegistry) {
-        this.restRegistry = camelContext.getInternalServiceManager().addService(restRegistry);
+        this.restRegistry = camelContext.getInternalServiceManager().addService(camelContext, restRegistry);
     }
 
     RestConfiguration getRestConfiguration() {
@@ -677,7 +678,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setClassResolver(ClassResolver classResolver) {
-        this.classResolver = camelContext.getInternalServiceManager().addService(classResolver);
+        this.classResolver = camelContext.getInternalServiceManager().addService(camelContext, classResolver);
     }
 
     MessageHistoryFactory getMessageHistoryFactory() {
@@ -692,7 +693,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setMessageHistoryFactory(MessageHistoryFactory messageHistoryFactory) {
-        this.messageHistoryFactory = camelContext.getInternalServiceManager().addService(messageHistoryFactory);
+        this.messageHistoryFactory = camelContext.getInternalServiceManager().addService(camelContext, messageHistoryFactory);
     }
 
     StreamCachingStrategy getStreamCachingStrategy() {
@@ -708,7 +709,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
 
     void setStreamCachingStrategy(StreamCachingStrategy streamCachingStrategy) {
         this.streamCachingStrategy
-                = camelContext.getInternalServiceManager().addService(streamCachingStrategy, true, false, true);
+                = camelContext.getInternalServiceManager().addService(camelContext, streamCachingStrategy, true, false, true);
     }
 
     InflightRepository getInflightRepository() {
@@ -723,7 +724,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setInflightRepository(InflightRepository repository) {
-        this.inflightRepository = camelContext.getInternalServiceManager().addService(repository);
+        this.inflightRepository = camelContext.getInternalServiceManager().addService(camelContext, repository);
     }
 
     UuidGenerator getUuidGenerator() {
@@ -738,7 +739,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setUuidGenerator(UuidGenerator uuidGenerator) {
-        this.uuidGenerator = camelContext.getInternalServiceManager().addService(uuidGenerator);
+        this.uuidGenerator = camelContext.getInternalServiceManager().addService(camelContext, uuidGenerator);
     }
 
     Tracer getTracer() {
@@ -753,7 +754,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setTracer(Tracer tracer) {
-        this.tracer = camelContext.getInternalServiceManager().addService(tracer, true, false, true);
+        this.tracer = camelContext.getInternalServiceManager().addService(camelContext, tracer, true, false, true);
     }
 
     TransformerRegistry getTransformerRegistry() {
@@ -768,7 +769,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setTransformerRegistry(TransformerRegistry transformerRegistry) {
-        this.transformerRegistry = camelContext.getInternalServiceManager().addService(transformerRegistry);
+        this.transformerRegistry = camelContext.getInternalServiceManager().addService(camelContext, transformerRegistry);
     }
 
     ValidatorRegistry getValidatorRegistry() {
@@ -783,7 +784,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     public void setValidatorRegistry(ValidatorRegistry validatorRegistry) {
-        this.validatorRegistry = camelContext.getInternalServiceManager().addService(validatorRegistry);
+        this.validatorRegistry = camelContext.getInternalServiceManager().addService(camelContext, validatorRegistry);
     }
 
     void stopTypeConverterRegistry() {
@@ -811,7 +812,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setTypeConverterRegistry(TypeConverterRegistry typeConverterRegistry) {
-        this.typeConverterRegistry = camelContext.getInternalServiceManager().addService(typeConverterRegistry);
+        this.typeConverterRegistry = camelContext.getInternalServiceManager().addService(camelContext, typeConverterRegistry);
     }
 
     void stopTypeConverter() {
@@ -827,7 +828,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setTypeConverter(TypeConverter typeConverter) {
-        this.typeConverter = camelContext.getInternalServiceManager().addService(typeConverter);
+        this.typeConverter = camelContext.getInternalServiceManager().addService(camelContext, typeConverter);
     }
 
     TypeConverter getOrCreateTypeConverter() {
@@ -857,7 +858,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setInjector(Injector injector) {
-        this.injector = camelContext.getInternalServiceManager().addService(injector);
+        this.injector = camelContext.getInternalServiceManager().addService(camelContext, injector);
     }
 
     void stopAndShutdownRouteController() {
@@ -876,7 +877,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setRouteController(RouteController routeController) {
-        this.routeController = camelContext.getInternalServiceManager().addService(routeController);
+        this.routeController = camelContext.getInternalServiceManager().addService(camelContext, routeController);
     }
 
     ShutdownStrategy getShutdownStrategy() {
@@ -891,7 +892,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     }
 
     void setShutdownStrategy(ShutdownStrategy shutdownStrategy) {
-        this.shutdownStrategy = camelContext.getInternalServiceManager().addService(shutdownStrategy);
+        this.shutdownStrategy = camelContext.getInternalServiceManager().addService(camelContext, shutdownStrategy);
     }
 
     ExecutorServiceManager getExecutorServiceManager() {
@@ -908,7 +909,8 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     void setExecutorServiceManager(ExecutorServiceManager executorServiceManager) {
         // special for executorServiceManager as want to stop it manually so
         // false in stopOnShutdown
-        this.executorServiceManager = camelContext.getInternalServiceManager().addService(executorServiceManager, false);
+        this.executorServiceManager
+                = camelContext.getInternalServiceManager().addService(camelContext, executorServiceManager, false);
     }
 
     @Override
@@ -980,7 +982,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
 
     @Override
     public <T> void addContextPlugin(Class<T> type, T module) {
-        final T addedModule = camelContext.getInternalServiceManager().addService(module);
+        final T addedModule = camelContext.getInternalServiceManager().addService(camelContext, module);
         pluginManager.addContextPlugin(type, addedModule);
     }
 
@@ -992,7 +994,7 @@ class DefaultCamelContextExtension implements ExtendedCamelContext {
     private <T> T lazyInitAndAdd(Supplier<T> supplier) {
         T module = supplier.get();
 
-        return camelContext.getInternalServiceManager().addService(module);
+        return camelContext.getInternalServiceManager().addService(camelContext, module);
     }
 
     /*
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/InternalServiceManager.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/InternalServiceManager.java
index e2502da0e2f..affe6b7677c 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/InternalServiceManager.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/InternalServiceManager.java
@@ -47,46 +47,45 @@ import org.slf4j.LoggerFactory;
 final class InternalServiceManager {
     private static final Logger LOG = LoggerFactory.getLogger(InternalServiceManager.class);
 
-    private final CamelContext camelContext;
     private final InternalRouteStartupManager internalRouteStartupManager;
 
     private final DeferServiceStartupListener deferStartupListener = new DeferServiceStartupListener();
     private final List<Service> services = new CopyOnWriteArrayList<>();
 
-    InternalServiceManager(CamelContext camelContext, InternalRouteStartupManager internalRouteStartupManager,
-                           List<StartupListener> startupListeners) {
+    InternalServiceManager(InternalRouteStartupManager internalRouteStartupManager, List<StartupListener> startupListeners) {
         /*
          Note: this is an internal API and not meant to be public, so it uses assertion for lightweight nullability
          checking for extremely unlikely scenarios that should be found during development time.
          */
-        assert camelContext != null : "the Camel context cannot be null";
         assert internalRouteStartupManager != null : "the internalRouteStartupManager cannot be null";
         assert startupListeners != null : "the startupListeners cannot be null";
 
-        this.camelContext = camelContext;
         this.internalRouteStartupManager = internalRouteStartupManager;
 
         startupListeners.add(deferStartupListener);
     }
 
-    public <T> T addService(T object) {
-        return addService(object, true);
+    public <T> T addService(CamelContext camelContext, T object) {
+        return addService(camelContext, object, true);
     }
 
-    public <T> T addService(T object, boolean stopOnShutdown) {
-        return addService(object, stopOnShutdown, true, true);
+    public <T> T addService(CamelContext camelContext, T object, boolean stopOnShutdown) {
+        return addService(camelContext, object, stopOnShutdown, true, true);
     }
 
-    public <T> T addService(T object, boolean stopOnShutdown, boolean forceStart, boolean useLifecycleStrategies) {
+    public <T> T addService(
+            CamelContext camelContext, T object, boolean stopOnShutdown, boolean forceStart, boolean useLifecycleStrategies) {
         try {
-            doAddService(object, stopOnShutdown, forceStart, useLifecycleStrategies);
+            doAddService(camelContext, object, stopOnShutdown, forceStart, useLifecycleStrategies);
         } catch (Exception e) {
             throw RuntimeCamelException.wrapRuntimeCamelException(e);
         }
         return object;
     }
 
-    public void doAddService(Object object, boolean stopOnShutdown, boolean forceStart, boolean useLifecycleStrategies)
+    public void doAddService(
+            CamelContext camelContext, Object object, boolean stopOnShutdown, boolean forceStart,
+            boolean useLifecycleStrategies)
             throws Exception {
 
         if (object == null) {
@@ -147,14 +146,14 @@ final class InternalServiceManager {
                         ServiceHelper.startService(service);
                     } else {
                         ServiceHelper.initService(service);
-                        deferStartService(object, stopOnShutdown, true);
+                        deferStartService(camelContext, object, stopOnShutdown, true);
                     }
                 }
             }
         }
     }
 
-    public void deferStartService(Object object, boolean stopOnShutdown, boolean startEarly) {
+    public void deferStartService(CamelContext camelContext, Object object, boolean stopOnShutdown, boolean startEarly) {
         if (object instanceof Service) {
             Service service = (Service) object;
 
@@ -223,7 +222,7 @@ final class InternalServiceManager {
         return null;
     }
 
-    public void stopConsumers() {
+    public void stopConsumers(CamelContext camelContext) {
         for (Service service : services) {
             if (service instanceof Consumer) {
                 InternalServiceManager.shutdownServices(camelContext, service);
@@ -231,7 +230,7 @@ final class InternalServiceManager {
         }
     }
 
-    public void shutdownServices() {
+    public void shutdownServices(CamelContext camelContext) {
         InternalServiceManager.shutdownServices(camelContext, services);
         services.clear();
     }