Issue #1605 - common executor and bufferpool for jsr356 client

This commit is contained in:
Joakim Erdfelt 2017-06-12 09:23:23 -07:00
parent 10a6e5fa5e
commit 67022482e5
3 changed files with 57 additions and 14 deletions

View File

@ -19,14 +19,20 @@
package org.eclipse.jetty.websocket.jsr356;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.MappedByteBufferPool;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ShutdownThread;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope;
/**
* Client {@link ContainerProvider} implementation.
@ -43,6 +49,8 @@ public class JettyClientContainerProvider extends ContainerProvider
private static boolean useSingleton = false;
private static WebSocketContainer INSTANCE;
private static boolean useServerContainer = false;
private static Executor commonExecutor;
private static ByteBufferPool commonBufferPool;
private static Object lock = new Object();
@ -177,8 +185,22 @@ public class JettyClientContainerProvider extends ContainerProvider
// Still no instance?
if (webSocketContainer == null)
{
// TODO: use cached Executor, ByteBufferPool, and HttpClient here
ClientContainer clientContainer = new ClientContainer();
if (commonExecutor == null)
{
QueuedThreadPool threadPool = new QueuedThreadPool();
String name = "Jsr356Client@" + hashCode();
threadPool.setName(name);
threadPool.setDaemon(true);
commonExecutor = threadPool;
}
if (commonBufferPool == null)
{
commonBufferPool = new MappedByteBufferPool();
}
SimpleContainerScope containerScope = new SimpleContainerScope(WebSocketPolicy.newClientPolicy(), commonBufferPool, commonExecutor, null);
ClientContainer clientContainer = new ClientContainer(containerScope);
if (contextHandler != null && contextHandler instanceof ContainerLifeCycle)
{
@ -192,7 +214,7 @@ public class JettyClientContainerProvider extends ContainerProvider
// register JVM wide shutdown thread
ShutdownThread.register(clientContainer);
}
if (!clientContainer.isStarted())
{
try

View File

@ -247,7 +247,7 @@ public class DelayedStartClientOnServerTest
assertThat("Response", response, startsWith("Connected to ws://"));
List<String> threadNames = getThreadNames(server);
assertNoHttpClientPoolThreads(threadNames);
assertThat("Threads", threadNames, hasItem(containsString("WebSocketContainer@")));
assertThat("Threads", threadNames, hasItem(containsString("Jsr356Client@")));
}
finally
{

View File

@ -34,31 +34,52 @@ public class SimpleContainerScope extends ContainerLifeCycle implements WebSocke
private final ByteBufferPool bufferPool;
private final DecoratedObjectFactory objectFactory;
private final WebSocketPolicy policy;
private Executor executor;
private final Executor executor;
private SslContextFactory sslContextFactory;
public SimpleContainerScope(WebSocketPolicy policy)
{
this(policy,new MappedByteBufferPool(),new DecoratedObjectFactory());
this(policy, new MappedByteBufferPool(), new DecoratedObjectFactory());
this.sslContextFactory = new SslContextFactory();
}
public SimpleContainerScope(WebSocketPolicy policy, ByteBufferPool bufferPool)
{
this(policy,bufferPool,new DecoratedObjectFactory());
this(policy, bufferPool, new DecoratedObjectFactory());
}
public SimpleContainerScope(WebSocketPolicy policy, ByteBufferPool bufferPool, DecoratedObjectFactory objectFactory)
{
this(policy, bufferPool, (Executor) null, objectFactory);
}
public SimpleContainerScope(WebSocketPolicy policy, ByteBufferPool bufferPool, Executor executor, DecoratedObjectFactory objectFactory)
{
this.policy = policy;
this.bufferPool = bufferPool;
this.objectFactory = objectFactory;
QueuedThreadPool threadPool = new QueuedThreadPool();
String name = "WebSocketContainer@" + hashCode();
threadPool.setName(name);
threadPool.setDaemon(true);
this.executor = threadPool;
if (objectFactory == null)
{
this.objectFactory = new DecoratedObjectFactory();
}
else
{
this.objectFactory = objectFactory;
}
if (executor == null)
{
QueuedThreadPool threadPool = new QueuedThreadPool();
String name = "WebSocketContainer@" + hashCode();
threadPool.setName(name);
threadPool.setDaemon(true);
this.executor = threadPool;
addBean(this.executor);
}
else
{
this.executor = executor;
}
}
@Override