Skip to content

Commit 1b0e95d

Browse files
committed
Support for RequestUpgradeStrategy + Lifecycle
Issue: SPR-15527
1 parent 24b3614 commit 1b0e95d

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

spring-web-reactive/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.commons.logging.LogFactory;
2323
import reactor.core.publisher.Mono;
2424

25+
import org.springframework.context.Lifecycle;
2526
import org.springframework.http.HttpMethod;
2627
import org.springframework.http.HttpStatus;
2728
import org.springframework.http.server.reactive.ServerHttpRequest;
@@ -43,7 +44,7 @@
4344
* @author Rossen Stoyanchev
4445
* @since 5.0
4546
*/
46-
public class HandshakeWebSocketService implements WebSocketService {
47+
public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
4748

4849
private static final String SEC_WEBSOCKET_KEY = "Sec-WebSocket-Key";
4950

@@ -58,6 +59,8 @@ public class HandshakeWebSocketService implements WebSocketService {
5859

5960
private final RequestUpgradeStrategy upgradeStrategy;
6061

62+
private volatile boolean running = false;
63+
6164

6265
/**
6366
* Default constructor automatic, classpath detection based discovery of the
@@ -104,6 +107,39 @@ public RequestUpgradeStrategy getUpgradeStrategy() {
104107
return this.upgradeStrategy;
105108
}
106109

110+
@Override
111+
public boolean isRunning() {
112+
return this.running;
113+
}
114+
115+
@Override
116+
public void start() {
117+
if (!isRunning()) {
118+
this.running = true;
119+
doStart();
120+
}
121+
}
122+
123+
protected void doStart() {
124+
if (getUpgradeStrategy() instanceof Lifecycle) {
125+
((Lifecycle) getUpgradeStrategy()).start();
126+
}
127+
}
128+
129+
@Override
130+
public void stop() {
131+
if (isRunning()) {
132+
this.running = false;
133+
doStop();
134+
}
135+
}
136+
137+
protected void doStop() {
138+
if (getUpgradeStrategy() instanceof Lifecycle) {
139+
((Lifecycle) getUpgradeStrategy()).stop();
140+
}
141+
}
142+
107143

108144
@Override
109145
public Mono<Void> handleRequest(ServerWebExchange exchange, WebSocketHandler webSocketHandler) {

spring-web-reactive/src/test/java/org/springframework/web/reactive/socket/server/AbstractWebSocketHandlerIntegrationTests.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,23 @@ static abstract class AbstractHandlerAdapterConfig {
104104

105105
@Bean
106106
public WebSocketHandlerAdapter handlerAdapter() {
107-
RequestUpgradeStrategy strategy = createUpgradeStrategy();
108-
WebSocketService service = new HandshakeWebSocketService(strategy);
109-
return new WebSocketHandlerAdapter(service);
107+
return new WebSocketHandlerAdapter(webSocketService());
110108
}
111109

112-
protected abstract RequestUpgradeStrategy createUpgradeStrategy();
110+
@Bean
111+
public WebSocketService webSocketService() {
112+
return new HandshakeWebSocketService(getUpgradeStrategy());
113+
}
114+
115+
protected abstract RequestUpgradeStrategy getUpgradeStrategy();
113116

114117
}
115118

116119
@Configuration
117120
static class ReactorNettyConfig extends AbstractHandlerAdapterConfig {
118121

119122
@Override
120-
protected RequestUpgradeStrategy createUpgradeStrategy() {
123+
protected RequestUpgradeStrategy getUpgradeStrategy() {
121124
return new ReactorNettyRequestUpgradeStrategy();
122125
}
123126
}
@@ -126,7 +129,7 @@ protected RequestUpgradeStrategy createUpgradeStrategy() {
126129
static class RxNettyConfig extends AbstractHandlerAdapterConfig {
127130

128131
@Override
129-
protected RequestUpgradeStrategy createUpgradeStrategy() {
132+
protected RequestUpgradeStrategy getUpgradeStrategy() {
130133
return new RxNettyRequestUpgradeStrategy();
131134
}
132135
}

0 commit comments

Comments
 (0)