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 0de3673132
commit af4962f842
3 changed files with 57 additions and 14 deletions

View File

@ -19,14 +19,20 @@
package org.eclipse.jetty.websocket.jsr356; package org.eclipse.jetty.websocket.jsr356;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import javax.websocket.ContainerProvider; import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer; 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.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.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ShutdownThread; 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. * Client {@link ContainerProvider} implementation.
@ -43,6 +49,8 @@ public class JettyClientContainerProvider extends ContainerProvider
private static boolean useSingleton = false; private static boolean useSingleton = false;
private static WebSocketContainer INSTANCE; private static WebSocketContainer INSTANCE;
private static boolean useServerContainer = false; private static boolean useServerContainer = false;
private static Executor commonExecutor;
private static ByteBufferPool commonBufferPool;
private static Object lock = new Object(); private static Object lock = new Object();
@ -177,8 +185,22 @@ public class JettyClientContainerProvider extends ContainerProvider
// Still no instance? // Still no instance?
if (webSocketContainer == null) if (webSocketContainer == null)
{ {
// TODO: use cached Executor, ByteBufferPool, and HttpClient here if (commonExecutor == null)
ClientContainer clientContainer = new ClientContainer(); {
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) if (contextHandler != null && contextHandler instanceof ContainerLifeCycle)
{ {
@ -192,7 +214,7 @@ public class JettyClientContainerProvider extends ContainerProvider
// register JVM wide shutdown thread // register JVM wide shutdown thread
ShutdownThread.register(clientContainer); ShutdownThread.register(clientContainer);
} }
if (!clientContainer.isStarted()) if (!clientContainer.isStarted())
{ {
try try

View File

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

View File

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