Skip to content

Commit

Permalink
Merge pull request #5952 from eclipse/jetty-10.0.x-5828-JavaxWebSocke…
Browse files Browse the repository at this point in the history
…tHttpClient

Issue #5828 - allow HttpClient to be used with JavaxWebSocketClientContainerProvider
  • Loading branch information
lachlan-roberts committed Feb 21, 2021
2 parents d425c97 + cee44ff commit 4d67b30
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 35 deletions.
Expand Up @@ -64,6 +64,7 @@ public WebSocketCoreClient(HttpClient httpClient, WebSocketComponents webSocketC
this.httpClient = httpClient;
this.components = webSocketComponents;
addBean(httpClient);
addBean(webSocketComponents);
}

public CompletableFuture<CoreSession> connect(FrameHandler frameHandler, URI wsUri) throws IOException
Expand Down
Expand Up @@ -20,9 +20,9 @@
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;
Expand Down
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -52,29 +53,26 @@ public static void stop(WebSocketContainer container) throws Exception
@Override
protected 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();

// 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);
}
}
return getContainer(null);
}

/**
* 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 void registerShutdown(JavaxWebSocketClientContainer container)
{
// Register as JVM runtime shutdown hook.
ShutdownThread.register(container);
LifeCycle.start(container);
}
}
Expand Up @@ -70,22 +70,12 @@ public JavaxWebSocketClientContainer()
*/
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(), (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<WebSocketComponents, WebSocketCoreClient> coreClientFactory)
Expand Down

0 comments on commit 4d67b30

Please sign in to comment.