localProperties = new HashMap<String, Object>(properties);
}
final Integer incomingBufferSize = Utils.getProperty(localProperties, TyrusWebSocketEngine.INCOMING_BUFFER_SIZE, Integer.class);
final ClusterContext clusterContext = Utils.getProperty(localProperties, ClusterContext.CLUSTER_CONTEXT, ClusterContext.class);
final ApplicationEventListener applicationEventListener = Utils.getProperty(localProperties, ApplicationEventListener.APPLICATION_EVENT_LISTENER, ApplicationEventListener.class);
final Integer maxSessionsPerApp = Utils.getProperty(localProperties, TyrusWebSocketEngine.MAX_SESSIONS_PER_APP, Integer.class);
final Integer maxSessionsPerRemoteAddr = Utils.getProperty(localProperties, TyrusWebSocketEngine.MAX_SESSIONS_PER_REMOTE_ADDR, Integer.class);
return new TyrusServerContainer((Set<Class<?>>) null) {
private final WebSocketEngine engine = TyrusWebSocketEngine.builder(this)
.incomingBufferSize(incomingBufferSize)
.clusterContext(clusterContext)
.applicationEventListener(applicationEventListener)
.maxSessionsPerApp(maxSessionsPerApp)
.maxSessionsPerRemoteAddr(maxSessionsPerRemoteAddr)
.build();
private HttpServer server;
private String contextPath;
@Override
public void register(Class<?> endpointClass) throws DeploymentException {
engine.register(endpointClass, contextPath);
}
@Override
public void register(ServerEndpointConfig serverEndpointConfig) throws DeploymentException {
engine.register(serverEndpointConfig, contextPath);
}
@Override
public WebSocketEngine getWebSocketEngine() {
return engine;
}
@Override
public void start(final String rootPath, int port) throws IOException, DeploymentException {
contextPath = rootPath;
server = new HttpServer();
final ServerConfiguration config = server.getServerConfiguration();
final NetworkListener listener =
new NetworkListener("grizzly",
"0.0.0.0",
port);
server.addListener(listener);
// server = HttpServer.createSimpleServer(rootPath, port);
ThreadPoolConfig workerThreadPoolConfig = Utils.getProperty(localProperties, WORKER_THREAD_POOL_CONFIG, ThreadPoolConfig.class);
ThreadPoolConfig selectorThreadPoolConfig = Utils.getProperty(localProperties, SELECTOR_THREAD_POOL_CONFIG, ThreadPoolConfig.class);
// TYRUS-287: configurable server thread pools
if (workerThreadPoolConfig != null || selectorThreadPoolConfig != null) {
TCPNIOTransportBuilder transportBuilder = TCPNIOTransportBuilder.newInstance();
if (workerThreadPoolConfig != null) {
transportBuilder.setWorkerThreadPoolConfig(workerThreadPoolConfig);
}
if (selectorThreadPoolConfig != null) {
transportBuilder.setSelectorThreadPoolConfig(selectorThreadPoolConfig);
}
transportBuilder.setIOStrategy(WorkerThreadIOStrategy.getInstance());
server.getListener("grizzly").setTransport(transportBuilder.build());
} else {
// if no configuration is set, just update IO Strategy to worker thread strat.
server.getListener("grizzly").getTransport().setIOStrategy(WorkerThreadIOStrategy.getInstance());
}
// idle timeout set to indefinite.
server.getListener("grizzly").getKeepAlive().setIdleTimeoutInSeconds(-1);
server.getListener("grizzly").registerAddOn(new WebSocketAddOn(this));
final WebSocketEngine webSocketEngine = getWebSocketEngine();
final Object staticContentPath = localProperties.get(Server.STATIC_CONTENT_ROOT);
HttpHandler staticHandler = null;
if (staticContentPath != null && !staticContentPath.toString().isEmpty()) {
staticHandler = new StaticHttpHandler(staticContentPath.toString());
}
final Object wsadl = localProperties.get(TyrusWebSocketEngine.WSADL_SUPPORT);
if (wsadl != null && wsadl.toString().equalsIgnoreCase("true")) { // wsadl enabled
config.addHttpHandler(new WsadlHttpHandler((TyrusWebSocketEngine) webSocketEngine, staticHandler));
} else if (staticHandler != null) { // wsadl disabled
config.addHttpHandler(staticHandler);
}
if (applicationEventListener != null) {
applicationEventListener.onApplicationInitialized(rootPath);
}
server.start();
super.start(rootPath, port);
}
@Override
public void stop() {
super.stop();
server.shutdownNow();
if (applicationEventListener != null) {
applicationEventListener.onApplicationDestroyed();
}
}
};
}