@@ -265,7 +265,7 @@ private void lazyListen() {
265
265
}
266
266
267
267
try {
268
- futureToAwait .get (getMaxSubscriptionRegistrationWaitingTime (), TimeUnit .SECONDS );
268
+ futureToAwait .get (getMaxSubscriptionRegistrationWaitingTime (), TimeUnit .MILLISECONDS );
269
269
} catch (InterruptedException e ) {
270
270
Thread .currentThread ().interrupt ();
271
271
} catch (ExecutionException e ) {
@@ -391,15 +391,13 @@ private boolean doUnsubscribe() {
391
391
return true ;
392
392
}
393
393
394
- try {
395
- listenFuture .join ();
396
- } catch (Exception e ) {
397
- // ignore, just await completion here.
398
- }
394
+ awaitRegistrationTime (listenFuture );
399
395
400
396
if (this .state .compareAndSet (state , State .prepareUnsubscribe ())) {
401
397
402
- getRequiredSubscriber ().cancel ();
398
+ getRequiredSubscriber ().unsubscribeAll ();
399
+
400
+ awaitRegistrationTime (this .unsubscribeFuture );
403
401
404
402
this .state .set (State .notListening ());
405
403
@@ -416,6 +414,16 @@ private boolean doUnsubscribe() {
416
414
}
417
415
}
418
416
417
+ private void awaitRegistrationTime (CompletableFuture <Void > future ) {
418
+ try {
419
+ future .get (getMaxSubscriptionRegistrationWaitingTime (), TimeUnit .MILLISECONDS );
420
+ } catch (InterruptedException e ) {
421
+ Thread .currentThread ().interrupt ();
422
+ } catch (ExecutionException | TimeoutException e ) {
423
+ // ignore
424
+ }
425
+ }
426
+
419
427
@ Override
420
428
public boolean isRunning () {
421
429
return this .started .get ();
@@ -876,7 +884,8 @@ protected void handleSubscriptionException(CompletableFuture<Void> future, BackO
876
884
long recoveryInterval = backOffExecution .nextBackOff ();
877
885
878
886
if (recoveryInterval != BackOffExecution .STOP ) {
879
- logger .error (String .format ("Connection failure occurred: %s. Restarting subscription task after %s ms." , ex , recoveryInterval ));
887
+ logger .error (String .format ("Connection failure occurred: %s. Restarting subscription task after %s ms." , ex ,
888
+ recoveryInterval ), ex );
880
889
}
881
890
882
891
return recoveryInterval ;
@@ -897,7 +906,9 @@ protected void handleSubscriptionException(CompletableFuture<Void> future, BackO
897
906
return ;
898
907
}
899
908
900
- logger .error ("SubscriptionTask aborted with exception:" , ex );
909
+ if (isRunning ()) { // log only if the container is still running to prevent close errors from logging
910
+ logger .error ("SubscriptionTask aborted with exception:" , ex );
911
+ }
901
912
future .completeExceptionally (ex );
902
913
}
903
914
@@ -1216,6 +1227,26 @@ void addSynchronization(SynchronizingMessageListener.SubscriptionSynchronizion s
1216
1227
this .synchronizingMessageListener .addSynchronization (synchronizer );
1217
1228
}
1218
1229
1230
+ public void unsubscribeAll () {
1231
+
1232
+ synchronized (localMonitor ) {
1233
+
1234
+ RedisConnection connection = this .connection ;
1235
+ if (connection == null ) {
1236
+ return ;
1237
+ }
1238
+
1239
+ doUnsubscribe (connection );
1240
+ }
1241
+ }
1242
+
1243
+ void doUnsubscribe (RedisConnection connection ) {
1244
+ closeSubscription (connection );
1245
+ closeConnection ();
1246
+
1247
+ unsubscribeFuture .complete (null );
1248
+ }
1249
+
1219
1250
/**
1220
1251
* Cancel all subscriptions and close the connection.
1221
1252
*/
@@ -1228,26 +1259,34 @@ public void cancel() {
1228
1259
return ;
1229
1260
}
1230
1261
1231
- if ( logger . isTraceEnabled ()) {
1232
- logger . trace ( "Cancelling Redis subscription..." );
1233
- }
1262
+ doCancel ( connection );
1263
+ }
1264
+ }
1234
1265
1235
- Subscription sub = connection .getSubscription ();
1266
+ void doCancel (RedisConnection connection ) {
1267
+ closeSubscription (connection );
1268
+ closeConnection ();
1269
+ }
1236
1270
1237
- if ( sub != null ) {
1271
+ void closeSubscription ( RedisConnection connection ) {
1238
1272
1239
- if (logger .isTraceEnabled ()) {
1240
- logger .trace ("Unsubscribing from all channels " );
1241
- }
1273
+ if (logger .isTraceEnabled ()) {
1274
+ logger .trace ("Cancelling Redis subscription... " );
1275
+ }
1242
1276
1243
- try {
1244
- sub .close ();
1245
- } catch (Exception e ) {
1246
- logger .warn ("Unable to unsubscribe from subscriptions" , e );
1247
- }
1277
+ Subscription sub = connection .getSubscription ();
1278
+
1279
+ if (sub != null ) {
1280
+
1281
+ if (logger .isTraceEnabled ()) {
1282
+ logger .trace ("Unsubscribing from all channels" );
1248
1283
}
1249
1284
1250
- closeConnection ();
1285
+ try {
1286
+ sub .close ();
1287
+ } catch (Exception e ) {
1288
+ logger .warn ("Unable to unsubscribe from subscriptions" , e );
1289
+ }
1251
1290
}
1252
1291
}
1253
1292
@@ -1324,6 +1363,7 @@ private void doWithSubscription(byte[][] data, BiConsumer<Subscription, byte[][]
1324
1363
}
1325
1364
}
1326
1365
}
1366
+
1327
1367
}
1328
1368
1329
1369
/**
@@ -1341,6 +1381,11 @@ class BlockingSubscriber extends Subscriber {
1341
1381
this .executor = executor ;
1342
1382
}
1343
1383
1384
+ @ Override
1385
+ void doUnsubscribe (RedisConnection connection ) {
1386
+ closeSubscription (connection ); // connection will be closed after exiting the doSubscribe method
1387
+ }
1388
+
1344
1389
@ Override
1345
1390
protected void eventuallyPerformSubscription (RedisConnection connection , BackOffExecution backOffExecution ,
1346
1391
CompletableFuture <Void > subscriptionDone , Collection <byte []> patterns , Collection <byte []> channels ) {
@@ -1369,6 +1414,8 @@ protected void eventuallyPerformSubscription(RedisConnection connection, BackOff
1369
1414
1370
1415
try {
1371
1416
doSubscribe (connection , patterns , initiallySubscribeToChannels );
1417
+ closeConnection ();
1418
+ unsubscribeFuture .complete (null );
1372
1419
} catch (Throwable t ) {
1373
1420
handleSubscriptionException (subscriptionDone , backOffExecution , t );
1374
1421
}
0 commit comments