Skip to content

Commit 2916b4b

Browse files
committed
Gracefully allow clients to exit when the server-side closes the websocket
- Use tomcat snapshot version that fixes the issue with a lingering thread - Use daemon thread for the heartbeat timer Also fixed an issue where the session may have been closed by the time the message handler is added by moving the code that add the handler in Endpoint.onOpen implementation.
1 parent a527671 commit 2916b4b

4 files changed

Lines changed: 9 additions & 5 deletions

File tree

‎cloudfoundry-client-lib/pom.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<properties>
2929
<spring.framework.version>3.2.2.RELEASE</spring.framework.version>
3030
<spring.security.oauth.version>1.0.0.RELEASE</spring.security.oauth.version>
31+
<tomcat.version>8.0-SNAPSHOT</tomcat.version>
3132
<jetty.version>8.1.12.v20130726</jetty.version>
3233
<byteman.version>2.1.3</byteman.version>
3334
</properties>
@@ -243,7 +244,7 @@
243244
<dependency>
244245
<groupId>org.apache.tomcat.embed</groupId>
245246
<artifactId>tomcat-embed-websocket</artifactId>
246-
<version>8.0-SNAPSHOT</version>
247+
<version>${tomcat.version}</version>
247248
</dependency>
248249
<dependency>
249250
<groupId>org.apache.tomcat</groupId>

‎cloudfoundry-client-lib/src/main/java/org/cloudfoundry/client/lib/rest/CloudControllerClientImpl.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,6 @@ public void beforeRequest(Map<String,List<String>> headers) {
16061606
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
16071607
ClientEndpointConfig config = ClientEndpointConfig.Builder.create().configurator(configurator).build();
16081608
Session session = container.connectToServer(new LoggregatorEndpoint(listener), config, loggregatorUri);
1609-
session.addMessageHandler(new LoggregatorMessageHandler(listener));
16101609
return new StreamingLogTokenImpl(session);
16111610
} catch (DeploymentException e) {
16121611
throw new CloudOperationException(e);

‎cloudfoundry-client-lib/src/main/java/org/cloudfoundry/client/lib/rest/LoggregatorEndpoint.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ public LoggregatorEndpoint(ApplicationLogListener listener) {
1717

1818
@Override
1919
public void onOpen(Session session, EndpointConfig config) {
20+
session.addMessageHandler(new LoggregatorMessageHandler(listener));
2021
}
2122

2223
@Override
2324
public void onClose(Session session, CloseReason closeReason) {
24-
if (closeReason.getCloseCode() == CloseReason.CloseCodes.NORMAL_CLOSURE) {
25+
if (closeReason.getCloseCode() == CloseReason.CloseCodes.NORMAL_CLOSURE
26+
|| closeReason.getCloseCode() == CloseReason.CloseCodes.GOING_AWAY) {
2527
listener.onComplete();
2628
} else {
2729
listener.onError(new CloudOperationException("Loggregrator connection closed unexpectedly " + closeReason));

‎cloudfoundry-client-lib/src/main/java/org/cloudfoundry/client/lib/rest/StreamingLogTokenImpl.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
public class StreamingLogTokenImpl implements StreamingLogToken {
1111
private static long keepAliveTime = 25000; // 25 seconds to match the go client
1212

13-
private Timer keepAliveTimer = new Timer();
13+
private Timer keepAliveTimer = new Timer(true);
1414

1515
private Session session;
16-
16+
1717
public StreamingLogTokenImpl(Session session) {
1818
this.session = session;
1919

@@ -29,6 +29,8 @@ private class KeepAliveTimerTask extends TimerTask {
2929
public void run() {
3030
if (session.isOpen()) {
3131
session.getAsyncRemote().sendText("keep alive");
32+
} else {
33+
keepAliveTimer.cancel();
3234
}
3335
}
3436
}

0 commit comments

Comments
 (0)