INT-4345: ServerWebSocketContainer: Fix Lifecycle
JIRA: https://jira.spring.io/browse/INT-4345 Resolves https://github.com/spring-projects/spring-integration/issues/2238 The `DefaultHandshakeHandler` doesn't implement `SmartLifecycle` (just `Lifecycle`) there it doesn't delegate its `start()` to the `RequestUpgradeStrategy`, e.g. `JettyRequestUpgradeStrategy`. On the other hand `DefaultHandshakeHandler` can be started as a dependant `Lifecycle` from some other `SmartLifecycle`, like it happens with the `WebSocketHandlerMapping` * Implement `SmartLifecycle` for the `ServerWebSocketContainer` and delegate its lifecycle to the provided `HandshakeHandler` * Fix `WebSocketInboundChannelAdapter` to properly implement `doStop()` with the propagation to the provided `webSocketContainer` * Fix deprecation warning in the `StompMessageHandlerWebSocketIntegrationTests` **Cherry-pick to 4.3.x**
This commit is contained in:
committed by
Gary Russell
parent
077b5ffffc
commit
bf1fef39db
@@ -18,6 +18,8 @@ package org.springframework.integration.websocket;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.context.Lifecycle;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -46,7 +48,8 @@ import org.springframework.web.socket.sockjs.transport.TransportHandler;
|
||||
* @author Gary Russell
|
||||
* @since 4.1
|
||||
*/
|
||||
public class ServerWebSocketContainer extends IntegrationWebSocketContainer implements WebSocketConfigurer {
|
||||
public class ServerWebSocketContainer extends IntegrationWebSocketContainer
|
||||
implements WebSocketConfigurer, SmartLifecycle {
|
||||
|
||||
private final String[] paths;
|
||||
|
||||
@@ -60,6 +63,10 @@ public class ServerWebSocketContainer extends IntegrationWebSocketContainer impl
|
||||
|
||||
private String[] origins;
|
||||
|
||||
private boolean autoStartup = true;
|
||||
|
||||
private int phase = 0;
|
||||
|
||||
public ServerWebSocketContainer(String... paths) {
|
||||
this.paths = paths;
|
||||
}
|
||||
@@ -178,6 +185,51 @@ public class ServerWebSocketContainer extends IntegrationWebSocketContainer impl
|
||||
|
||||
}
|
||||
|
||||
public void setAutoStartup(boolean autoStartup) {
|
||||
this.autoStartup = autoStartup;
|
||||
}
|
||||
|
||||
public void setPhase(int phase) {
|
||||
this.phase = phase;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAutoStartup() {
|
||||
return this.autoStartup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPhase() {
|
||||
return this.phase;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return this.handshakeHandler instanceof Lifecycle && ((Lifecycle) this.handshakeHandler).isRunning();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void start() {
|
||||
if (this.handshakeHandler instanceof Lifecycle && !isRunning()) {
|
||||
((Lifecycle) this.handshakeHandler).start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if (isRunning()) {
|
||||
((Lifecycle) this.handshakeHandler).start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(Runnable callback) {
|
||||
if (isRunning()) {
|
||||
((Lifecycle) this.handshakeHandler).stop();
|
||||
}
|
||||
callback.run();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.web.socket.config.annotation.SockJsServiceRegistration
|
||||
*/
|
||||
|
||||
@@ -61,6 +61,7 @@ import org.springframework.web.socket.messaging.SessionConnectedEvent;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 4.1
|
||||
*/
|
||||
public class WebSocketInboundChannelAdapter extends MessageProducerSupport
|
||||
@@ -255,6 +256,9 @@ public class WebSocketInboundChannelAdapter extends MessageProducerSupport
|
||||
@Override
|
||||
protected void doStop() {
|
||||
this.active = false;
|
||||
if (this.webSocketContainer instanceof Lifecycle) {
|
||||
((Lifecycle) this.webSocketContainer).stop();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isActive() {
|
||||
|
||||
Reference in New Issue
Block a user