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

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


The following commit(s) were added to refs/heads/camel-4.0.x by this push:
     new 90aed8b0d5f CAMEL-20613: set AbstractCamelContext#endpointStrategies 
to ConcurrentHashSet to prevent ConcurrentModificationException (#13820)
90aed8b0d5f is described below

commit 90aed8b0d5f27de84efe6ae607b7da9c974bb1dc
Author: Bartosz Popiela <[email protected]>
AuthorDate: Thu Apr 18 13:11:13 2024 +0200

    CAMEL-20613: set AbstractCamelContext#endpointStrategies to 
ConcurrentHashSet to prevent ConcurrentModificationException (#13820)
    
    If a thread iterates over the list of strategies while another thread adds 
a new strategy, ConcurrentModificationException is thrown. To fix this issue, 
AbstractCamelContext#endpointStrategies can be set to a thread-safe collection.
---
 .../org/apache/camel/impl/engine/AbstractCamelContext.java |  4 ++--
 .../camel/impl/engine/DefaultCamelContextExtension.java    | 14 ++++++++------
 2 files changed, 10 insertions(+), 8 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 e046a0aec54..ff0d40b00d2 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
@@ -204,7 +204,7 @@ public abstract class AbstractCamelContext extends 
BaseService
     private final Object lock = new Object();
     private final DefaultCamelContextExtension camelContextExtension = new 
DefaultCamelContextExtension(this);
     private final AtomicInteger endpointKeyCounter = new AtomicInteger();
-    private final List<EndpointStrategy> endpointStrategies = new 
ArrayList<>();
+    private final Set<EndpointStrategy> endpointStrategies = 
ConcurrentHashMap.newKeySet();
     private final GlobalEndpointConfiguration globalEndpointConfiguration = 
new DefaultGlobalEndpointConfiguration();
     private final Map<String, Component> components = new 
ConcurrentHashMap<>();
     private final Set<Route> routes = new LinkedHashSet<>();
@@ -4226,7 +4226,7 @@ public abstract class AbstractCamelContext extends 
BaseService
         return camelContextExtension.getRegistry();
     }
 
-    List<EndpointStrategy> getEndpointStrategies() {
+    Set<EndpointStrategy> getEndpointStrategies() {
         return endpointStrategies;
     }
 
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 a32cf1c34a3..3f161551457 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
@@ -173,15 +173,17 @@ class DefaultCamelContextExtension implements 
ExtendedCamelContext {
 
     @Override
     public void registerEndpointCallback(EndpointStrategy strategy) {
-        if (!camelContext.getEndpointStrategies().contains(strategy)) {
-            // let it be invoked for already registered endpoints so it can
-            // catch-up.
-            camelContext.getEndpointStrategies().add(strategy);
+        // let it be invoked for already registered endpoints so it can
+        // catch-up.
+        if (camelContext.getEndpointStrategies().add(strategy)) {
             for (Endpoint endpoint : camelContext.getEndpoints()) {
-                Endpoint newEndpoint = 
strategy.registerEndpoint(endpoint.getEndpointUri(), endpoint);
+                Endpoint newEndpoint = 
strategy.registerEndpoint(endpoint.getEndpointUri(),
+                        endpoint);
                 if (newEndpoint != null) {
                     // put will replace existing endpoint with the new endpoint
-                    
camelContext.getEndpointRegistry().put(camelContext.getEndpointKey(endpoint.getEndpointUri()),
 newEndpoint);
+                    camelContext.getEndpointRegistry()
+                            
.put(camelContext.getEndpointKey(endpoint.getEndpointUri()),
+                                    newEndpoint);
                 }
             }
         }

Reply via email to