Issue #1516 - making WebSocket started thread pools have identifying names

This commit is contained in:
Joakim Erdfelt 2017-05-11 09:52:02 -07:00
parent 2679715a30
commit c75e3c19d9
4 changed files with 41 additions and 12 deletions

View File

@ -51,7 +51,7 @@ public class DelayedStartClientTest
assertThat("Container", container, notNullValue()); assertThat("Container", container, notNullValue());
List<String> threadNames = getThreadNames(); List<String> threadNames = getThreadNames();
assertThat("Threads", threadNames, not(hasItem(containsString("SimpleContainerScope.Executor@")))); assertThat("Threads", threadNames, not(hasItem(containsString("WebSocketContainer@"))));
assertThat("Threads", threadNames, not(hasItem(containsString("HttpClient@")))); assertThat("Threads", threadNames, not(hasItem(containsString("HttpClient@"))));
} }

View File

@ -37,6 +37,7 @@ import java.net.URI;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
@ -175,6 +176,19 @@ public class DelayedStartClientOnServerTest
} }
} }
private void assertNoHttpClientPoolThreads(List<String> threadNames)
{
for (String threadName : threadNames)
{
if (threadName.startsWith("HttpClient@") && !threadName.endsWith("-scheduler"))
{
throw new AssertionError("Found non-scheduler HttpClient thread in <" +
threadNames.stream().collect(Collectors.joining("[", ", ", "]"))
+ ">");
}
}
}
/** /**
* Using the Server specific techniques of JSR356, configure the WebSocketContainer. * Using the Server specific techniques of JSR356, configure the WebSocketContainer.
*/ */
@ -212,8 +226,9 @@ public class DelayedStartClientOnServerTest
{ {
server.start(); server.start();
List<String> threadNames = getThreadNames(); List<String> threadNames = getThreadNames();
assertThat("Threads", threadNames, not(hasItem(containsString("SimpleContainerScope.Executor@")))); assertNoHttpClientPoolThreads(threadNames);
assertThat("Threads", threadNames, not(hasItem(containsString("HttpClient@")))); assertThat("Threads", threadNames, not(hasItem(containsString("WebSocketContainer@"))));
assertThat("Threads", threadNames, not(hasItem(containsString("WebSocketClient@"))));
} }
finally finally
{ {
@ -221,6 +236,7 @@ public class DelayedStartClientOnServerTest
} }
} }
@Test @Test
public void testHttpClientThreads_AfterClientConnectTo() throws Exception public void testHttpClientThreads_AfterClientConnectTo() throws Exception
{ {
@ -237,8 +253,8 @@ public class DelayedStartClientOnServerTest
String response = GET(server.getURI().resolve("/connect")); String response = GET(server.getURI().resolve("/connect"));
assertThat("Response", response, startsWith("Connected to ws://")); assertThat("Response", response, startsWith("Connected to ws://"));
List<String> threadNames = getThreadNames(); List<String> threadNames = getThreadNames();
assertThat("Threads", threadNames, hasItem(containsString("SimpleContainerScope.Executor@"))); assertNoHttpClientPoolThreads(threadNames);
assertThat("Threads", threadNames, hasItem(containsString("HttpClient@"))); assertThat("Threads", threadNames, hasItem(containsString("WebSocketContainer@")));
} }
finally finally
{ {
@ -262,7 +278,8 @@ public class DelayedStartClientOnServerTest
String response = GET(server.getURI().resolve("/connect")); String response = GET(server.getURI().resolve("/connect"));
assertThat("Response", response, startsWith("Connected to ws://")); assertThat("Response", response, startsWith("Connected to ws://"));
List<String> threadNames = getThreadNames(); List<String> threadNames = getThreadNames();
assertThat("Threads", threadNames, hasItem(containsString("HttpClient@"))); assertNoHttpClientPoolThreads(threadNames);
assertThat("Threads", threadNames, hasItem(containsString("WebSocketClient@")));
} }
finally finally
{ {
@ -286,8 +303,9 @@ public class DelayedStartClientOnServerTest
String response = GET(server.getURI().resolve("/configure")); String response = GET(server.getURI().resolve("/configure"));
assertThat("Response", response, startsWith("Configured " + ClientContainer.class.getName())); assertThat("Response", response, startsWith("Configured " + ClientContainer.class.getName()));
List<String> threadNames = getThreadNames(); List<String> threadNames = getThreadNames();
assertThat("Threads", threadNames, not(hasItem(containsString("SimpleContainerScope.Executor@")))); assertNoHttpClientPoolThreads(threadNames);
assertThat("Threads", threadNames, not(hasItem(containsString("HttpClient@")))); assertThat("Threads", threadNames, not(hasItem(containsString("WebSocketContainer@"))));
assertThat("Threads", threadNames, not(hasItem(containsString("WebSocketClient@"))));
} }
finally finally
{ {
@ -311,8 +329,8 @@ public class DelayedStartClientOnServerTest
String response = GET(server.getURI().resolve("/configure")); String response = GET(server.getURI().resolve("/configure"));
assertThat("Response", response, startsWith("Configured " + ServerContainer.class.getName())); assertThat("Response", response, startsWith("Configured " + ServerContainer.class.getName()));
List<String> threadNames = getThreadNames(); List<String> threadNames = getThreadNames();
assertThat("Threads", threadNames, not(hasItem(containsString("SimpleContainerScope.Executor@")))); assertNoHttpClientPoolThreads(threadNames);
assertThat("Threads", threadNames, not(hasItem(containsString("HttpClient@")))); assertThat("Threads", threadNames, not(hasItem(containsString("WebSocketContainer@"))));
} }
finally finally
{ {

View File

@ -39,6 +39,7 @@ import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.Scheduler; import org.eclipse.jetty.util.thread.Scheduler;
import org.eclipse.jetty.util.thread.ShutdownThread; import org.eclipse.jetty.util.thread.ShutdownThread;
import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.Session;
@ -267,7 +268,17 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
sslContextFactory = new SslContextFactory(); sslContextFactory = new SslContextFactory();
} }
this.httpClient = new HttpClient(sslContextFactory); this.httpClient = new HttpClient(sslContextFactory);
this.httpClient.setExecutor(scope.getExecutor()); Executor executor = scope.getExecutor();
if (executor == null)
{
QueuedThreadPool threadPool = new QueuedThreadPool();
String name = "WebSocketClient@" + hashCode();
threadPool.setName(name);
threadPool.setDaemon(true);
executor = threadPool;
}
this.httpClient.setExecutor(executor);
addBean(this.httpClient); addBean(this.httpClient);
this.extensionRegistry = new WebSocketExtensionFactory(containerScope); this.extensionRegistry = new WebSocketExtensionFactory(containerScope);

View File

@ -55,7 +55,7 @@ public class SimpleContainerScope extends ContainerLifeCycle implements WebSocke
this.objectFactory = objectFactory; this.objectFactory = objectFactory;
QueuedThreadPool threadPool = new QueuedThreadPool(); QueuedThreadPool threadPool = new QueuedThreadPool();
String name = SimpleContainerScope.class.getSimpleName() + ".Executor@" + hashCode(); String name = "WebSocketContainer@" + hashCode();
threadPool.setName(name); threadPool.setName(name);
threadPool.setDaemon(true); threadPool.setDaemon(true);
this.executor = threadPool; this.executor = threadPool;