Skip to content

Commit 680d186

Browse files
committed
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 spring-projects#4403 Signed-off-by: Junhyeok Lee <[email protected]>
1 parent e368a42 commit 680d186

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
*
4444
* @author Christoph Strobl
4545
* @author Mark Paluch
46+
* @author Junhyeok Lee
4647
* @since 2.1
4748
*/
4849
public class DefaultMessageListenerContainer implements MessageListenerContainer {
@@ -62,6 +63,7 @@ public class DefaultMessageListenerContainer implements MessageListenerContainer
6263
private final Lock subscriptionWrite = Lock.of(subscriptionMonitor.writeLock());
6364

6465
private boolean running = false;
66+
private boolean autoStartup = true;
6567

6668
/**
6769
* Create a new {@link DefaultMessageListenerContainer}.
@@ -105,7 +107,16 @@ public DefaultMessageListenerContainer(MongoTemplate template, Executor taskExec
105107

106108
@Override
107109
public boolean isAutoStartup() {
108-
return false;
110+
return this.autoStartup;
111+
}
112+
113+
/**
114+
* Set whether to auto-start this container.
115+
* <p>Default is {@code true}.
116+
* @param autoStartup {@code true} to auto-start.
117+
*/
118+
public void setAutoStartup(boolean autoStartup) {
119+
this.autoStartup = autoStartup;
109120
}
110121

111122
@Override

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

+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* Unit tests for {@link DefaultMessageListenerContainer}.
3737
*
3838
* @author Christoph Strobl
39+
* @author Junhyeok Lee
3940
*/
4041
@ExtendWith(MockitoExtension.class)
4142
class DefaultMessageListenerContainerUnitTests {
@@ -80,6 +81,17 @@ void removeSubscriptionWhileRunning() throws Throwable {
8081
runOnce(new RemoveSubscriptionWhileRunning(container));
8182
}
8283

84+
@Test // GH-4403
85+
void shouldHaveAutoStartupEnabledByDefault() {
86+
assertThat(container.isAutoStartup()).isTrue();
87+
}
88+
89+
@Test // GH-4403
90+
void shouldAllowDisablingAutoStartup() {
91+
container.setAutoStartup(false);
92+
assertThat(container.isAutoStartup()).isFalse();
93+
}
94+
8395
private static class RemoveSubscriptionWhileRunning extends MultithreadedTestCase {
8496

8597
DefaultMessageListenerContainer container;

0 commit comments

Comments
 (0)