Skip to content

Commit

Permalink
Issue #10547 - Allow Executor of WebSocketClient to be customized via…
Browse files Browse the repository at this point in the history
… HttpClient

Signed-off-by: Joakim Erdfelt <[email protected]>
  • Loading branch information
joakime committed Sep 19, 2023
1 parent 43eb08b commit b79cab5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketPoli
private final List<WebSocketSessionListener> 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;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit b79cab5

Please sign in to comment.