Skip to content

Commit d3a4ae7

Browse files
committed
GH-3726: Avoid ConcurrentModificationException in KafkaMessageListenerContainer
Fixes GH-3726 (#3726) Signed-off-by: Tim Barabanov <[email protected]>
1 parent 7aa87dd commit d3a4ae7

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

spring-kafka/src/main/java/org/springframework/kafka/listener/KafkaMessageListenerContainer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 the original author or authors.
2+
* Copyright 2016-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -625,7 +625,7 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
625625

626626
private final Map<TopicPartition, Long> offsets = new LinkedHashMap<>();
627627

628-
private final Collection<TopicPartition> assignedPartitions = new LinkedHashSet<>();
628+
private final Collection<TopicPartition> assignedPartitions = Collections.synchronizedSet(new LinkedHashSet<>());
629629

630630
private final Map<TopicPartition, OffsetAndMetadata> lastCommits = new HashMap<>();
631631

@@ -1247,7 +1247,8 @@ private void subscribeOrAssignTopics(final Consumer<? super K, ? super V> subscr
12471247
else {
12481248
List<TopicPartitionOffset> topicPartitionsToAssign =
12491249
Arrays.asList(KafkaMessageListenerContainer.this.topicPartitions);
1250-
this.definedPartitions = new LinkedHashMap<>(topicPartitionsToAssign.size());
1250+
this.definedPartitions = Collections.synchronizedMap(
1251+
new LinkedHashMap<>(topicPartitionsToAssign.size()));
12511252
for (TopicPartitionOffset topicPartition : topicPartitionsToAssign) {
12521253
this.definedPartitions.put(topicPartition.getTopicPartition(),
12531254
new OffsetMetadata(topicPartition.getOffset(), topicPartition.isRelativeToCurrent(),

0 commit comments

Comments
 (0)