Issue #1604 - WebSocketContainer stop is now smarter

This commit is contained in:
Joakim Erdfelt 2017-06-07 11:48:36 -07:00
parent ab98caafbb
commit 5b72d8a733
2 changed files with 49 additions and 12 deletions

View File

@ -149,8 +149,6 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
this.endpointClientMetadataCache = new ConcurrentHashMap<>();
this.decoderFactory = new DecoderFactory(this,PrimitiveDecoderMetadataSet.INSTANCE);
this.encoderFactory = new EncoderFactory(this,PrimitiveEncoderMetadataSet.INSTANCE);
ShutdownThread.register(this);
}
/**

View File

@ -23,6 +23,9 @@ import java.lang.reflect.Method;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.thread.ShutdownThread;
/**
* Client {@link ContainerProvider} implementation.
* <p>
@ -59,6 +62,28 @@ public class JettyClientContainerProvider extends ContainerProvider
}
}
public Object getContextHandler()
{
try
{
// Equiv of: ContextHandler.Context context = ContextHandler.getCurrentContext()
Class<?> clazzContextHandler = Class.forName("org.eclipse.jetty.server.handler.ContextHandler");
Method methodGetContext = clazzContextHandler.getMethod("getCurrentContext");
Object objContext = methodGetContext.invoke(null);
if (objContext == null)
return null;
// Equiv of: ContextHandler handler = ContextHandler.getContextHandler(context);
Class<?> clazzContext = objContext.getClass();
Method methodGetContextHandler = clazzContextHandler.getMethod("getContextHandler", clazzContext);
return methodGetContextHandler.invoke(objContext);
}
catch (Throwable ignore)
{
return null;
}
}
/**
* Used by {@link ContainerProvider#getWebSocketContainer()} to get a new instance
* of the Client {@link WebSocketContainer}.
@ -81,24 +106,38 @@ public class JettyClientContainerProvider extends ContainerProvider
catch (Throwable ignore)
{
}
if (INSTANCE == null)
{
INSTANCE = new ClientContainer();
}
if (!INSTANCE.isStarted())
{
try
Object contextHandler = getContextHandler();
if (contextHandler != null && contextHandler instanceof ContainerLifeCycle)
{
INSTANCE.start();
// Add as bean to contextHandler
// Allow startup to follow Jetty lifecycle
((ContainerLifeCycle) contextHandler).addBean(INSTANCE, true);
}
catch (Exception e)
else
{
throw new RuntimeException("Unable to start Client Container", e);
// Static Initialization
// register JVM wide shutdown thread
ShutdownThread.register(INSTANCE);
if (!INSTANCE.isStarted())
{
try
{
INSTANCE.start();
}
catch (Exception e)
{
throw new RuntimeException("Unable to start Client Container", e);
}
}
}
}
return INSTANCE;
}
}