3131
3232import java .util .ArrayList ;
3333import java .util .Collection ;
34- import java .util .Timer ;
35- import java .util .TimerTask ;
34+ import java .util .concurrent .Executors ;
35+ import java .util .concurrent .ScheduledExecutorService ;
36+ import java .util .concurrent .ScheduledFuture ;
37+ import java .util .concurrent .TimeUnit ;
3638
3739
3840/**
@@ -60,15 +62,15 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
6062 private boolean reuseAddr ;
6163
6264 /**
63- * Attribute for a timer allowing to check for lost connections
64- * @since 1.3.4
65+ * Attribute for a service that triggers lost connection checking
66+ * @since 1.4.1
6567 */
66- private Timer connectionLostTimer ;
68+ private ScheduledExecutorService connectionLostCheckerService ;
6769 /**
68- * Attribute for a timertask allowing to check for lost connections
69- * @since 1.3.4
70+ * Attribute for a task that checks for lost connections
71+ * @since 1.4.1
7072 */
71- private TimerTask connectionLostTimerTask ;
73+ private ScheduledFuture connectionLostCheckerFuture ;
7274
7375 /**
7476 * Attribute for the lost connection check interval
@@ -139,7 +141,7 @@ public void setConnectionLostTimeout( int connectionLostTimeout ) {
139141 */
140142 protected void stopConnectionLostTimer () {
141143 synchronized (syncConnectionLost ) {
142- if (connectionLostTimer != null || connectionLostTimerTask != null ) {
144+ if (connectionLostCheckerService != null || connectionLostCheckerFuture != null ) {
143145 this .websocketRunning = false ;
144146 log .trace ("Connection lost timer stopped" );
145147 cancelConnectionLostTimer ();
@@ -168,8 +170,8 @@ protected void startConnectionLostTimer() {
168170 */
169171 private void restartConnectionLostTimer () {
170172 cancelConnectionLostTimer ();
171- connectionLostTimer = new Timer ( "WebSocketTimer" );
172- connectionLostTimerTask = new TimerTask () {
173+ connectionLostCheckerService = Executors . newSingleThreadScheduledExecutor ( );
174+ Runnable connectionLostChecker = new Runnable () {
173175
174176 /**
175177 * Keep the connections in a separate list to not cause deadlocks
@@ -190,8 +192,8 @@ public void run() {
190192 connections .clear ();
191193 }
192194 };
193- connectionLostTimer .scheduleAtFixedRate ( connectionLostTimerTask ,1000L *connectionLostTimeout , 1000L *connectionLostTimeout );
194195
196+ connectionLostCheckerFuture = connectionLostCheckerService .scheduleAtFixedRate (connectionLostChecker , connectionLostTimeout , connectionLostTimeout , TimeUnit .SECONDS );
195197 }
196198
197199 /**
@@ -228,13 +230,13 @@ private void executeConnectionLostDetection(WebSocket webSocket, long current) {
228230 * @since 1.3.4
229231 */
230232 private void cancelConnectionLostTimer () {
231- if ( connectionLostTimer != null ) {
232- connectionLostTimer . cancel ();
233- connectionLostTimer = null ;
233+ if ( connectionLostCheckerService != null ) {
234+ connectionLostCheckerService . shutdownNow ();
235+ connectionLostCheckerService = null ;
234236 }
235- if ( connectionLostTimerTask != null ) {
236- connectionLostTimerTask .cancel ();
237- connectionLostTimerTask = null ;
237+ if ( connectionLostCheckerFuture != null ) {
238+ connectionLostCheckerFuture .cancel (false );
239+ connectionLostCheckerFuture = null ;
238240 }
239241 }
240242
0 commit comments