From 7a72a6ac64656110fdfca3b0a42baa41a14b8cf8 Mon Sep 17 00:00:00 2001
From: Lachlan Roberts
Date: Wed, 23 Dec 2020 17:14:12 +1100
Subject: [PATCH 1/4] Issue #5828 - allow HttpClient to be used with
JavaxWebSocketClientContainerProvider
Signed-off-by: Lachlan Roberts
---
.../core/client/WebSocketCoreClient.java | 1 +
.../src/main/java/module-info.java | 2 +-
...JavaxWebSocketClientContainerProvider.java | 35 +++++++++++++------
.../JavaxWebSocketClientContainer.java | 7 +---
4 files changed, 27 insertions(+), 18 deletions(-)
diff --git a/jetty-websocket/websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/WebSocketCoreClient.java b/jetty-websocket/websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/WebSocketCoreClient.java
index cab076489d8..eabf31d5abe 100644
--- a/jetty-websocket/websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/WebSocketCoreClient.java
+++ b/jetty-websocket/websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/WebSocketCoreClient.java
@@ -64,6 +64,7 @@ public class WebSocketCoreClient extends ContainerLifeCycle
this.httpClient = httpClient;
this.components = webSocketComponents;
addBean(httpClient);
+ addBean(webSocketComponents);
}
public CompletableFuture connect(FrameHandler frameHandler, URI wsUri) throws IOException
diff --git a/jetty-websocket/websocket-javax-client/src/main/java/module-info.java b/jetty-websocket/websocket-javax-client/src/main/java/module-info.java
index 54323eb068b..d6894b93269 100644
--- a/jetty-websocket/websocket-javax-client/src/main/java/module-info.java
+++ b/jetty-websocket/websocket-javax-client/src/main/java/module-info.java
@@ -20,9 +20,9 @@ module org.eclipse.jetty.websocket.javax.client
exports org.eclipse.jetty.websocket.javax.client;
exports org.eclipse.jetty.websocket.javax.client.internal to org.eclipse.jetty.websocket.javax.server;
- requires org.eclipse.jetty.client;
requires org.eclipse.jetty.websocket.core.client;
requires org.eclipse.jetty.websocket.javax.common;
+ requires transitive org.eclipse.jetty.client;
requires transitive jetty.websocket.api;
provides ContainerProvider with JavaxWebSocketClientContainerProvider;
diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
index 3f00b215773..46e67ab95f2 100644
--- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
+++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
@@ -16,6 +16,7 @@ package org.eclipse.jetty.websocket.javax.client;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
+import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.ShutdownThread;
import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
@@ -50,24 +51,36 @@ public class JavaxWebSocketClientContainerProvider extends ContainerProvider
*
*/
@Override
- protected WebSocketContainer getContainer()
+ public WebSocketContainer getContainer()
{
- // See: https://github.com/javaee/websocket-spec/issues/212
- // TODO: on multiple executions, do we warn?
- // TODO: do we care?
- // TODO: on multiple executions, do we share bufferPool/executors/etc?
- // TODO: do we want to provide a non-standard way to configure to always return the same clientContainer based on a config somewhere? (system.property?)
-
JavaxWebSocketClientContainer clientContainer = new JavaxWebSocketClientContainer();
+ registerShutdown(clientContainer);
+ return clientContainer;
+ }
+ /**
+ * Get a new instance of a client {@link WebSocketContainer} which uses a supplied {@link HttpClient}.
+ * @param httpClient a pre-configured {@link HttpClient} to be used by the implementation.
+ * @see #getContainer()
+ */
+ public WebSocketContainer getContainer(HttpClient httpClient)
+ {
+ JavaxWebSocketClientContainer clientContainer = new JavaxWebSocketClientContainer(httpClient);
+ registerShutdown(clientContainer);
+ return clientContainer;
+ }
+
+ // See: https://github.com/eclipse-ee4j/websocket-api/issues/212
+ private WebSocketContainer registerShutdown(JavaxWebSocketClientContainer container)
+ {
// Register as JVM runtime shutdown hook?
- ShutdownThread.register(clientContainer);
+ ShutdownThread.register(container);
- if (!clientContainer.isStarted())
+ if (!container.isStarted())
{
try
{
- clientContainer.start();
+ container.start();
}
catch (Exception e)
{
@@ -75,6 +88,6 @@ public class JavaxWebSocketClientContainerProvider extends ContainerProvider
}
}
- return clientContainer;
+ return container;
}
}
diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java
index c22f991e167..4f65bbd2c9e 100644
--- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java
+++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java
@@ -70,12 +70,7 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
*/
public JavaxWebSocketClientContainer(final HttpClient httpClient)
{
- this(new WebSocketComponents(), (wsComponents) ->
- {
- WebSocketCoreClient coreClient = new WebSocketCoreClient(httpClient, wsComponents);
- coreClient.getHttpClient().setName("Javax-WebSocketClient@" + Integer.toHexString(coreClient.getHttpClient().hashCode()));
- return coreClient;
- });
+ this(new WebSocketComponents(), (wsComponents) -> new WebSocketCoreClient(httpClient, wsComponents));
}
public JavaxWebSocketClientContainer(WebSocketComponents components)
From 0e4ed5c7b1530308b5f45326de0d05920e205d59 Mon Sep 17 00:00:00 2001
From: Lachlan Roberts
Date: Mon, 15 Feb 2021 15:13:02 +1100
Subject: [PATCH 2/4] Issue #5828 - cleanups from review
Signed-off-by: Lachlan Roberts
---
...JavaxWebSocketClientContainerProvider.java | 19 +++----------------
1 file changed, 3 insertions(+), 16 deletions(-)
diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
index 70f23f7a09d..9b64a566d3a 100644
--- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
+++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
@@ -71,23 +71,10 @@ public class JavaxWebSocketClientContainerProvider extends ContainerProvider
}
// See: https://github.com/eclipse-ee4j/websocket-api/issues/212
- private WebSocketContainer registerShutdown(JavaxWebSocketClientContainer container)
+ private void registerShutdown(JavaxWebSocketClientContainer container)
{
- // Register as JVM runtime shutdown hook?
+ // Register as JVM runtime shutdown hook.
ShutdownThread.register(container);
-
- if (!container.isStarted())
- {
- try
- {
- container.start();
- }
- catch (Exception e)
- {
- throw new RuntimeException("Unable to start Client Container", e);
- }
- }
-
- return container;
+ LifeCycle.start(container);
}
}
From b69f470304620997f0960a60dd21034130c86988 Mon Sep 17 00:00:00 2001
From: Lachlan Roberts
Date: Mon, 15 Feb 2021 15:32:18 +1100
Subject: [PATCH 3/4] Issue #5828 - only use the HttpClient constructor for
JavaxWebSocketClientContainer
Signed-off-by: Lachlan Roberts
---
.../client/JavaxWebSocketClientContainerProvider.java | 4 +---
.../client/internal/JavaxWebSocketClientContainer.java | 9 ++-------
2 files changed, 3 insertions(+), 10 deletions(-)
diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
index 9b64a566d3a..3497fdac519 100644
--- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
+++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
@@ -53,9 +53,7 @@ public class JavaxWebSocketClientContainerProvider extends ContainerProvider
@Override
public WebSocketContainer getContainer()
{
- JavaxWebSocketClientContainer clientContainer = new JavaxWebSocketClientContainer();
- registerShutdown(clientContainer);
- return clientContainer;
+ return getContainer(null);
}
/**
diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java
index 4feb641a88c..883db46e5f6 100644
--- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java
+++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientContainer.java
@@ -70,17 +70,12 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
*/
public JavaxWebSocketClientContainer(final HttpClient httpClient)
{
- this(new WebSocketComponents(), (wsComponents) -> new WebSocketCoreClient(httpClient, wsComponents));
+ this(new WebSocketComponents(), (components) -> new WebSocketCoreClient(httpClient, components));
}
public JavaxWebSocketClientContainer(WebSocketComponents components)
{
- this(components, (wsComponents) ->
- {
- WebSocketCoreClient coreClient = new WebSocketCoreClient(wsComponents);
- coreClient.getHttpClient().setName("Javax-WebSocketClient@" + Integer.toHexString(coreClient.getHttpClient().hashCode()));
- return coreClient;
- });
+ this(components, WebSocketCoreClient::new);
}
public JavaxWebSocketClientContainer(WebSocketComponents components, Function coreClientFactory)
From cee44ff642bac599a6a6d3deda1fc776570a1618 Mon Sep 17 00:00:00 2001
From: Lachlan Roberts
Date: Fri, 19 Feb 2021 19:56:36 +1100
Subject: [PATCH 4/4] JavaxWebSocketClientContainerProvider getContainer should
remain protected.
Signed-off-by: Lachlan Roberts
---
.../javax/client/JavaxWebSocketClientContainerProvider.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
index 3497fdac519..739b4531772 100644
--- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
+++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/JavaxWebSocketClientContainerProvider.java
@@ -51,7 +51,7 @@ public class JavaxWebSocketClientContainerProvider extends ContainerProvider
*
*/
@Override
- public WebSocketContainer getContainer()
+ protected WebSocketContainer getContainer()
{
return getContainer(null);
}