Merge pull request #5952 from eclipse/jetty-10.0.x-5828-JavaxWebSocketHttpClient

Issue #5828 - allow HttpClient to be used with JavaxWebSocketClientContainerProvider
This commit is contained in:
Lachlan 2021-02-22 09:41:34 +11:00 committed by GitHub
commit 4d67b30785
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 35 deletions

View File

@ -64,6 +64,7 @@ public class WebSocketCoreClient extends ContainerLifeCycle
this.httpClient = httpClient; this.httpClient = httpClient;
this.components = webSocketComponents; this.components = webSocketComponents;
addBean(httpClient); addBean(httpClient);
addBean(webSocketComponents);
} }
public CompletableFuture<CoreSession> connect(FrameHandler frameHandler, URI wsUri) throws IOException public CompletableFuture<CoreSession> connect(FrameHandler frameHandler, URI wsUri) throws IOException

View File

@ -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;
exports org.eclipse.jetty.websocket.javax.client.internal to org.eclipse.jetty.websocket.javax.server; 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.core.client;
requires org.eclipse.jetty.websocket.javax.common; requires org.eclipse.jetty.websocket.javax.common;
requires transitive org.eclipse.jetty.client;
requires transitive jetty.websocket.api; requires transitive jetty.websocket.api;
provides ContainerProvider with JavaxWebSocketClientContainerProvider; provides ContainerProvider with JavaxWebSocketClientContainerProvider;

View File

@ -16,6 +16,7 @@ package org.eclipse.jetty.websocket.javax.client;
import javax.websocket.ContainerProvider; import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer; import javax.websocket.WebSocketContainer;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.ShutdownThread; import org.eclipse.jetty.util.thread.ShutdownThread;
import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer; import org.eclipse.jetty.websocket.javax.client.internal.JavaxWebSocketClientContainer;
@ -52,29 +53,26 @@ public class JavaxWebSocketClientContainerProvider extends ContainerProvider
@Override @Override
protected WebSocketContainer getContainer() protected WebSocketContainer getContainer()
{ {
// See: https://github.com/javaee/websocket-spec/issues/212 return getContainer(null);
// 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();
// Register as JVM runtime shutdown hook?
ShutdownThread.register(clientContainer);
if (!clientContainer.isStarted())
{
try
{
clientContainer.start();
}
catch (Exception e)
{
throw new RuntimeException("Unable to start Client Container", e);
}
} }
/**
* 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; return clientContainer;
} }
// See: https://github.com/eclipse-ee4j/websocket-api/issues/212
private void registerShutdown(JavaxWebSocketClientContainer container)
{
// Register as JVM runtime shutdown hook.
ShutdownThread.register(container);
LifeCycle.start(container);
}
} }

View File

@ -70,22 +70,12 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
*/ */
public JavaxWebSocketClientContainer(final HttpClient httpClient) public JavaxWebSocketClientContainer(final HttpClient httpClient)
{ {
this(new WebSocketComponents(), (wsComponents) -> this(new WebSocketComponents(), (components) -> new WebSocketCoreClient(httpClient, components));
{
WebSocketCoreClient coreClient = new WebSocketCoreClient(httpClient, wsComponents);
coreClient.getHttpClient().setName("Javax-WebSocketClient@" + Integer.toHexString(coreClient.getHttpClient().hashCode()));
return coreClient;
});
} }
public JavaxWebSocketClientContainer(WebSocketComponents components) public JavaxWebSocketClientContainer(WebSocketComponents components)
{ {
this(components, (wsComponents) -> this(components, WebSocketCoreClient::new);
{
WebSocketCoreClient coreClient = new WebSocketCoreClient(wsComponents);
coreClient.getHttpClient().setName("Javax-WebSocketClient@" + Integer.toHexString(coreClient.getHttpClient().hashCode()));
return coreClient;
});
} }
public JavaxWebSocketClientContainer(WebSocketComponents components, Function<WebSocketComponents, WebSocketCoreClient> coreClientFactory) public JavaxWebSocketClientContainer(WebSocketComponents components, Function<WebSocketComponents, WebSocketCoreClient> coreClientFactory)