Skip to content

Commit 982b789

Browse files
committed
[GH-3208] Feat: Allow overriding SmartLifecycle phase in RedisMessageListenerContainer.
Signed-off-by: Su Ko <[email protected]>
1 parent 1eab4d3 commit 982b789

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/main/java/org/springframework/data/redis/listener/RedisMessageListenerContainer.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
* @author Mark Paluch
104104
* @author John Blum
105105
* @author Seongjun Lee
106+
* @author Su Ko
106107
* @see MessageListener
107108
* @see SubscriptionListener
108109
*/
@@ -168,6 +169,8 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
168169

169170
private @Nullable Subscriber subscriber;
170171

172+
private int phase = Integer.MAX_VALUE;
173+
171174
/**
172175
* Set an ErrorHandler to be invoked in case of any uncaught exceptions thrown while processing a Message. By default,
173176
* there will be <b>no</b> ErrorHandler so that error-level logging is the only result.
@@ -618,6 +621,22 @@ public void removeMessageListener(MessageListener listener) {
618621
removeMessageListener(listener, Collections.emptySet());
619622
}
620623

624+
@Override
625+
public int getPhase() {
626+
return this.phase;
627+
}
628+
629+
/**
630+
* Specify the lifecycle phase for this container.
631+
* Lower values start earlier and stop later.
632+
* The default is {@code Integer.MAX_VALUE}.
633+
*
634+
* @see SmartLifecycle#getPhase()
635+
*/
636+
public void setPhase(int phase) {
637+
this.phase = phase;
638+
}
639+
621640
private void initMapping(Map<? extends MessageListener, Collection<? extends Topic>> listeners) {
622641

623642
// stop the listener if currently running

src/test/java/org/springframework/data/redis/listener/RedisMessageListenerContainerUnitTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,15 @@ void shouldRemoveAllListenersWhenListenerIsNull() {
239239

240240
assertThatNoException().isThrownBy(() -> container.removeMessageListener(null, Collections.singletonList(topic)));
241241
}
242+
243+
@Test // GH-3208
244+
void defaultPhaseShouldBeMaxValue() {
245+
assertThat(container.getPhase()).isEqualTo(Integer.MAX_VALUE);
246+
}
247+
248+
@Test // GH-3208
249+
void shouldApplyConfiguredPhase() {
250+
container.setPhase(3208);
251+
assertThat(container.getPhase()).isEqualTo(3208);
252+
}
242253
}

0 commit comments

Comments
 (0)