This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch kamelet2 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 5f3181603b92ae6e7ba30628caa0909dc3763e2e Author: Claus Ibsen <[email protected]> AuthorDate: Sun Jan 26 09:46:38 2025 +0100 CAMEL-21599: camel-kamelet - Rework error handler for kamelets to be more standard Camel. WIP --- .../camel/component/kamelet/KameletComponent.java | 31 +++++++++++++++------- .../org/apache/camel/ExtendedCamelContext.java | 19 +++++++++++++ .../org/apache/camel/spi/LifecycleStrategy.java | 10 +++++++ .../impl/engine/DefaultCamelContextExtension.java | 15 +++++++++++ .../camel/impl/engine/InternalServiceManager.java | 16 +++++------ .../org/apache/camel/impl/DefaultCamelContext.java | 4 +++ 6 files changed, 78 insertions(+), 17 deletions(-) diff --git a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java index e7d493e0d1a..586293c5d64 100644 --- a/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java +++ b/components/camel-kamelet/src/main/java/org/apache/camel/component/kamelet/KameletComponent.java @@ -171,7 +171,8 @@ public class KameletComponent extends DefaultComponent { // since this is the real kamelet, then we need to hand it // over to the tracker. // - lifecycleHandler.track(this); + String routeId = getCamelContext().getCamelContextExtension().getCreateRoutes(); + lifecycleHandler.track(this, routeId); } }; @@ -431,7 +432,11 @@ public class KameletComponent extends DefaultComponent { * be used to create routes from templates. */ private class LifecycleHandler extends LifecycleStrategySupport { - private final List<KameletEndpoint> endpoints; + + record Tuple(KameletEndpoint endpoint, String parentRouteId) { + } + + private final List<Tuple> endpoints; private final AtomicBoolean initialized; public LifecycleHandler() { @@ -439,7 +444,7 @@ public class KameletComponent extends DefaultComponent { this.initialized = new AtomicBoolean(); } - public void createRouteForEndpoint(KameletEndpoint endpoint) throws Exception { + public void createRouteForEndpoint(KameletEndpoint endpoint, String parentRouteId) throws Exception { final ModelCamelContext context = (ModelCamelContext) getCamelContext(); final String templateId = endpoint.getTemplateId(); final String routeId = endpoint.getRouteId(); @@ -470,15 +475,23 @@ public class KameletComponent extends DefaultComponent { } } + // @Override + // public void onEndpointAdd(Endpoint endpoint, Route route) { + // if (endpoint instanceof KameletEndpoint ke) { + // String parentRouteId = getCamelContext().getCamelContextExtension().getCreateRoutes(); + // track(ke, parentRouteId); + // } + // } + @Override public void onContextInitialized(CamelContext context) throws VetoCamelContextStartException { if (this.initialized.compareAndSet(false, true)) { - for (KameletEndpoint endpoint : endpoints) { + for (Tuple tuple : endpoints) { try { - createRouteForEndpoint(endpoint); + createRouteForEndpoint(tuple.endpoint, tuple.parentRouteId); } catch (Exception e) { throw new VetoCamelContextStartException( - "Failure creating route from template: " + endpoint.getTemplateId(), e, context); + "Failure creating route from template: " + tuple.endpoint.getTemplateId(), e, context); } } @@ -490,16 +503,16 @@ public class KameletComponent extends DefaultComponent { this.initialized.set(initialized); } - public void track(KameletEndpoint endpoint) { + public void track(KameletEndpoint endpoint, String parentRouteId) { if (this.initialized.get()) { try { - createRouteForEndpoint(endpoint); + createRouteForEndpoint(endpoint, parentRouteId); } catch (Exception e) { throw RuntimeCamelException.wrapRuntimeException(e); } } else { LOG.debug("Tracking route template={} and id={}", endpoint.getTemplateId(), endpoint.getRouteId()); - this.endpoints.add(endpoint); + this.endpoints.add(new Tuple(endpoint, parentRouteId)); } } } diff --git a/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java b/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java index a03bad961fb..0f72c929411 100644 --- a/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java +++ b/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java @@ -122,9 +122,28 @@ public interface ExtendedCamelContext { * {@link CamelContext} itself is in started state. * * @return <tt>true</tt> if current thread is setting up route(s), or <tt>false</tt> if not. + * @see #getCreateRoutes() */ boolean isSetupRoutes(); + /** + * Method to signal to {@link CamelContext} that the process to create routes is in progress. + * + * @param routeId the current id of the route being created + * @see #getCreateRoutes() + */ + void createRoutes(String routeId); + + /** + * Indicates whether current thread is creating a route as part of starting Camel. + * <p/> + * This can be useful to know by {@link LifecycleStrategy} or the likes, in case they need to react differently. + * + * @return the route id currently being created/started, or <tt>null</tt> if not. + * @see #isSetupRoutes() + */ + String getCreateRoutes(); + /** * Registers a {@link org.apache.camel.spi.EndpointStrategy callback} to allow you to do custom logic when an * {@link Endpoint} is about to be registered to the {@link org.apache.camel.spi.EndpointRegistry}. diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/LifecycleStrategy.java b/core/camel-api/src/main/java/org/apache/camel/spi/LifecycleStrategy.java index 4b397c27f3c..a1abcf2ec49 100644 --- a/core/camel-api/src/main/java/org/apache/camel/spi/LifecycleStrategy.java +++ b/core/camel-api/src/main/java/org/apache/camel/spi/LifecycleStrategy.java @@ -111,6 +111,16 @@ public interface LifecycleStrategy { */ void onEndpointAdd(Endpoint endpoint); + /** + * Notification on adding an {@link Endpoint}. + * + * @param endpoint the added endpoint + * @param route the route the endpoint belongs to if any possible to determine + */ + default void onEndpointAdd(Endpoint endpoint, Route route) { + onEndpointAdd(endpoint); + } + /** * Notification on removing an {@link Endpoint}. * 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 6f8557fc90a..488d6b04bd9 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 @@ -91,6 +91,7 @@ import org.slf4j.LoggerFactory; class DefaultCamelContextExtension implements ExtendedCamelContext { private final AbstractCamelContext camelContext; + private final ThreadLocal<String> isCreateRoutes = new ThreadLocal<>(); private final ThreadLocal<Boolean> isSetupRoutes = new ThreadLocal<>(); private final List<InterceptStrategy> interceptStrategies = new ArrayList<>(); private final Map<String, FactoryFinder> factories = new ConcurrentHashMap<>(); @@ -307,6 +308,11 @@ class DefaultCamelContextExtension implements ExtendedCamelContext { return answer != null && answer; } + @Override + public String getCreateRoutes() { + return isCreateRoutes.get(); + } + @Override public void addBootstrap(BootstrapCloseable bootstrap) { bootstraps.add(bootstrap); @@ -393,6 +399,15 @@ class DefaultCamelContextExtension implements ExtendedCamelContext { this.registry = registry; } + @Override + public void createRoutes(String routeId) { + if (routeId != null) { + isCreateRoutes.set(routeId); + } else { + isSetupRoutes.remove(); + } + } + @Override public void setupRoutes(boolean done) { if (done) { 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 a55d55d62f3..2bfa1305889 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 @@ -98,17 +98,17 @@ final class InternalServiceManager { if (object instanceof Service service) { if (useLifecycleStrategies) { for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) { + Route route; + if (service instanceof RouteAware routeAware) { + route = routeAware.getRoute(); + } else { + // if the service is added while creating a new route then grab the route from the startup manager + route = internalRouteStartupManager.getSetupRoute(); + } if (service instanceof Endpoint endpoint) { // use specialized endpoint add - strategy.onEndpointAdd(endpoint); + strategy.onEndpointAdd(endpoint, route); } else { - Route route; - if (service instanceof RouteAware routeAware) { - route = routeAware.getRoute(); - } else { - // if the service is added while creating a new route then grab the route from the startup manager - route = internalRouteStartupManager.getSetupRoute(); - } strategy.onServiceAdd(camelContext, service, route); } } diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java index 950e5686a6f..4ced97c424c 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultCamelContext.java @@ -723,6 +723,9 @@ public class DefaultCamelContext extends SimpleCamelContext implements ModelCame StartupStepRecorder recorder = getCamelContextReference().getCamelContextExtension().getStartupStepRecorder(); StartupStep step = recorder.beginStep(Route.class, routeDefinition.getRouteId(), "Create Route"); + + getCamelContextExtension().createRoutes(routeDefinition.getRouteId()); + Route route = model.getModelReifierFactory().createRoute(this, routeDefinition); recorder.endStep(step); @@ -750,6 +753,7 @@ public class DefaultCamelContext extends SimpleCamelContext implements ModelCame if (!alreadyStartingRoutes) { setStartingRoutes(false); } + getCamelContextExtension().createRoutes(null); pc.setLocalProperties(null); if (localBeans != null) { localBeans.setLocalBeanRepository(null);
