Skip to content

Commit 1c39df1

Browse files
authored
Merge pull request #13671 from jetty/fix/jetty-12.1.x/ServerWebSocketContainer-restart
Fix ServerWebSocketContainer lifecycle when restarted.
2 parents 10cfa7e + 03c136b commit 1c39df1

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

jetty-core/jetty-websocket/jetty-websocket-jetty-server/src/main/java/org/eclipse/jetty/websocket/server/ServerWebSocketContainer.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public static ServerWebSocketContainer ensure(Server server, ContextHandler cont
8181
? WebSocketServerComponents.ensureWebSocketComponents(server)
8282
: WebSocketServerComponents.ensureWebSocketComponents(server, contextHandler);
8383
WebSocketMappings mappings = new WebSocketMappings(components);
84-
container = new ServerWebSocketContainer(mappings);
85-
container.addBean(mappings);
86-
context.setAttribute(WebSocketContainer.class.getName(), container);
84+
container = new ServerWebSocketContainer(context, mappings);
85+
ContainerLifeCycle parent = contextHandler == null ? server : contextHandler;
86+
parent.addManaged(container);
8787
}
8888
return container;
8989
}
@@ -122,18 +122,28 @@ public static ServerWebSocketContainer get(Context context)
122122
private final List<WebSocketSessionListener> listeners = new ArrayList<>();
123123
private final SessionTracker sessionTracker = new SessionTracker();
124124
private final Configuration configuration = new Configuration();
125+
private final Context context;
125126
private final WebSocketMappings mappings;
126127
private final FrameHandlerFactory factory;
127128
private InvocationType invocationType = InvocationType.BLOCKING;
128129

129-
ServerWebSocketContainer(WebSocketMappings mappings)
130+
ServerWebSocketContainer(Context context, WebSocketMappings mappings)
130131
{
132+
this.context = context;
131133
this.mappings = mappings;
134+
installBean(mappings);
132135
this.factory = new ServerFrameHandlerFactory(this, mappings.getWebSocketComponents());
133136
addSessionListener(sessionTracker);
134137
installBean(sessionTracker);
135138
}
136139

140+
@Override
141+
protected void doStart() throws Exception
142+
{
143+
context.setAttribute(WebSocketContainer.class.getName(), this);
144+
super.doStart();
145+
}
146+
137147
@Override
138148
public Executor getExecutor()
139149
{

jetty-core/jetty-websocket/jetty-websocket-jetty-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeHandler.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
import org.eclipse.jetty.server.handler.ContextHandler;
2323
import org.eclipse.jetty.util.Callback;
2424
import org.eclipse.jetty.util.thread.Invocable;
25-
import org.eclipse.jetty.websocket.api.WebSocketContainer;
26-
import org.eclipse.jetty.websocket.core.WebSocketComponents;
27-
import org.eclipse.jetty.websocket.core.server.WebSocketMappings;
28-
import org.eclipse.jetty.websocket.core.server.WebSocketServerComponents;
2925

3026
/**
3127
* <p>A {@link Handler} that may perform the upgrade from HTTP to WebSocket.</p>
@@ -107,14 +103,8 @@ public static WebSocketUpgradeHandler from(Server server, ContextHandler context
107103
*/
108104
public static WebSocketUpgradeHandler from(Server server, ContextHandler context, Consumer<ServerWebSocketContainer> configurator)
109105
{
110-
WebSocketComponents components = WebSocketServerComponents.ensureWebSocketComponents(server, context);
111-
WebSocketMappings mappings = new WebSocketMappings(components);
112-
ServerWebSocketContainer container = new ServerWebSocketContainer(mappings);
113-
container.addBean(mappings);
114-
115-
WebSocketUpgradeHandler wsHandler = new WebSocketUpgradeHandler(container, configurator);
116-
context.getContext().setAttribute(WebSocketContainer.class.getName(), container);
117-
return wsHandler;
106+
ServerWebSocketContainer container = ServerWebSocketContainer.ensure(server, context);
107+
return new WebSocketUpgradeHandler(container, configurator);
118108
}
119109

120110
/**
@@ -150,13 +140,7 @@ public static WebSocketUpgradeHandler from(Server server)
150140
*/
151141
public static WebSocketUpgradeHandler from(Server server, Consumer<ServerWebSocketContainer> configurator)
152142
{
153-
WebSocketComponents components = WebSocketServerComponents.ensureWebSocketComponents(server);
154-
WebSocketMappings mappings = new WebSocketMappings(components);
155-
ServerWebSocketContainer container = new ServerWebSocketContainer(mappings);
156-
157-
WebSocketUpgradeHandler wsHandler = new WebSocketUpgradeHandler(container, configurator);
158-
server.getContext().setAttribute(WebSocketContainer.class.getName(), container);
159-
return wsHandler;
143+
return from(server, null, configurator);
160144
}
161145

162146
private final ServerWebSocketContainer _container;
@@ -186,7 +170,7 @@ public WebSocketUpgradeHandler(ServerWebSocketContainer container, Consumer<Serv
186170
{
187171
_container = container;
188172
_configurator = configurator;
189-
addManaged(container);
173+
installBean(container);
190174
}
191175

192176
/**

jetty-core/jetty-websocket/jetty-websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/RestartTest.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.eclipse.jetty.server.handler.ContextHandler;
2222
import org.eclipse.jetty.websocket.api.Callback;
2323
import org.eclipse.jetty.websocket.client.WebSocketClient;
24+
import org.eclipse.jetty.websocket.server.ServerWebSocketContainer;
2425
import org.eclipse.jetty.websocket.server.WebSocketUpgradeHandler;
2526
import org.junit.jupiter.api.AfterEach;
2627
import org.junit.jupiter.api.BeforeEach;
@@ -29,14 +30,17 @@
2930
import static org.hamcrest.MatcherAssert.assertThat;
3031
import static org.hamcrest.Matchers.containsString;
3132
import static org.hamcrest.Matchers.equalTo;
33+
import static org.junit.jupiter.api.Assertions.assertNotNull;
34+
import static org.junit.jupiter.api.Assertions.assertNull;
3235
import static org.junit.jupiter.api.Assertions.assertTrue;
3336

3437
public class RestartTest
3538
{
3639
private Server _server;
3740
private ServerConnector _connector;
41+
private ContextHandler _contextHandler;
42+
private WebSocketUpgradeHandler _upgradeHandler;
3843
private WebSocketClient _client;
39-
private WebSocketUpgradeHandler upgradeHandler;
4044

4145
@BeforeEach
4246
public void before() throws Exception
@@ -45,11 +49,11 @@ public void before() throws Exception
4549
_connector = new ServerConnector(_server);
4650
_server.addConnector(_connector);
4751

48-
ContextHandler contextHandler = new ContextHandler("/");
49-
upgradeHandler = WebSocketUpgradeHandler.from(_server, contextHandler,
52+
_contextHandler = new ContextHandler("/");
53+
_upgradeHandler = WebSocketUpgradeHandler.from(_server, _contextHandler,
5054
container -> container.addMapping("/", (req, resp, cb) -> new EchoSocket()));
51-
contextHandler.setHandler(upgradeHandler);
52-
_server.setHandler(contextHandler);
55+
_contextHandler.setHandler(_upgradeHandler);
56+
_server.setHandler(_contextHandler);
5357

5458
_server.start();
5559

@@ -65,15 +69,25 @@ public void after() throws Exception
6569
}
6670

6771
@Test
68-
public void test() throws Exception
72+
public void testEchoStopStartEcho() throws Exception
6973
{
7074
testEcho();
7175
_server.stop();
72-
assertThat(upgradeHandler.getServerWebSocketContainer().dump(), containsString("PathMappings[size=0]"));
76+
assertThat(_upgradeHandler.getServerWebSocketContainer().dump(), containsString("PathMappings[size=0]"));
7377
_server.start();
7478
testEcho();
7579
}
7680

81+
@Test
82+
public void testStopStartGet() throws Exception
83+
{
84+
assertNotNull(ServerWebSocketContainer.get(_contextHandler.getContext()));
85+
_server.stop();
86+
assertNull(ServerWebSocketContainer.get(_contextHandler.getContext()));
87+
_server.start();
88+
assertNotNull(ServerWebSocketContainer.get(_contextHandler.getContext()));
89+
}
90+
7791
private void testEcho() throws Exception
7892
{
7993
EchoSocket clientEndpoint = new EchoSocket();

jetty-ee11/jetty-ee11-websocket/jetty-ee11-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee11/websocket/jakarta/tests/JakartaWebSocketRestartTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.concurrent.TimeUnit;
1818

1919
import jakarta.websocket.Session;
20+
import jakarta.websocket.server.ServerContainer;
2021
import org.eclipse.jetty.ee11.servlet.FilterHolder;
2122
import org.eclipse.jetty.ee11.servlet.ServletContextHandler;
2223
import org.eclipse.jetty.ee11.websocket.jakarta.client.JakartaWebSocketClientContainer;
@@ -54,6 +55,9 @@ public void before() throws Exception
5455
contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
5556
contextHandler.setContextPath("/");
5657
server.setHandler(contextHandler);
58+
JakartaWebSocketServletContainerInitializer.configure(contextHandler, (context, container) ->
59+
container.addEndpoint(EchoSocket.class));
60+
server.start();
5761

5862
client = new JakartaWebSocketClientContainer();
5963
client.start();
@@ -69,10 +73,6 @@ public void stop() throws Exception
6973
@Test
7074
public void testWebSocketRestart() throws Exception
7175
{
72-
JakartaWebSocketServletContainerInitializer.configure(contextHandler, (context, container) ->
73-
container.addEndpoint(EchoSocket.class));
74-
server.start();
75-
7676
int numEventListeners = contextHandler.getEventListeners().size();
7777
for (int i = 0; i < 100; i++)
7878
{
@@ -103,6 +103,16 @@ public void testWebSocketRestart() throws Exception
103103
assertThat(contextHandler.getServletHandler().getFilters().length, is(0));
104104
}
105105

106+
@Test
107+
public void testStopStartGet() throws Exception
108+
{
109+
assertNotNull(contextHandler.getContext().getAttribute(ServerContainer.class.getName()));
110+
server.stop();
111+
assertNull(contextHandler.getContext().getAttribute(ServerContainer.class.getName()));
112+
server.start();
113+
assertNotNull(contextHandler.getContext().getAttribute(ServerContainer.class.getName()));
114+
}
115+
106116
private void testEchoMessage() throws Exception
107117
{
108118
// Test we can upgrade to websocket and send a message.

0 commit comments

Comments
 (0)