416812 - Don't start WebSocketClient for every context

+ Lazy initializing WebSocketClient connection manager
  to avoid holding threads that the selector manager
  will allocate.
This commit is contained in:
Joakim Erdfelt 2013-09-09 12:06:57 -07:00
parent b1b8e2b0a1
commit aeac72a684
4 changed files with 39 additions and 15 deletions

View File

@ -68,7 +68,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
/** Tracking for all declared Client endpoints */
private final Map<Class<?>, EndpointMetadata> endpointClientMetadataCache;
/** The jetty websocket client in use for this container */
private final WebSocketClient client;
private WebSocketClient client;
public ClientContainer()
{
@ -179,6 +179,11 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
super.doStop();
}
public WebSocketClient getClient()
{
return client;
}
public EndpointMetadata getClientEndpointMetadata(Class<?> endpoint)
{
EndpointMetadata metadata = null;

View File

@ -19,13 +19,11 @@
package org.eclipse.jetty.websocket.jsr356.demo;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
public class ExampleClient
@ -45,13 +43,14 @@ public class ExampleClient
private void run() throws DeploymentException, IOException, URISyntaxException, InterruptedException
{
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
ExampleSocket socket = new ExampleSocket();
URI uri = new URI("ws://echo.websocket.org/");
Session session = container.connectToServer(socket,uri);
socket.writeMessage("Hello");
socket.messageLatch.await(1,TimeUnit.SECONDS); // give remote 1 second to respond
session.close();
socket.closeLatch.await(1,TimeUnit.SECONDS); // give remote 1 second to acknowledge response
System.exit(0);
TimeUnit.HOURS.sleep(1);
// ExampleSocket socket = new ExampleSocket();
// URI uri = new URI("ws://echo.websocket.org/");
// Session session = container.connectToServer(socket,uri);
// socket.writeMessage("Hello");
// socket.messageLatch.await(1,TimeUnit.SECONDS); // give remote 1 second to respond
// session.close();
// socket.closeLatch.await(1,TimeUnit.SECONDS); // give remote 1 second to acknowledge response
// System.exit(0);
}
}

View File

@ -52,7 +52,7 @@ public class ServerContainer extends ClientContainer implements javax.websocket.
eventDriverFactory.addImplementation(new JsrServerExtendsEndpointImpl());
this.webSocketServerFactory.addSessionFactory(new JsrSessionFactory(this));
}
public EndpointInstance newClientEndpointInstance(Object endpoint, ServerEndpointConfig config, String path)
{
EndpointMetadata metadata = getClientEndpointMetadata(endpoint.getClass());

View File

@ -147,6 +147,7 @@ public class WebSocketClient extends ContainerLifeCycle
LOG.debug("connect websocket {} to {}",websocket,toUri);
// Grab Connection Manager
initConnectionManager();
ConnectionManager manager = getConnectionManager();
// Setup Driver for user provided websocket
@ -184,6 +185,28 @@ public class WebSocketClient extends ContainerLifeCycle
return promise;
}
private synchronized void initConnectionManager() throws IOException
{
if (connectionManager != null)
{
return;
}
try
{
connectionManager = newConnectionManager();
addBean(connectionManager);
connectionManager.start();
}
catch (IOException e)
{
throw e;
}
catch (Exception e)
{
throw new IOException(e);
}
}
@Override
protected void doStart() throws Exception
{
@ -221,9 +244,6 @@ public class WebSocketClient extends ContainerLifeCycle
cookieStore = new HttpCookieStore.Empty();
}
this.connectionManager = newConnectionManager();
addBean(this.connectionManager);
super.doStart();
LOG.info("Started {}",this);