|
1 | 1 | /* |
2 | | - * Copyright 2015-2019 the original author or authors. |
| 2 | + * Copyright 2015-2020 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -93,38 +93,46 @@ public void setConcurrency(int concurrency) { |
93 | 93 | * this container. |
94 | 94 | */ |
95 | 95 | public List<KafkaMessageListenerContainer<K, V>> getContainers() { |
96 | | - return Collections.unmodifiableList(this.containers); |
| 96 | + synchronized (this.lifecycleMonitor) { |
| 97 | + return Collections.unmodifiableList(new ArrayList<>(this.containers)); |
| 98 | + } |
97 | 99 | } |
98 | 100 |
|
99 | 101 | @Override |
100 | 102 | public Collection<TopicPartition> getAssignedPartitions() { |
101 | | - return this.containers.stream() |
102 | | - .map(KafkaMessageListenerContainer::getAssignedPartitions) |
103 | | - .filter(Objects::nonNull) |
104 | | - .flatMap(Collection::stream) |
105 | | - .collect(Collectors.toList()); |
| 103 | + synchronized (this.lifecycleMonitor) { |
| 104 | + return this.containers.stream() |
| 105 | + .map(KafkaMessageListenerContainer::getAssignedPartitions) |
| 106 | + .filter(Objects::nonNull) |
| 107 | + .flatMap(Collection::stream) |
| 108 | + .collect(Collectors.toList()); |
| 109 | + } |
106 | 110 | } |
107 | 111 |
|
108 | 112 | @Override |
109 | 113 | public boolean isContainerPaused() { |
110 | | - boolean paused = isPaused(); |
111 | | - if (paused) { |
112 | | - for (AbstractMessageListenerContainer<K, V> container : this.containers) { |
113 | | - if (!container.isContainerPaused()) { |
114 | | - return false; |
| 114 | + synchronized (this.lifecycleMonitor) { |
| 115 | + boolean paused = isPaused(); |
| 116 | + if (paused) { |
| 117 | + for (AbstractMessageListenerContainer<K, V> container : this.containers) { |
| 118 | + if (!container.isContainerPaused()) { |
| 119 | + return false; |
| 120 | + } |
115 | 121 | } |
116 | 122 | } |
| 123 | + return paused; |
117 | 124 | } |
118 | | - return paused; |
119 | 125 | } |
120 | 126 |
|
121 | 127 | @Override |
122 | 128 | public Map<String, Map<MetricName, ? extends Metric>> metrics() { |
123 | | - Map<String, Map<MetricName, ? extends Metric>> metrics = new HashMap<>(); |
124 | | - for (KafkaMessageListenerContainer<K, V> container : this.containers) { |
125 | | - metrics.putAll(container.metrics()); |
| 129 | + synchronized (this.lifecycleMonitor) { |
| 130 | + Map<String, Map<MetricName, ? extends Metric>> metrics = new HashMap<>(); |
| 131 | + for (KafkaMessageListenerContainer<K, V> container : this.containers) { |
| 132 | + metrics.putAll(container.metrics()); |
| 133 | + } |
| 134 | + return Collections.unmodifiableMap(metrics); |
126 | 135 | } |
127 | | - return Collections.unmodifiableMap(metrics); |
128 | 136 | } |
129 | 137 |
|
130 | 138 | /* |
@@ -231,14 +239,18 @@ protected void doStop(final Runnable callback) { |
231 | 239 |
|
232 | 240 | @Override |
233 | 241 | public void pause() { |
234 | | - super.pause(); |
235 | | - this.containers.forEach(AbstractMessageListenerContainer::pause); |
| 242 | + synchronized (this.lifecycleMonitor) { |
| 243 | + super.pause(); |
| 244 | + this.containers.forEach(AbstractMessageListenerContainer::pause); |
| 245 | + } |
236 | 246 | } |
237 | 247 |
|
238 | 248 | @Override |
239 | 249 | public void resume() { |
240 | | - super.resume(); |
241 | | - this.containers.forEach(AbstractMessageListenerContainer::resume); |
| 250 | + synchronized (this.lifecycleMonitor) { |
| 251 | + super.resume(); |
| 252 | + this.containers.forEach(AbstractMessageListenerContainer::resume); |
| 253 | + } |
242 | 254 | } |
243 | 255 |
|
244 | 256 | @Override |
|
0 commit comments