423646 - WebSocket / JSR / WebSocketContainer (Client) should have its LifeCycle stop on standalone use

+ Not working (yet), but narrowed down the problem.
This commit is contained in:
Joakim Erdfelt 2013-12-18 05:51:24 -07:00
parent 2b0db585ef
commit 9a20a534b5
4 changed files with 54 additions and 11 deletions

View File

@ -100,6 +100,8 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
client.setEventDriverFactory(new JsrEventDriverFactory(client.getPolicy()));
client.setSessionFactory(new JsrSessionFactory(this,this));
addBean(client);
ShutdownThread.register(this);
}
private Session connect(EndpointInstance instance, URI path) throws IOException
@ -180,18 +182,10 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
return connect(instance,path);
}
@Override
protected void doStart() throws Exception
{
super.doStart();
ShutdownThread.register(client);
}
@Override
protected void doStop() throws Exception
{
endpointClientMetadataCache.clear();
ShutdownThread.deregister(client);
super.doStop();
}

View File

@ -21,15 +21,51 @@ package org.eclipse.jetty.websocket.jsr356.demo;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
public class ExampleClient
{
@ClientEndpoint
public class ExampleSocket
{
public String message;
public CountDownLatch messageLatch = new CountDownLatch(1);
public CountDownLatch closeLatch = new CountDownLatch(1);
@OnOpen
public void onOpen(Session session)
{
System.out.println("Opened");
}
@OnMessage
public void onMessage(String msg)
{
System.out.printf("Received: %s%n",Objects.toString(msg));
this.messageLatch.countDown();
}
@OnClose
public void onClose(CloseReason close)
{
System.out.printf("Closed: %d, %s%n",close.getCloseCode().getCode(),Objects.toString(close.getReasonPhrase()));
this.closeLatch.countDown();
}
}
public static void main(String[] args)
{
try
@ -45,13 +81,19 @@ public class ExampleClient
private void run() throws DeploymentException, IOException, URISyntaxException, InterruptedException
{
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
System.out.printf("WebSocketContainer Impl: %s%n",container.getClass().getName());
ExampleSocket socket = new ExampleSocket();
URI uri = new URI("ws://echo.websocket.org/");
Session session = container.connectToServer(socket,uri);
socket.writeMessage("Hello");
RemoteEndpoint.Basic remote = session.getBasicRemote();
String msg = "Hello world";
System.out.printf("Sending: %s%n",Objects.toString(msg));
remote.sendText(msg);
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);
System.out.println("Socket is closed");
}
}

View File

@ -1,5 +1,5 @@
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
org.eclipse.jetty.LEVEL=WARN
org.eclipse.jetty.LEVEL=DEBUG
# org.eclipse.jetty.websocket.LEVEL=WARN
# org.eclipse.jetty.websocket.jsr356.LEVEL=DEBUG

View File

@ -43,6 +43,7 @@ import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
import org.eclipse.jetty.util.thread.Scheduler;
import org.eclipse.jetty.util.thread.ShutdownThread;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.api.extensions.Extension;
@ -108,6 +109,10 @@ public class WebSocketClient extends ContainerLifeCycle implements SessionListen
this.masker = new RandomMasker();
this.eventDriverFactory = new EventDriverFactory(policy);
this.sessionFactory = new WebSocketSessionFactory(this);
addBean(this.executor);
addBean(this.sslContextFactory);
addBean(this.bufferPool);
}
public Future<Session> connect(Object websocket, URI toUri) throws IOException
@ -407,6 +412,8 @@ public class WebSocketClient extends ContainerLifeCycle implements SessionListen
private synchronized void initialiseClient() throws IOException
{
ShutdownThread.register(this);
if (executor == null)
{
QueuedThreadPool threadPool = new QueuedThreadPool();