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

View File

@ -19,13 +19,11 @@
package org.eclipse.jetty.websocket.jsr356.demo; package org.eclipse.jetty.websocket.jsr356.demo;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.websocket.ContainerProvider; import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException; import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer; import javax.websocket.WebSocketContainer;
public class ExampleClient public class ExampleClient
@ -45,13 +43,14 @@ public class ExampleClient
private void run() throws DeploymentException, IOException, URISyntaxException, InterruptedException private void run() throws DeploymentException, IOException, URISyntaxException, InterruptedException
{ {
WebSocketContainer container = ContainerProvider.getWebSocketContainer(); WebSocketContainer container = ContainerProvider.getWebSocketContainer();
ExampleSocket socket = new ExampleSocket(); TimeUnit.HOURS.sleep(1);
URI uri = new URI("ws://echo.websocket.org/"); // ExampleSocket socket = new ExampleSocket();
Session session = container.connectToServer(socket,uri); // URI uri = new URI("ws://echo.websocket.org/");
socket.writeMessage("Hello"); // Session session = container.connectToServer(socket,uri);
socket.messageLatch.await(1,TimeUnit.SECONDS); // give remote 1 second to respond // socket.writeMessage("Hello");
session.close(); // socket.messageLatch.await(1,TimeUnit.SECONDS); // give remote 1 second to respond
socket.closeLatch.await(1,TimeUnit.SECONDS); // give remote 1 second to acknowledge response // session.close();
System.exit(0); // 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()); eventDriverFactory.addImplementation(new JsrServerExtendsEndpointImpl());
this.webSocketServerFactory.addSessionFactory(new JsrSessionFactory(this)); this.webSocketServerFactory.addSessionFactory(new JsrSessionFactory(this));
} }
public EndpointInstance newClientEndpointInstance(Object endpoint, ServerEndpointConfig config, String path) public EndpointInstance newClientEndpointInstance(Object endpoint, ServerEndpointConfig config, String path)
{ {
EndpointMetadata metadata = getClientEndpointMetadata(endpoint.getClass()); EndpointMetadata metadata = getClientEndpointMetadata(endpoint.getClass());

View File

@ -147,6 +147,7 @@ public class WebSocketClient extends ContainerLifeCycle
LOG.debug("connect websocket {} to {}",websocket,toUri); LOG.debug("connect websocket {} to {}",websocket,toUri);
// Grab Connection Manager // Grab Connection Manager
initConnectionManager();
ConnectionManager manager = getConnectionManager(); ConnectionManager manager = getConnectionManager();
// Setup Driver for user provided websocket // Setup Driver for user provided websocket
@ -184,6 +185,28 @@ public class WebSocketClient extends ContainerLifeCycle
return promise; 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 @Override
protected void doStart() throws Exception protected void doStart() throws Exception
{ {
@ -221,9 +244,6 @@ public class WebSocketClient extends ContainerLifeCycle
cookieStore = new HttpCookieStore.Empty(); cookieStore = new HttpCookieStore.Empty();
} }
this.connectionManager = newConnectionManager();
addBean(this.connectionManager);
super.doStart(); super.doStart();
LOG.info("Started {}",this); LOG.info("Started {}",this);