diff --git a/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java b/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java index 271956510932..38689277356f 100644 --- a/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java +++ b/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java @@ -64,7 +64,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketPoli private final List sessionListeners = new CopyOnWriteArrayList<>(); private final SessionTracker sessionTracker = new SessionTracker(); private final Configuration.ConfigurationCustomizer configurationCustomizer = new Configuration.ConfigurationCustomizer(); - private final WebSocketComponents components = new WebSocketComponents(); + private final WebSocketComponents components; private boolean stopAtShutdown = false; private long _stopTimeout = Long.MAX_VALUE; @@ -83,6 +83,9 @@ public WebSocketClient() */ public WebSocketClient(HttpClient httpClient) { + ByteBufferPool bufferPool = httpClient != null ? httpClient.getByteBufferPool() : null; + Executor executor = httpClient != null ? httpClient.getExecutor() : null; + components = new WebSocketComponents(null, null, bufferPool, null, null, executor); coreClient = new WebSocketCoreClient(httpClient, components); addManaged(coreClient); frameHandlerFactory = new JettyWebSocketFrameHandlerFactory(this, components); diff --git a/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/WebSocketClientTest.java b/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/WebSocketClientTest.java index 8756812bfb60..d27838d6e7ab 100644 --- a/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/WebSocketClientTest.java +++ b/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/WebSocketClientTest.java @@ -22,9 +22,15 @@ import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.HttpClientTransport; +import org.eclipse.jetty.client.http.HttpClientTransportOverHTTP; +import org.eclipse.jetty.io.ClientConnector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -55,6 +61,7 @@ import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; public class WebSocketClientTest @@ -112,6 +119,63 @@ public void stopServer() throws Exception server.stop(); } + @Test + public void testCustomizeExecutorDirectly() throws Exception + { + Executor executor = Executors.newFixedThreadPool(50); + HttpClient httpClient = new HttpClient(); + httpClient.setExecutor(executor); + try + { + httpClient.start(); + WebSocketClient webSocketClient = new WebSocketClient(httpClient); + try + { + webSocketClient.start(); + Executor inuseExecutor = webSocketClient.getExecutor(); + assertSame(executor, inuseExecutor); + } + finally + { + webSocketClient.stop(); + } + } + finally + { + httpClient.stop(); + } + } + + @Test + public void testCustomizeExecutorViaConnector() throws Exception + { + ClientConnector clientConnector = new ClientConnector(); + clientConnector.setSelectors(1); + Executor executor = Executors.newFixedThreadPool(50); + clientConnector.setExecutor(executor); + HttpClientTransport transport = new HttpClientTransportOverHTTP(clientConnector); + HttpClient httpClient = new HttpClient(transport); + try + { + httpClient.start(); + WebSocketClient webSocketClient = new WebSocketClient(httpClient); + try + { + webSocketClient.start(); + Executor inuseExecutor = webSocketClient.getExecutor(); + assertSame(executor, inuseExecutor); + } + finally + { + webSocketClient.stop(); + } + } + finally + { + httpClient.stop(); + } + } + @Test public void testAddExtensionNotInstalled() throws Exception {