From 2b1ff4c5db781a3d0b5b83fab44e47cd7e64dbd6 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Sun, 29 Jun 2014 17:00:24 -0400 Subject: [PATCH] Fix condition in SubProtocolWebSocketHandler Issue: SPR-11884 --- .../socket/messaging/SubProtocolWebSocketHandler.java | 10 +++------- .../messaging/SubProtocolWebSocketHandlerTests.java | 1 + 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java index 4ffb57d509..534681fc09 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java @@ -70,7 +70,7 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler, * connection isn't doing well (proxy issue, slow network?) and can be closed. * @see #checkSessions() */ - private final int TIME_TO_FIRST_MESSAGE = 60 * 1000; + private static final int TIME_TO_FIRST_MESSAGE = 60 * 1000; private final Log logger = LogFactory.getLog(SubProtocolWebSocketHandler.class); @@ -298,10 +298,6 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler, if (holder != null) { holder.setHasHandledMessages(); } - else { - // Should never happen - throw new IllegalStateException("Session not found: " + session); - } checkSessions(); } @@ -366,7 +362,7 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler, */ private void checkSessions() throws IOException { long currentTime = System.currentTimeMillis(); - if (!isRunning() && currentTime - this.lastSessionCheckTime < TIME_TO_FIRST_MESSAGE) { + if (!isRunning() || (currentTime - this.lastSessionCheckTime < TIME_TO_FIRST_MESSAGE)) { return; } if (this.sessionCheckLock.tryLock()) { @@ -376,7 +372,7 @@ public class SubProtocolWebSocketHandler implements WebSocketHandler, continue; } long timeSinceCreated = currentTime - holder.getCreateTime(); - if (holder.hasHandledMessages() || timeSinceCreated < TIME_TO_FIRST_MESSAGE) { + if (timeSinceCreated < TIME_TO_FIRST_MESSAGE) { continue; } WebSocketSession session = holder.getSession(); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandlerTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandlerTests.java index 8f315b44b8..66230c19ca 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandlerTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandlerTests.java @@ -165,6 +165,7 @@ public class SubProtocolWebSocketHandlerTests { new DirectFieldAccessor(sessions.get("id1")).setPropertyValue("createTime", sixtyOneSecondsAgo); new DirectFieldAccessor(sessions.get("id2")).setPropertyValue("createTime", sixtyOneSecondsAgo); + this.webSocketHandler.start(); this.webSocketHandler.handleMessage(session1, new TextMessage("foo")); assertTrue(session1.isOpen()); -- GitLab