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

davsclaus pushed a commit to branch fix/CAMEL-24065
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 0a8742c05c35650ba0f98d1e67452dbb8918af32
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Jul 15 11:23:02 2026 +0200

    CAMEL-24065: camel-spring - Fix ConcurrentModificationException in 
EventComponent
    
    Co-Authored-By: Claude Opus 4.6 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../event/EventComponentConcurrencyTest.java       | 101 +++++++++++++++++++++
 .../camel/component/event/EventComponent.java      |   4 +-
 .../camel/component/event/EventEndpoint.java       |   4 +-
 3 files changed, 106 insertions(+), 3 deletions(-)

diff --git 
a/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/component/event/EventComponentConcurrencyTest.java
 
b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/component/event/EventComponentConcurrencyTest.java
new file mode 100644
index 000000000000..990700257f3e
--- /dev/null
+++ 
b/components/camel-spring-parent/camel-spring-xml/src/test/java/org/apache/camel/component/event/EventComponentConcurrencyTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.component.event;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spring.SpringCamelContext;
+import org.junit.jupiter.api.Test;
+import org.springframework.context.ApplicationEvent;
+import 
org.springframework.context.annotation.AnnotationConfigApplicationContext;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+public class EventComponentConcurrencyTest {
+
+    @Test
+    void stoppingRouteDuringEventDispatchMustNotThrow() throws Exception {
+        try (AnnotationConfigApplicationContext appCtx = new 
AnnotationConfigApplicationContext()) {
+            appCtx.refresh();
+
+            SpringCamelContext camel = new SpringCamelContext(appCtx);
+            appCtx.addApplicationListener(camel);
+
+            camel.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() {
+                    from("spring-event:first").routeId("first")
+                            .process(e -> 
e.getContext().getRouteController().stopRoute("second"));
+                    from("spring-event:second").routeId("second")
+                            .to("log:second");
+                }
+            });
+            camel.start();
+
+            try {
+                assertDoesNotThrow(() -> appCtx.publishEvent(new 
TestEvent(this)));
+            } finally {
+                camel.stop();
+            }
+        }
+    }
+
+    @Test
+    void survivingEndpointStillReceivesEventsAfterAnotherIsStopped() throws 
Exception {
+        try (AnnotationConfigApplicationContext appCtx = new 
AnnotationConfigApplicationContext()) {
+            appCtx.refresh();
+
+            SpringCamelContext camel = new SpringCamelContext(appCtx);
+            appCtx.addApplicationListener(camel);
+
+            camel.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() {
+                    from("spring-event:endpointA").routeId("routeA")
+                            .to("mock:a");
+                    from("spring-event:endpointB").routeId("routeB")
+                            .to("mock:b");
+                }
+            });
+            camel.start();
+
+            MockEndpoint mockA = camel.getEndpoint("mock:a", 
MockEndpoint.class);
+            MockEndpoint mockB = camel.getEndpoint("mock:b", 
MockEndpoint.class);
+
+            // both endpoints receive the first event
+            mockA.expectedMinimumMessageCount(1);
+            mockB.expectedMinimumMessageCount(1);
+            appCtx.publishEvent(new TestEvent(this));
+            MockEndpoint.assertIsSatisfied(camel);
+
+            // stop routeA, routeB must still receive events
+            camel.getRouteController().stopRoute("routeA");
+            mockB.reset();
+            mockB.expectedMinimumMessageCount(1);
+            appCtx.publishEvent(new TestEvent(this));
+            MockEndpoint.assertIsSatisfied(camel);
+
+            camel.stop();
+        }
+    }
+
+    static class TestEvent extends ApplicationEvent {
+        TestEvent(Object source) {
+            super(source);
+        }
+    }
+}
diff --git 
a/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/component/event/EventComponent.java
 
b/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/component/event/EventComponent.java
index 141d94a61c73..75f68442bc44 100644
--- 
a/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/component/event/EventComponent.java
+++ 
b/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/component/event/EventComponent.java
@@ -16,9 +16,9 @@
  */
 package org.apache.camel.component.event;
 
-import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.CopyOnWriteArraySet;
 
 import org.apache.camel.spi.annotations.Component;
 import org.apache.camel.support.DefaultComponent;
@@ -37,7 +37,7 @@ import 
org.springframework.context.ConfigurableApplicationContext;
 public class EventComponent extends DefaultComponent implements 
ApplicationContextAware {
     private static final Logger LOG = 
LoggerFactory.getLogger(EventComponent.class);
     private ApplicationContext applicationContext;
-    private final Set<EventEndpoint> endpoints = new LinkedHashSet<>();
+    private final Set<EventEndpoint> endpoints = new CopyOnWriteArraySet<>();
 
     public EventComponent() {
     }
diff --git 
a/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/component/event/EventEndpoint.java
 
b/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/component/event/EventEndpoint.java
index 667035bf1355..811ff8fc0a92 100644
--- 
a/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/component/event/EventEndpoint.java
+++ 
b/components/camel-spring-parent/camel-spring/src/main/java/org/apache/camel/component/event/EventEndpoint.java
@@ -134,8 +134,10 @@ public class EventEndpoint extends DefaultEndpoint 
implements ApplicationContext
     public void consumerStopped(EventConsumer consumer) {
         lock.lock();
         try {
-            getComponent().consumerStopped(this);
             getLoadBalancer().removeProcessor(consumer.getAsyncProcessor());
+            if (getLoadBalancer().getProcessors().isEmpty()) {
+                getComponent().consumerStopped(this);
+            }
         } finally {
             lock.unlock();
         }

Reply via email to