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 bdaa9425d2da CAMEL-27038: camel-core - a route whose endpoint failed 
to create the consumer can be started again (#30244)
bdaa9425d2da is described below

commit bdaa9425d2da1d3be266ed9b96a3bf7c77c6c968
Author: allthingssecurity <[email protected]>
AuthorDate: Tue Nov 17 14:29:34 2026 +0530

    CAMEL-27038: camel-core - a route whose endpoint failed to create the 
consumer can be started again (#30244)
    
    … consumer can be started again
    
    RouteService.setUp set setUpDone before doSetup, so when the endpoint
    failed to create the consumer on a start of a stopped route, the next
    start skipped the set up: the warm up failed with a NullPointerException
    ("services" is null), and the start after that started the route with no
    consumer (the route reported Started, but nothing consumed). The route
    could only be started again after it was removed and added again.
    
    A failed set up now clears the consumer and the child services it found
    and sets setUpDone back, so the next start sets the route up again.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../org/apache/camel/impl/engine/RouteService.java | 10 +++
 .../RouteStartAfterFailedCreateConsumerTest.java   | 98 ++++++++++++++++++++++
 2 files changed, 108 insertions(+)

diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/RouteService.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/RouteService.java
index 1a40d641060f..c78a19634097 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/RouteService.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/RouteService.java
@@ -136,6 +136,16 @@ public class RouteService extends ChildServiceSupport {
             try {
                 doSetup();
             } catch (Exception e) {
+                // set up again on the next start: the route is not set up 
(such as when the endpoint failed to create
+                // the consumer), and a next start that skipped the set up 
would find no consumer and child services
+                lock.lock();
+                try {
+                    input = null;
+                    childServices = null;
+                    setUpDone.set(false);
+                } finally {
+                    lock.unlock();
+                }
                 throw new FailedToStartRouteException(getId(), getLocation(), 
extractUsefulMessage(e), e);
             }
         }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/RouteStartAfterFailedCreateConsumerTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/impl/RouteStartAfterFailedCreateConsumerTest.java
new file mode 100644
index 000000000000..04861b238982
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/RouteStartAfterFailedCreateConsumerTest.java
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.FailedToStartRouteException;
+import org.apache.camel.Processor;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.direct.DirectComponent;
+import org.apache.camel.component.direct.DirectEndpoint;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * A route whose endpoint fails to create the consumer when the route is 
started again: once the endpoint can create it,
+ * the route starts and consumes.
+ */
+class RouteStartAfterFailedCreateConsumerTest extends ContextTestSupport {
+
+    private final AtomicBoolean failCreateConsumer = new AtomicBoolean();
+
+    @Test
+    void routeStartsOnceTheConsumerCanBeCreated() throws Exception {
+        context.getRouteController().stopRoute("foo");
+
+        failCreateConsumer.set(true);
+        assertThrows(FailedToStartRouteException.class, () -> 
context.getRouteController().startRoute("foo"));
+        assertEquals(ServiceStatus.Stopped, 
context.getRouteController().getRouteStatus("foo"));
+
+        failCreateConsumer.set(false);
+        context.getRouteController().startRoute("foo");
+        assertEquals(ServiceStatus.Started, 
context.getRouteController().getRouteStatus("foo"));
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello");
+        template.sendBody("direct:flaky", "Hello");
+        mock.assertIsSatisfied();
+    }
+
+    @Test
+    void routeStartsAfterTwoFailedStarts() throws Exception {
+        context.getRouteController().stopRoute("foo");
+
+        failCreateConsumer.set(true);
+        assertThrows(FailedToStartRouteException.class, () -> 
context.getRouteController().startRoute("foo"));
+        assertThrows(FailedToStartRouteException.class, () -> 
context.getRouteController().startRoute("foo"));
+
+        failCreateConsumer.set(false);
+        context.getRouteController().startRoute("foo");
+        assertEquals(ServiceStatus.Started, 
context.getRouteController().getRouteStatus("foo"));
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello");
+        template.sendBody("direct:flaky", "Hello");
+        mock.assertIsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                context.addEndpoint("direct://flaky",
+                        new DirectEndpoint("direct://flaky", 
context.getComponent("direct", DirectComponent.class)) {
+                            @Override
+                            public Consumer createConsumer(Processor 
processor) throws Exception {
+                                if (failCreateConsumer.get()) {
+                                    throw new IllegalStateException("Simulated 
failure to create the consumer");
+                                }
+                                return super.createConsumer(processor);
+                            }
+                        });
+                from("direct:flaky").routeId("foo").to("mock:result");
+            }
+        };
+    }
+}

Reply via email to