418068 WebSocketClient has lazy or injected Executor

This commit is contained in:
Greg Wilkins 2013-09-26 18:06:55 +10:00
parent 643b6c3c77
commit b52d7f09b0
7 changed files with 52 additions and 33 deletions

View File

@ -48,7 +48,9 @@ public class LikeJettyXml
public static void main(String[] args) throws Exception
{
String jetty_home = System.getProperty("jetty.home","../../jetty-distribution/target/distribution");
String jetty_base = System.getProperty("jetty.home","../../jetty-distribution/target/distribution/demo-base");
System.setProperty("jetty.home",jetty_home);
System.setProperty("jetty.base",jetty_base);
// === jetty.xml ===
@ -132,7 +134,7 @@ public class LikeJettyXml
deployer.setContextAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",".*/servlet-api-[^/]*\\.jar$");
WebAppProvider webapp_provider = new WebAppProvider();
webapp_provider.setMonitoredDirName(jetty_home + "/webapps");
webapp_provider.setMonitoredDirName(jetty_base + "/webapps");
webapp_provider.setDefaultsDescriptor(jetty_home + "/etc/webdefault.xml");
webapp_provider.setScanInterval(1);
webapp_provider.setExtractWars(true);
@ -176,7 +178,7 @@ public class LikeJettyXml
// === test-realm.xml ===
HashLoginService login = new HashLoginService();
login.setName("Test Realm");
login.setConfig(jetty_home + "/etc/realm.properties");
login.setConfig(jetty_base + "/etc/realm.properties");
login.setRefreshInterval(0);
server.addBean(login);

View File

@ -18,6 +18,9 @@
package org.eclipse.jetty.embedded;
import java.lang.management.ManagementFactory;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
@ -30,6 +33,10 @@ public class OneWebApp
// a randomly available port will be assigned that you can either look in the logs for the port,
// or programmatically obtain it for use in test cases.
Server server = new Server(8080);
// Setup JMX
MBeanContainer mbContainer=new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
server.addBean(mbContainer);
// The WebAppContext is the entity that controls the environment in which a web application lives and
// breathes. In this example the context path is being set to "/" so it is suitable for serving root context
@ -38,7 +45,7 @@ public class OneWebApp
// PlusConfiguration) to choosing where the webapp will unpack itself.
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar("../../tests/test-webapps/test-jetty-webapp/target/test-jetty-webapp-9.0.0-SNAPSHOT.war");
webapp.setWar("../../jetty-distribution/target/distribution/demo-base/webapps/test.war");
// A WebAppContext is a ContextHandler as well so it needs to be set to the server so it is aware of where to
// send the appropriate requests.

View File

@ -26,6 +26,7 @@ import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import javax.websocket.ClientEndpoint;
@ -71,6 +72,11 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
private WebSocketClient client;
public ClientContainer()
{
this(null);
}
public ClientContainer(Executor executor)
{
endpointClientMetadataCache = new ConcurrentHashMap<>();
decoderFactory = new DecoderFactory(PrimitiveDecoderMetadataSet.INSTANCE);
@ -80,7 +86,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
decoderFactory.init(empty);
encoderFactory.init(empty);
client = new WebSocketClient();
client = new WebSocketClient(executor);
client.setEventDriverFactory(new JsrEventDriverFactory(client.getPolicy()));
client.setSessionFactory(new JsrSessionFactory(this));
addBean(client);

View File

@ -2,21 +2,7 @@
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Add javax.websocket Configuring classes to all webapps for this Server -->
<!-- =========================================================== -->
<Call class="org.eclipse.jetty.webapp.Configuration$ClassList" name="setServerDefault">
<Arg><Ref refid="Server" /></Arg>
<Call name="addBefore">
<Arg name="beforeClass">org.eclipse.jetty.annotations.AnnotationConfiguration</Arg>
<Arg>
<Array type="String">
<Item>org.eclipse.jetty.websocket.jsr356.server.WebSocketConfiguration</Item>
</Array>
</Arg>
</Call>
</Call>
<!-- Should a Websocket container be initialized for each context? -->
<!-- Can be overridden by context attribute -->
<Call name="setAttribute">

View File

@ -18,6 +18,8 @@
package org.eclipse.jetty.websocket.jsr356.server;
import java.util.concurrent.Executor;
import javax.websocket.DeploymentException;
import javax.websocket.Endpoint;
import javax.websocket.server.ServerEndpoint;
@ -42,9 +44,9 @@ public class ServerContainer extends ClientContainer implements javax.websocket.
private final MappedWebSocketCreator mappedCreator;
private final WebSocketServerFactory webSocketServerFactory;
public ServerContainer(MappedWebSocketCreator creator, WebSocketServerFactory factory)
public ServerContainer(MappedWebSocketCreator creator, WebSocketServerFactory factory, Executor executor)
{
super();
super(executor);
this.mappedCreator = creator;
this.webSocketServerFactory = factory;
EventDriverFactory eventDriverFactory = this.webSocketServerFactory.getEventDriverFactory();

View File

@ -65,7 +65,7 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit
context.setAttribute(WebSocketUpgradeFilter.class.getName(),filter);
// Create the Jetty ServerContainer implementation
ServerContainer jettyContainer = new ServerContainer(filter,filter.getFactory());
ServerContainer jettyContainer = new ServerContainer(filter,filter.getFactory(),context.getServer().getThreadPool());
context.addBean(jettyContainer);
// Store a reference to the ServerContainer per javax.websocket spec 1.0 final section 6.4 Programmatic Server Deployment

View File

@ -79,11 +79,22 @@ public class WebSocketClient extends ContainerLifeCycle
public WebSocketClient()
{
this(null);
this(null,null);
}
public WebSocketClient(Executor executor)
{
this(executor,null);
}
public WebSocketClient(SslContextFactory sslContextFactory)
{
this(null,sslContextFactory);
}
public WebSocketClient(Executor executor, SslContextFactory sslContextFactory)
{
this.executor=executor;
this.sslContextFactory = sslContextFactory;
this.policy = WebSocketPolicy.newClientPolicy();
this.bufferPool = new MappedByteBufferPool();
@ -93,6 +104,7 @@ public class WebSocketClient extends ContainerLifeCycle
this.sessionFactory = new WebSocketSessionFactory();
}
public Future<Session> connect(Object websocket, URI toUri) throws IOException
{
ClientUpgradeRequest request = new ClientUpgradeRequest(toUri);
@ -147,7 +159,7 @@ public class WebSocketClient extends ContainerLifeCycle
LOG.debug("connect websocket {} to {}",websocket,toUri);
// Grab Connection Manager
initConnectionManager();
initialiseClient();
ConnectionManager manager = getConnectionManager();
// Setup Driver for user provided websocket
@ -185,8 +197,19 @@ public class WebSocketClient extends ContainerLifeCycle
return promise;
}
private synchronized void initConnectionManager() throws IOException
private synchronized void initialiseClient() throws IOException
{
if (executor == null)
{
QueuedThreadPool threadPool = new QueuedThreadPool();
String name = WebSocketClient.class.getSimpleName() + "@" + hashCode();
threadPool.setName(name);
executor = threadPool;
addBean(executor,true);
}
else
addBean(executor,false);
if (connectionManager != null)
{
return;
@ -219,14 +242,6 @@ public class WebSocketClient extends ContainerLifeCycle
String name = WebSocketClient.class.getSimpleName() + "@" + hashCode();
if (executor == null)
{
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setName(name);
executor = threadPool;
}
addBean(executor);
if (bufferPool == null)
{
bufferPool = new MappedByteBufferPool();
@ -465,6 +480,7 @@ public class WebSocketClient extends ContainerLifeCycle
public void setExecutor(Executor executor)
{
updateBean(this.executor,executor);
this.executor = executor;
}