Skip to content

Commit

Permalink
CAMEL-20613: set AbstractCamelContext#endpointStrategies to Concurren…
Browse files Browse the repository at this point in the history
…tHashSet to prevent ConcurrentModificationException (#13821)

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.
  • Loading branch information
bartoszpop committed Apr 18, 2024
1 parent 2f1fc98 commit 3ccf540
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public abstract class AbstractCamelContext extends BaseService

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<>();
Expand Down Expand Up @@ -4203,7 +4203,7 @@ public Registry getRegistry() {
return camelContextExtension.getRegistry();
}

List<EndpointStrategy> getEndpointStrategies() {
Set<EndpointStrategy> getEndpointStrategies() {
return endpointStrategies;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,17 @@ public Endpoint getEndpoint(NormalizedEndpointUri uri, Map<String, Object> param

@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);
}
}
}
Expand Down

0 comments on commit 3ccf540

Please sign in to comment.