@@ -163,64 +163,11 @@ public class RedisMessageListenerContainer implements InitializingBean, Disposab
163
163
164
164
private volatile CompletableFuture <Void > unsubscribeFuture = new CompletableFuture <>();
165
165
166
- /**
167
- * Container listening state.
168
- */
169
- static class State {
170
-
171
- private final boolean prepareListening ;
172
- private final boolean listening ;
173
-
174
- private State (boolean prepareListening , boolean listening ) {
175
- this .prepareListening = prepareListening ;
176
- this .listening = listening ;
177
- }
178
-
179
- /**
180
- * Initial state. Next state is {@link #prepareListening()}.
181
- */
182
- public static State notListening () {
183
- return new State (false , false );
184
- }
185
-
186
- /**
187
- * Prepare listening after {@link #notListening()}. Next states are either {@link #notListening()} upon failure or
188
- * {@link #listening()}.
189
- */
190
- public static State prepareListening () {
191
- return new State (true , false );
192
- }
193
-
194
- /**
195
- * Active listening state after {@link #prepareListening()}. Next is {@link #prepareUnsubscribe()}.
196
- */
197
- public static State listening () {
198
- return new State (true , true );
199
- }
200
-
201
- /**
202
- * Prepare unsubscribe after {@link #listening()}. Next state is {@link #notListening()}.
203
- */
204
- public static State prepareUnsubscribe () {
205
- return new State (false , true );
206
- }
207
-
208
- private boolean isListenerActivated () {
209
- return isListening () || isPrepareListening ();
210
- }
211
-
212
- public boolean isListening () {
213
- return listening ;
214
- }
215
-
216
- public boolean isPrepareListening () {
217
- return prepareListening ;
218
- }
219
- }
220
-
221
166
@ Override
222
167
public void afterPropertiesSet () {
223
168
169
+ Assert .state (!afterPropertiesSet , "Container already initialized." );
170
+
224
171
if (this .connectionFactory == null ) {
225
172
throw new IllegalArgumentException ("RedisConnectionFactory is not set" );
226
173
}
@@ -234,6 +181,7 @@ public void afterPropertiesSet() {
234
181
subscriptionExecutor = taskExecutor ;
235
182
}
236
183
184
+
237
185
this .subscriber = createSubscriber (connectionFactory , this .subscriptionExecutor );
238
186
239
187
afterPropertiesSet = true ;
@@ -268,7 +216,7 @@ public void destroy() throws Exception {
268
216
((DisposableBean ) taskExecutor ).destroy ();
269
217
270
218
if (logger .isDebugEnabled ()) {
271
- logger .debug ("Stopped internally-managed task executor" );
219
+ logger .debug ("Stopped internally-managed task executor. " );
272
220
}
273
221
}
274
222
}
@@ -323,7 +271,7 @@ private void lazyListen() {
323
271
} catch (ExecutionException e ) {
324
272
throw new CompletionException (e .getCause ());
325
273
} catch (TimeoutException e ) {
326
- throw new IllegalStateException ("Subscription registration timeout exceeded" , e );
274
+ throw new IllegalStateException ("Subscription registration timeout exceeded. " , e );
327
275
}
328
276
}
329
277
@@ -333,7 +281,7 @@ private void lazyListen() {
333
281
private CompletableFuture <Void > lazyListen (BackOffExecution backOffExecution ) {
334
282
335
283
if (!hasTopics ()) {
336
- logger .debug ("Postpone listening for Redis messages until actual listeners are added" );
284
+ logger .debug ("Postpone listening for Redis messages until actual listeners are added. " );
337
285
return CompletableFuture .completedFuture (null );
338
286
}
339
287
@@ -367,10 +315,10 @@ private boolean doSubscribe(BackOffExecution backOffExecution) {
367
315
listenFuture .whenComplete ((unused , throwable ) -> {
368
316
369
317
if (throwable == null ) {
370
- logger .debug ("RedisMessageListenerContainer listeners registered successfully" );
318
+ logger .debug ("RedisMessageListenerContainer listeners registered successfully. " );
371
319
this .state .set (State .listening ());
372
320
} else {
373
- logger .debug ("Failed to start RedisMessageListenerContainer listeners" , throwable );
321
+ logger .debug ("Failed to start RedisMessageListenerContainer listeners. " , throwable );
374
322
this .state .set (State .notListening ());
375
323
}
376
324
@@ -382,7 +330,7 @@ private boolean doSubscribe(BackOffExecution backOffExecution) {
382
330
}
383
331
});
384
332
385
- logger .debug ("Subscribing to topics for RedisMessageListenerContainer" );
333
+ logger .debug ("Subscribing to topics for RedisMessageListenerContainer. " );
386
334
387
335
return true ;
388
336
}
@@ -422,7 +370,7 @@ public void stop(Runnable callback) {
422
370
stopListening ();
423
371
424
372
if (logger .isDebugEnabled ()) {
425
- logger .debug ("Stopped RedisMessageListenerContainer" );
373
+ logger .debug ("Stopped RedisMessageListenerContainer. " );
426
374
}
427
375
428
376
callback .run ();
@@ -459,7 +407,7 @@ private boolean doUnsubscribe() {
459
407
this .unsubscribeFuture = new CompletableFuture <>();
460
408
461
409
if (logger .isDebugEnabled ()) {
462
- logger .debug ("Stopped RedisMessageListenerContainer " );
410
+ logger .debug ("Stopped listening. " );
463
411
}
464
412
465
413
return true ;
@@ -831,7 +779,7 @@ public void setRecoveryInterval(long recoveryInterval) {
831
779
*
832
780
* @see #handleSubscriptionException
833
781
* @see #setRecoveryInterval(long)
834
- * @since 3.0
782
+ * @since 2.7
835
783
*/
836
784
public void setRecoveryBackoff (BackOff recoveryInterval ) {
837
785
@@ -928,8 +876,7 @@ protected void handleSubscriptionException(CompletableFuture<Void> future, BackO
928
876
long recoveryInterval = backOffExecution .nextBackOff ();
929
877
930
878
if (recoveryInterval != BackOffExecution .STOP ) {
931
- logger .error (
932
- "Connection failure occurred:" + ex + ". Restarting subscription task after " + recoveryInterval + " ms" );
879
+ logger .error (String .format ("Connection failure occurred: %s. Restarting subscription task after %s ms." , ex , recoveryInterval ));
933
880
}
934
881
935
882
return recoveryInterval ;
@@ -1011,14 +958,6 @@ private void dispatchSubscriptionNotification(Collection<MessageListener> listen
1011
958
}
1012
959
}
1013
960
1014
- /**
1015
- * Represents an operation that accepts three input arguments {@link SubscriptionListener},
1016
- * {@code channel or pattern}, and {@code count} and returns no result.
1017
- */
1018
- interface SubscriptionConsumer {
1019
- void accept (SubscriptionListener listener , byte [] channelOrPattern , long count );
1020
- }
1021
-
1022
961
private void dispatchMessage (Collection <MessageListener > listeners , Message message , @ Nullable byte [] pattern ) {
1023
962
1024
963
byte [] source = (pattern != null ? pattern .clone () : message .getChannel ());
@@ -1057,6 +996,73 @@ private byte[] serialize(Topic topic) {
1057
996
return serializer .serialize (topic .getTopic ());
1058
997
}
1059
998
999
+ /**
1000
+ * Represents an operation that accepts three input arguments {@link SubscriptionListener},
1001
+ * {@code channel or pattern}, and {@code count} and returns no result.
1002
+ */
1003
+ interface SubscriptionConsumer {
1004
+ void accept (SubscriptionListener listener , byte [] channelOrPattern , long count );
1005
+ }
1006
+
1007
+ /**
1008
+ * Container listening state.
1009
+ *
1010
+ * @author Mark Paluch
1011
+ * @since 2.7
1012
+ */
1013
+ static class State {
1014
+
1015
+ private final boolean prepareListening ;
1016
+ private final boolean listening ;
1017
+
1018
+ private State (boolean prepareListening , boolean listening ) {
1019
+
1020
+ this .prepareListening = prepareListening ;
1021
+ this .listening = listening ;
1022
+ }
1023
+
1024
+ /**
1025
+ * Initial state. Next state is {@link #prepareListening()}.
1026
+ */
1027
+ static State notListening () {
1028
+ return new State (false , false );
1029
+ }
1030
+
1031
+ /**
1032
+ * Prepare listening after {@link #notListening()}. Next states are either {@link #notListening()} upon failure or
1033
+ * {@link #listening()}.
1034
+ */
1035
+ static State prepareListening () {
1036
+ return new State (true , false );
1037
+ }
1038
+
1039
+ /**
1040
+ * Active listening state after {@link #prepareListening()}. Next is {@link #prepareUnsubscribe()}.
1041
+ */
1042
+ static State listening () {
1043
+ return new State (true , true );
1044
+ }
1045
+
1046
+ /**
1047
+ * Prepare unsubscribe after {@link #listening()}. Next state is {@link #notListening()}.
1048
+ */
1049
+ static State prepareUnsubscribe () {
1050
+ return new State (false , true );
1051
+ }
1052
+
1053
+ private boolean isListenerActivated () {
1054
+ return isListening () || isPrepareListening ();
1055
+ }
1056
+
1057
+ public boolean isListening () {
1058
+ return listening ;
1059
+ }
1060
+
1061
+ public boolean isPrepareListening () {
1062
+ return prepareListening ;
1063
+ }
1064
+ }
1065
+
1060
1066
/**
1061
1067
* Actual message dispatcher/multiplexer.
1062
1068
*
@@ -1119,7 +1125,7 @@ public void onPatternUnsubscribed(byte[] pattern, long count) {
1119
1125
* actual listeners without blocking the event loop.
1120
1126
*
1121
1127
* @author Mark Paluch
1122
- * @since 3.0
1128
+ * @since 2.7
1123
1129
*/
1124
1130
class Subscriber {
1125
1131
@@ -1324,7 +1330,7 @@ private void doWithSubscription(byte[][] data, BiConsumer<Subscription, byte[][]
1324
1330
* Blocking variant of a subscriber for connectors that block within the (p)subscribe method.
1325
1331
*
1326
1332
* @author Mark Paluch
1327
- * @since 3.0
1333
+ * @since 2.7
1328
1334
*/
1329
1335
class BlockingSubscriber extends Subscriber {
1330
1336
0 commit comments