INT-4028: Fix ClientWebSocketContainer start/stop
JIRA: https://jira.spring.io/browse/INT-4028 Previously the `ClientWebSocketContainer` after the connection failure couldn't be restored even after `stop()/start()` because the `openConnectionException` property hasn't been clear on `stop()` * Add `openConnectionException = null` to `stopInternal()` logic * Also clear `openConnectionException` and `clientSession` on `start()` * Plus add recovery (restart) logic into the `getSession()` if the `!clientSession.isOpen()` **Cherry-pick to 4.2.x** Fix race condition around `start/stop` The new `start/stop` logic brakes the in-flight connection. * Introduce one more `connecting` flag to indicate that we are in connecting process. * Absorb the a new test for `ClientWebSocketContainer` with the existing one. Looks like there is some extra session close in between. * Adjust timeouts in the `StompIntegrationTests` to 20 secs. Add `org.apache.catalina` category for tracing logs
This commit is contained in:
committed by
Gary Russell
parent
06a1d503be
commit
39ee4ea3b6
@@ -54,7 +54,7 @@ public final class ClientWebSocketContainer extends IntegrationWebSocketContaine
|
||||
|
||||
private final WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
|
||||
|
||||
private final ConnectionManagerSupport connectionManager;
|
||||
private final IntegrationWebSocketConnectionManager connectionManager;
|
||||
|
||||
private volatile CountDownLatch connectionLatch;
|
||||
|
||||
@@ -64,6 +64,8 @@ public final class ClientWebSocketContainer extends IntegrationWebSocketContaine
|
||||
|
||||
private volatile int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
|
||||
|
||||
private volatile boolean connecting;
|
||||
|
||||
public ClientWebSocketContainer(WebSocketClient client, String uriTemplate, Object... uriVariables) {
|
||||
Assert.notNull(client, "'client' must not be null");
|
||||
this.connectionManager = new IntegrationWebSocketConnectionManager(client, uriTemplate, uriVariables);
|
||||
@@ -108,18 +110,31 @@ public final class ClientWebSocketContainer extends IntegrationWebSocketContaine
|
||||
@Override
|
||||
public WebSocketSession getSession(String sessionId) {
|
||||
if (isRunning()) {
|
||||
if (!isConnected() && !this.connecting) {
|
||||
stop();
|
||||
start();
|
||||
}
|
||||
|
||||
try {
|
||||
this.connectionLatch.await(this.connectionTimeout, TimeUnit.SECONDS);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
logger.error("'clientSession' has not been established during 'openConnection'");
|
||||
}
|
||||
this.connecting = false;
|
||||
}
|
||||
if (this.openConnectionException != null) {
|
||||
throw new IllegalStateException(this.openConnectionException);
|
||||
|
||||
try {
|
||||
if (this.openConnectionException != null) {
|
||||
throw new IllegalStateException(this.openConnectionException);
|
||||
}
|
||||
Assert.state(this.clientSession != null,
|
||||
"'clientSession' has not been established. Consider to 'start' this container.");
|
||||
}
|
||||
catch (IllegalStateException e) {
|
||||
stop();
|
||||
throw e;
|
||||
}
|
||||
Assert.state(this.clientSession != null,
|
||||
"'clientSession' has not been established. Consider to 'start' this container.");
|
||||
return this.clientSession;
|
||||
}
|
||||
|
||||
@@ -131,6 +146,15 @@ public final class ClientWebSocketContainer extends IntegrationWebSocketContaine
|
||||
this.connectionManager.setPhase(phase);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return {@code true} if the {@link #clientSession} is opened.
|
||||
* @return the {@link WebSocketSession#isOpen()} state.
|
||||
* @since 4.2.6
|
||||
*/
|
||||
public boolean isConnected() {
|
||||
return this.connectionManager.isConnected();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAutoStartup() {
|
||||
return this.connectionManager.isAutoStartup();
|
||||
@@ -149,6 +173,8 @@ public final class ClientWebSocketContainer extends IntegrationWebSocketContaine
|
||||
@Override
|
||||
public synchronized void start() {
|
||||
if (!isRunning()) {
|
||||
this.clientSession = null;
|
||||
this.openConnectionException = null;
|
||||
this.connectionLatch = new CountDownLatch(1);
|
||||
this.connectionManager.start();
|
||||
}
|
||||
@@ -178,7 +204,8 @@ public final class ClientWebSocketContainer extends IntegrationWebSocketContaine
|
||||
|
||||
private final boolean syncClientLifecycle;
|
||||
|
||||
private IntegrationWebSocketConnectionManager(WebSocketClient client, String uriTemplate, Object... uriVariables) {
|
||||
private IntegrationWebSocketConnectionManager(WebSocketClient client, String uriTemplate,
|
||||
Object... uriVariables) {
|
||||
super(uriTemplate, uriVariables);
|
||||
this.client = client;
|
||||
this.syncClientLifecycle = ((client instanceof Lifecycle) && !((Lifecycle) client).isRunning());
|
||||
@@ -189,6 +216,7 @@ public final class ClientWebSocketContainer extends IntegrationWebSocketContaine
|
||||
if (this.syncClientLifecycle) {
|
||||
((Lifecycle) this.client).start();
|
||||
}
|
||||
ClientWebSocketContainer.this.connecting = true;
|
||||
super.startInternal();
|
||||
}
|
||||
|
||||
@@ -202,14 +230,14 @@ public final class ClientWebSocketContainer extends IntegrationWebSocketContaine
|
||||
}
|
||||
finally {
|
||||
ClientWebSocketContainer.this.clientSession = null;
|
||||
ClientWebSocketContainer.this.openConnectionException = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void openConnection() {
|
||||
|
||||
logger.info("Connecting to WebSocket at " + getUri());
|
||||
ClientWebSocketContainer.this.headers.setSecWebSocketProtocol(ClientWebSocketContainer.this.getSubProtocols());
|
||||
ClientWebSocketContainer.this.headers.setSecWebSocketProtocol(getSubProtocols());
|
||||
ListenableFuture<WebSocketSession> future =
|
||||
this.client.doHandshake(ClientWebSocketContainer.this.webSocketHandler,
|
||||
ClientWebSocketContainer.this.headers, getUri());
|
||||
@@ -229,6 +257,7 @@ public final class ClientWebSocketContainer extends IntegrationWebSocketContaine
|
||||
ClientWebSocketContainer.this.openConnectionException = t;
|
||||
ClientWebSocketContainer.this.connectionLatch.countDown();
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user