Skip to content

Commit 4eabe29

Browse files
committed
Polishing contribution
Closes gh-28785
1 parent cd10171 commit 4eabe29

File tree

4 files changed

+51
-47
lines changed

4 files changed

+51
-47
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/client/ConnectionManagerSupport.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
import org.springframework.web.util.UriComponentsBuilder;
2626

2727
/**
28-
* A base class for WebSocket connection managers. Provides a declarative style of
29-
* connecting to a WebSocket server given a URI to connect to. The connection occurs when
30-
* the Spring ApplicationContext is refreshed, if the {@link #autoStartup} property is set
31-
* to {@code true}, or if set to {@code false}, the {@link #start()} and #stop methods can
32-
* be invoked manually.
28+
* Base class for a connection manager that automates the process of connecting
29+
* to a WebSocket server with the Spring ApplicationContext lifecycle. Connects
30+
* to a WebSocket server on {@link #start()} and disconnects on {@link #stop()}.
31+
* If {@link #setAutoStartup(boolean)} is set to {@code true} this will be done
32+
* automatically when the Spring {@code ApplicationContext} is refreshed.
3333
*
3434
* @author Rossen Stoyanchev
3535
* @since 4.0
@@ -163,11 +163,19 @@ public boolean isRunning() {
163163
return this.running;
164164
}
165165

166+
/**
167+
* Whether the connection is open/{@code true} or closed/{@code false}.
168+
*/
169+
public abstract boolean isConnected();
166170

171+
/**
172+
* Subclasses implement this to actually establish the connection.
173+
*/
167174
protected abstract void openConnection();
168175

176+
/**
177+
* Subclasses implement this to close the connection.
178+
*/
169179
protected abstract void closeConnection() throws Exception;
170180

171-
protected abstract boolean isConnected();
172-
173181
}

spring-websocket/src/main/java/org/springframework/web/socket/client/WebSocketConnectionManager.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,10 +29,9 @@
2929
import org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator;
3030

3131
/**
32-
* A WebSocket connection manager that is given a URI, a {@link WebSocketClient}, and a
33-
* {@link WebSocketHandler}, connects to a WebSocket server through {@link #start()} and
34-
* {@link #stop()} methods. If {@link #setAutoStartup(boolean)} is set to {@code true}
35-
* this will be done automatically when the Spring ApplicationContext is refreshed.
32+
* WebSocket {@link ConnectionManagerSupport connection manager} that connects
33+
* to the server via {@link WebSocketClient} and handles the session with a
34+
* {@link WebSocketHandler}.
3635
*
3736
* @author Rossen Stoyanchev
3837
* @since 4.0
@@ -46,7 +45,7 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
4645
@Nullable
4746
private WebSocketSession webSocketSession;
4847

49-
private WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
48+
private final WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
5049

5150

5251
public WebSocketConnectionManager(WebSocketClient client,
@@ -58,14 +57,6 @@ public WebSocketConnectionManager(WebSocketClient client,
5857
}
5958

6059

61-
/**
62-
* Decorate the WebSocketHandler provided to the class constructor.
63-
* <p>By default {@link LoggingWebSocketHandlerDecorator} is added.
64-
*/
65-
protected WebSocketHandler decorateWebSocketHandler(WebSocketHandler handler) {
66-
return new LoggingWebSocketHandlerDecorator(handler);
67-
}
68-
6960
/**
7061
* Set the sub-protocols to use. If configured, specified sub-protocols will be
7162
* requested in the handshake through the {@code Sec-WebSocket-Protocol} header. The
@@ -130,6 +121,11 @@ public void stopInternal() throws Exception {
130121
super.stopInternal();
131122
}
132123

124+
@Override
125+
public boolean isConnected() {
126+
return (this.webSocketSession != null && this.webSocketSession.isOpen());
127+
}
128+
133129
@Override
134130
protected void openConnection() {
135131
if (logger.isInfoEnabled()) {
@@ -159,9 +155,12 @@ protected void closeConnection() throws Exception {
159155
}
160156
}
161157

162-
@Override
163-
public boolean isConnected() {
164-
return (this.webSocketSession != null && this.webSocketSession.isOpen());
158+
/**
159+
* Decorate the WebSocketHandler provided to the class constructor.
160+
* <p>By default {@link LoggingWebSocketHandlerDecorator} is added.
161+
*/
162+
protected WebSocketHandler decorateWebSocketHandler(WebSocketHandler handler) {
163+
return new LoggingWebSocketHandlerDecorator(handler);
165164
}
166165

167166
}

spring-websocket/src/main/java/org/springframework/web/socket/client/standard/AnnotatedEndpointConnectionManager.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,11 +31,9 @@
3131
import org.springframework.web.socket.handler.BeanCreatingHandlerProvider;
3232

3333
/**
34-
* A WebSocket connection manager that is given a URI, a
35-
* {@link javax.websocket.ClientEndpoint}-annotated endpoint, connects to a
36-
* WebSocket server through the {@link #start()} and {@link #stop()} methods.
37-
* If {@link #setAutoStartup(boolean)} is set to {@code true} this will be
38-
* done automatically when the Spring ApplicationContext is refreshed.
34+
* WebSocket {@link ConnectionManagerSupport connection manager} that connects
35+
* to the server via {@link WebSocketContainer} and handles the session with an
36+
* {@link javax.websocket.ClientEndpoint @ClientEndpoint} endpoint.
3937
*
4038
* @author Rossen Stoyanchev
4139
* @since 4.0
@@ -101,6 +99,12 @@ public TaskExecutor getTaskExecutor() {
10199
}
102100

103101

102+
@Override
103+
public boolean isConnected() {
104+
Session session = this.session;
105+
return (session != null && session.isOpen());
106+
}
107+
104108
@Override
105109
protected void openConnection() {
106110
this.taskExecutor.execute(() -> {
@@ -135,10 +139,4 @@ protected void closeConnection() throws Exception {
135139
}
136140
}
137141

138-
@Override
139-
protected boolean isConnected() {
140-
Session session = this.session;
141-
return (session != null && session.isOpen());
142-
}
143-
144142
}

spring-websocket/src/main/java/org/springframework/web/socket/client/standard/EndpointConnectionManager.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,10 +39,9 @@
3939
import org.springframework.web.socket.handler.BeanCreatingHandlerProvider;
4040

4141
/**
42-
* A WebSocket connection manager that is given a URI, an {@link Endpoint}, connects to a
43-
* WebSocket server through the {@link #start()} and {@link #stop()} methods. If
44-
* {@link #setAutoStartup(boolean)} is set to {@code true} this will be done automatically
45-
* when the Spring ApplicationContext is refreshed.
42+
* WebSocket {@link ConnectionManagerSupport connection manager} that connects
43+
* to the server via {@link WebSocketContainer} and handles the session with an
44+
* {@link Endpoint}.
4645
*
4746
* @author Rossen Stoyanchev
4847
* @since 4.0
@@ -133,6 +132,12 @@ public TaskExecutor getTaskExecutor() {
133132
}
134133

135134

135+
@Override
136+
public boolean isConnected() {
137+
Session session = this.session;
138+
return (session != null && session.isOpen());
139+
}
140+
136141
@Override
137142
protected void openConnection() {
138143
this.taskExecutor.execute(() -> {
@@ -168,10 +173,4 @@ protected void closeConnection() throws Exception {
168173
}
169174
}
170175

171-
@Override
172-
protected boolean isConnected() {
173-
Session session = this.session;
174-
return (session != null && session.isOpen());
175-
}
176-
177176
}

0 commit comments

Comments
 (0)