Browse Source

Enable auto-startup for `DefaultMessageListenerContainer`.

This change modifies DefaultMessageListenerContainer to automatically start when the Spring application context is initialized, by default.

The 'autoStartup' property defaults to true. A setter 'setAutoStartup(boolean)' has been added to allow users to disable this behavior if needed.

Closes #4403
Original pull request: #4976

Signed-off-by: Junhyeok Lee <jhl221123@naver.com>
issue/4985
Junhyeok Lee 7 months ago committed by Mark Paluch
parent
commit
431fc7f886
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 13
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/messaging/DefaultMessageListenerContainer.java
  2. 12
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/messaging/DefaultMessageListenerContainerUnitTests.java

13
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/messaging/DefaultMessageListenerContainer.java

@ -43,6 +43,7 @@ import org.springframework.util.ObjectUtils; @@ -43,6 +43,7 @@ import org.springframework.util.ObjectUtils;
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Junhyeok Lee
* @since 2.1
*/
public class DefaultMessageListenerContainer implements MessageListenerContainer {
@ -62,6 +63,7 @@ public class DefaultMessageListenerContainer implements MessageListenerContainer @@ -62,6 +63,7 @@ public class DefaultMessageListenerContainer implements MessageListenerContainer
private final Lock subscriptionWrite = Lock.of(subscriptionMonitor.writeLock());
private boolean running = false;
private boolean autoStartup = true;
/**
* Create a new {@link DefaultMessageListenerContainer}.
@ -105,7 +107,16 @@ public class DefaultMessageListenerContainer implements MessageListenerContainer @@ -105,7 +107,16 @@ public class DefaultMessageListenerContainer implements MessageListenerContainer
@Override
public boolean isAutoStartup() {
return false;
return this.autoStartup;
}
/**
* Set whether to auto-start this container.
* <p>Default is {@code true}.
* @param autoStartup {@code true} to auto-start.
*/
public void setAutoStartup(boolean autoStartup) {
this.autoStartup = autoStartup;
}
@Override

12
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/messaging/DefaultMessageListenerContainerUnitTests.java

@ -36,6 +36,7 @@ import org.springframework.util.ErrorHandler; @@ -36,6 +36,7 @@ import org.springframework.util.ErrorHandler;
* Unit tests for {@link DefaultMessageListenerContainer}.
*
* @author Christoph Strobl
* @author Junhyeok Lee
*/
@ExtendWith(MockitoExtension.class)
class DefaultMessageListenerContainerUnitTests {
@ -80,6 +81,17 @@ class DefaultMessageListenerContainerUnitTests { @@ -80,6 +81,17 @@ class DefaultMessageListenerContainerUnitTests {
runOnce(new RemoveSubscriptionWhileRunning(container));
}
@Test // GH-4403
void shouldHaveAutoStartupEnabledByDefault() {
assertThat(container.isAutoStartup()).isTrue();
}
@Test // GH-4403
void shouldAllowDisablingAutoStartup() {
container.setAutoStartup(false);
assertThat(container.isAutoStartup()).isFalse();
}
private static class RemoveSubscriptionWhileRunning extends MultithreadedTestCase {
DefaultMessageListenerContainer container;

Loading…
Cancel
Save