20
20
import java .net .URI ;
21
21
import java .nio .ByteBuffer ;
22
22
import java .time .Duration ;
23
- import java .util .ArrayList ;
24
23
import java .util .Collections ;
25
24
import java .util .List ;
26
25
import java .util .concurrent .CompletableFuture ;
@@ -383,7 +382,11 @@ private class WebSocketTcpConnectionHandlerAdapter implements BiConsumer<WebSock
383
382
384
383
private volatile long lastWriteTime = -1 ;
385
384
386
- private final List <ScheduledFuture <?>> inactivityTasks = new ArrayList <>(2 );
385
+ @ Nullable
386
+ private ScheduledFuture <?> readInactivityFuture ;
387
+
388
+ @ Nullable
389
+ private ScheduledFuture <?> writeInactivityFuture ;
387
390
388
391
public WebSocketTcpConnectionHandlerAdapter (TcpConnectionHandler <byte []> stompSession ) {
389
392
Assert .notNull (stompSession , "TcpConnectionHandler must not be null" );
@@ -430,24 +433,9 @@ public void handleTransportError(WebSocketSession session, Throwable ex) {
430
433
431
434
@ Override
432
435
public void afterConnectionClosed (WebSocketSession session , CloseStatus closeStatus ) {
433
- cancelInactivityTasks ();
434
436
this .stompSession .afterConnectionClosed ();
435
437
}
436
438
437
- private void cancelInactivityTasks () {
438
- for (ScheduledFuture <?> task : this .inactivityTasks ) {
439
- try {
440
- task .cancel (true );
441
- }
442
- catch (Throwable ex ) {
443
- // Ignore
444
- }
445
- }
446
- this .lastReadTime = -1 ;
447
- this .lastWriteTime = -1 ;
448
- this .inactivityTasks .clear ();
449
- }
450
-
451
439
@ Override
452
440
public boolean supportsPartialMessages () {
453
441
return false ;
@@ -486,7 +474,7 @@ public void onReadInactivity(final Runnable runnable, final long duration) {
486
474
Assert .state (getTaskScheduler () != null , "No TaskScheduler configured" );
487
475
this .lastReadTime = System .currentTimeMillis ();
488
476
Duration delay = Duration .ofMillis (duration / 2 );
489
- this .inactivityTasks . add ( getTaskScheduler ().scheduleWithFixedDelay (() -> {
477
+ this .readInactivityFuture = getTaskScheduler ().scheduleWithFixedDelay (() -> {
490
478
if (System .currentTimeMillis () - this .lastReadTime > duration ) {
491
479
try {
492
480
runnable .run ();
@@ -497,15 +485,15 @@ public void onReadInactivity(final Runnable runnable, final long duration) {
497
485
}
498
486
}
499
487
}
500
- }, delay )) ;
488
+ }, delay );
501
489
}
502
490
503
491
@ Override
504
492
public void onWriteInactivity (final Runnable runnable , final long duration ) {
505
493
Assert .state (getTaskScheduler () != null , "No TaskScheduler configured" );
506
494
this .lastWriteTime = System .currentTimeMillis ();
507
495
Duration delay = Duration .ofMillis (duration / 2 );
508
- this .inactivityTasks . add ( getTaskScheduler ().scheduleWithFixedDelay (() -> {
496
+ this .writeInactivityFuture = getTaskScheduler ().scheduleWithFixedDelay (() -> {
509
497
if (System .currentTimeMillis () - this .lastWriteTime > duration ) {
510
498
try {
511
499
runnable .run ();
@@ -516,11 +504,12 @@ public void onWriteInactivity(final Runnable runnable, final long duration) {
516
504
}
517
505
}
518
506
}
519
- }, delay )) ;
507
+ }, delay );
520
508
}
521
509
522
510
@ Override
523
511
public void close () {
512
+ cancelInactivityTasks ();
524
513
WebSocketSession session = this .session ;
525
514
if (session != null ) {
526
515
try {
@@ -533,6 +522,31 @@ public void close() {
533
522
}
534
523
}
535
524
}
525
+
526
+ private void cancelInactivityTasks () {
527
+ ScheduledFuture <?> readFuture = this .readInactivityFuture ;
528
+ this .readInactivityFuture = null ;
529
+ cancelFuture (readFuture );
530
+
531
+ ScheduledFuture <?> writeFuture = this .writeInactivityFuture ;
532
+ this .writeInactivityFuture = null ;
533
+ cancelFuture (writeFuture );
534
+
535
+ this .lastReadTime = -1 ;
536
+ this .lastWriteTime = -1 ;
537
+ }
538
+
539
+ private static void cancelFuture (@ Nullable ScheduledFuture <?> future ) {
540
+ if (future != null ) {
541
+ try {
542
+ future .cancel (true );
543
+ }
544
+ catch (Throwable ex ) {
545
+ // Ignore
546
+ }
547
+ }
548
+ }
549
+
536
550
}
537
551
538
552
0 commit comments