GH-3371: Fence child containers after ConcurrentContainer stops

Fixes: #3371

Containers are not restricted from starting after ConcurrentContainer stopped or restarted. These changes would fix this issue.

* Enhancements to fence container after ConcurrentContainer stops

(cherry picked from commit 20696f2390)
This commit is contained in:
LokeshAlamuri
2024-07-31 22:28:38 +05:30
committed by Spring Builds
parent 1a5d62d277
commit 10f9726122
3 changed files with 14 additions and 0 deletions

View File

@@ -124,6 +124,8 @@ public abstract class AbstractMessageListenerContainer<K, V>
private volatile boolean running = false;
private volatile boolean fenced = false;
private volatile boolean paused;
private volatile boolean stoppedNormally = true;
@@ -275,6 +277,10 @@ public abstract class AbstractMessageListenerContainer<K, V>
return this.running;
}
protected void setFenced(boolean fenced) {
this.fenced = fenced;
}
@Deprecated(since = "3.2", forRemoval = true)
protected boolean isPaused() {
return this.paused;
@@ -509,6 +515,7 @@ public abstract class AbstractMessageListenerContainer<K, V>
if (!isRunning()) {
Assert.state(this.containerProperties.getMessageListener() instanceof GenericMessageListener,
() -> "A " + GenericMessageListener.class.getName() + " implementation must be provided");
Assert.state(!this.fenced, "Container Fenced. It is not allowed to start.");
doStart();
}
}

View File

@@ -352,6 +352,7 @@ public class ConcurrentMessageListenerContainer<K, V> extends AbstractMessageLis
}
}
for (KafkaMessageListenerContainer<K, V> container : this.containers) {
container.setFenced(true);
if (container.isRunning()) {
if (normal) {
container.stop(() -> {

View File

@@ -17,6 +17,7 @@
package org.springframework.kafka.listener;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
@@ -200,6 +201,7 @@ public class ConcurrentMessageListenerContainerTests {
assertThat(container.metrics()).isNotNull();
Set<KafkaMessageListenerContainer<Integer, String>> children = new HashSet<>(containers);
assertThat(container.isInExpectedState()).isTrue();
MessageListenerContainer childContainer = container.getContainers().get(0);
container.getContainers().get(0).stopAbnormally(() -> { });
assertThat(container.isInExpectedState()).isFalse();
container.getContainers().get(0).start();
@@ -222,6 +224,10 @@ public class ConcurrentMessageListenerContainerTests {
});
assertThat(overrides.get().getProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG)).isNull();
this.logger.info("Stop auto");
assertThat(childContainer.isRunning()).isFalse();
assertThat(container.isRunning()).isFalse();
// Fenced container. Throws exception
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> childContainer.start());
}
@Test