use of org.glassfish.grizzly.http.server.NetworkListener in project OpenTripPlanner by opentripplanner.
the class GrizzlyServer method run.
/**
* This function goes through roughly the same steps as Jersey's GrizzlyServerFactory, but we instead construct
* an HttpServer and NetworkListener manually so we can set the number of threads and other details.
*/
public void run() {
LOG.info("Starting OTP Grizzly server on ports {} (HTTP) and {} (HTTPS) of interface {}", params.port, params.securePort, params.bindAddress);
LOG.info("OTP server base path is {}", params.basePath);
HttpServer httpServer = new HttpServer();
/* Configure SSL */
SSLContextConfigurator sslConfig = new SSLContextConfigurator();
sslConfig.setKeyStoreFile(new File(params.basePath, "keystore").getAbsolutePath());
sslConfig.setKeyStorePass("opentrip");
/* Set up a pool of threads to handle incoming HTTP requests. */
// TODO we should probably use Grizzly async processing rather than tying up the HTTP handler threads.
ThreadPoolConfig threadPoolConfig = ThreadPoolConfig.defaultConfig().setCorePoolSize(MIN_THREADS).setMaxPoolSize(getMaxThreads());
/* HTTP (non-encrypted) listener */
NetworkListener httpListener = new NetworkListener("otp_insecure", params.bindAddress, params.port);
httpListener.setSecure(false);
/* HTTPS listener */
NetworkListener httpsListener = new NetworkListener("otp_secure", params.bindAddress, params.securePort);
// Ideally we'd share the threads between HTTP and HTTPS.
httpsListener.setSecure(true);
httpsListener.setSSLEngineConfig(new SSLEngineConfigurator(sslConfig).setClientMode(false).setNeedClientAuth(false));
// For both HTTP and HTTPS listeners: enable gzip compression, set thread pool, add listener to httpServer.
for (NetworkListener listener : new NetworkListener[] { httpListener, httpsListener }) {
CompressionConfig cc = listener.getCompressionConfig();
cc.setCompressionMode(CompressionConfig.CompressionMode.ON);
// the min number of bytes to compress
cc.setCompressionMinSize(50000);
// the mime types to compress
cc.setCompressableMimeTypes("application/json", "text/json");
listener.getTransport().setWorkerThreadPoolConfig(threadPoolConfig);
httpServer.addListener(listener);
}
/* Add a few handlers (~= servlets) to the Grizzly server. */
/* 1. A Grizzly wrapper around the Jersey Application. */
Application app = new OTPApplication(server, !params.insecure);
HttpHandler dynamicHandler = ContainerFactory.createContainer(HttpHandler.class, app);
httpServer.getServerConfiguration().addHttpHandler(dynamicHandler, "/otp/");
/* 2. A static content handler to serve the client JS apps etc. from the classpath. */
CLStaticHttpHandler staticHandler = new CLStaticHttpHandler(GrizzlyServer.class.getClassLoader(), "/client/");
if (params.disableFileCache) {
LOG.info("Disabling HTTP server static file cache.");
staticHandler.setFileCacheEnabled(false);
}
httpServer.getServerConfiguration().addHttpHandler(staticHandler, "/");
/* 3. A static content handler to serve local files from the filesystem, under the "local" path. */
if (params.clientDirectory != null) {
StaticHttpHandler localHandler = new StaticHttpHandler(params.clientDirectory.getAbsolutePath());
localHandler.setFileCacheEnabled(false);
httpServer.getServerConfiguration().addHttpHandler(localHandler, "/local");
}
/* 3. Test alternate HTTP handling without Jersey. */
// As in servlets, * is needed in base path to identify the "rest" of the path.
// GraphService gs = (GraphService) iocFactory.getComponentProvider(GraphService.class).getInstance();
// Graph graph = gs.getGraph();
// httpServer.getServerConfiguration().addHttpHandler(new OTPHttpHandler(graph), "/test/*");
// Add shutdown hook to gracefully shut down Grizzly.
// Signal handling (sun.misc.Signal) is potentially not available on all JVMs.
Thread shutdownThread = new Thread(httpServer::shutdown);
Runtime.getRuntime().addShutdownHook(shutdownThread);
/* RELINQUISH CONTROL TO THE SERVER THREAD */
try {
httpServer.start();
LOG.info("Grizzly server running.");
Thread.currentThread().join();
} catch (BindException be) {
LOG.error("Cannot bind to port {}. Is it already in use?", params.port);
} catch (IOException ioe) {
LOG.error("IO exception while starting server.");
} catch (InterruptedException ie) {
LOG.info("Interrupted, shutting down.");
}
// Clean up graceful shutdown hook before shutting down Grizzly.
Runtime.getRuntime().removeShutdownHook(shutdownThread);
httpServer.shutdown();
}
use of org.glassfish.grizzly.http.server.NetworkListener in project jersey by jersey.
the class Main method startServer.
public static HttpServer startServer(String webRootPath) {
final HttpServer server = new HttpServer();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
server.shutdownNow();
}
}));
final NetworkListener listener = new NetworkListener("grizzly", "localhost", PORT);
server.addListener(listener);
final ServerConfiguration config = server.getServerConfiguration();
// add handler for serving static content
config.addHttpHandler(new CLStaticHttpHandler(Main.class.getClassLoader(), WEB_ROOT), APP_PATH);
// add handler for serving JAX-RS resources
config.addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(createResourceConfig(), GrizzlyHttpContainer.class), APP_PATH);
try {
// Start the server.
server.start();
} catch (Exception ex) {
throw new ProcessingException("Exception thrown when trying to start grizzly server", ex);
}
return server;
}
use of org.glassfish.grizzly.http.server.NetworkListener in project jersey by jersey.
the class Main method startServer.
public static HttpServer startServer(String webRootPath) {
final HttpServer server = new HttpServer();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
server.shutdownNow();
}
}));
final NetworkListener listener = new NetworkListener("grizzly", "localhost", PORT);
server.addListener(listener);
final ServerConfiguration config = server.getServerConfiguration();
// add handler for serving static content
config.addHttpHandler(new CLStaticHttpHandler(Main.class.getClassLoader(), WEB_ROOT), APP_PATH);
// add handler for serving JAX-RS resources
config.addHttpHandler(RuntimeDelegate.getInstance().createEndpoint(createResourceConfig(), GrizzlyHttpContainer.class), APP_PATH);
try {
// Start the server.
server.start();
} catch (Exception ex) {
throw new ProcessingException("Exception thrown when trying to start grizzly server", ex);
}
return server;
}
use of org.glassfish.grizzly.http.server.NetworkListener in project dukescript-presenters by dukescript.
the class DynamicHTTP method pageURL.
private static URI pageURL(String proto, HttpServer server, final String page) {
NetworkListener listener = server.getListeners().iterator().next();
int port = listener.getPort();
try {
return new URI(proto + "://localhost:" + port + page);
} catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
}
}
use of org.glassfish.grizzly.http.server.NetworkListener in project weblogic-kubernetes-operator by oracle.
the class RestServer method createHttpsServer.
private HttpServer createHttpsServer(Container container, SSLContext ssl, String uri) throws Exception {
HttpServer h = GrizzlyHttpServerFactory.createHttpServer(URI.create(uri), createResourceConfig(), // used for call org.glassfish.jersey.grizzly2.httpserver.NetworkListener#setSecure(boolean)}.
true, new SSLEngineConfigurator(ssl).setClientMode(false).setNeedClientAuth(false).setEnabledProtocols(SSL_PROTOCOLS), false);
// We discovered the default thread pool configuration was generating hundreds of
// threads. Tune it down to something more modest. Note: these are core
// pool sizes, so they can still grow if there is sufficient load.
Collection<NetworkListener> nlc = h.getListeners();
if (nlc != null) {
for (NetworkListener nl : nlc) {
TCPNIOTransport transport = nl.getTransport();
ThreadPoolConfig t = transport.getWorkerThreadPoolConfig();
if (t == null) {
t = ThreadPoolConfig.defaultConfig();
transport.setWorkerThreadPoolConfig(t);
}
t.setCorePoolSize(CORE_POOL_SIZE);
ThreadFactory x = t.getThreadFactory();
ThreadFactory tf = x != null ? x : Executors.defaultThreadFactory();
t.setThreadFactory((r) -> {
Thread n = tf.newThread(() -> {
ContainerResolver.getDefault().enterContainer(container);
r.run();
});
if (!n.isDaemon()) {
n.setDaemon(true);
}
return n;
});
t = transport.getKernelThreadPoolConfig();
if (t == null) {
t = ThreadPoolConfig.defaultConfig();
transport.setKernelThreadPoolConfig(t);
}
t.setCorePoolSize(CORE_POOL_SIZE);
x = t.getThreadFactory();
ThreadFactory tf2 = x != null ? x : Executors.defaultThreadFactory();
t.setThreadFactory((r) -> {
Thread n = tf2.newThread(() -> {
ContainerResolver.getDefault().enterContainer(container);
r.run();
});
if (!n.isDaemon()) {
n.setDaemon(true);
}
return n;
});
transport.setSelectorRunnersCount(CORE_POOL_SIZE);
}
}
h.start();
return h;
}
Aggregations