/** * Returns a new handler that can be used to wait for all requests to finish before shutting down the server gracefully. * * @param next The next http handler * @return The graceful shutdown handler */ public static GracefulShutdownHandler gracefulShutdown(HttpHandler next) { return new GracefulShutdownHandler(next); }
@Override public void stop() throws Exception { shutdown.shutdown(); shutdown.awaitShutdown(awaitShutdown); server.stop(); }
public void shutdown() { activeRequestsUpdater.incrementAndGet(this); //the request count is never zero when shutdown is set to true shutdown = true; decrementRequests(); }
private void decrementRequests() { if (shutdown) { //we don't read the request count until after checking the shutdown variable //otherwise we could read the request count as zero, a new request could state, and then we shutdown //see https://issues.jboss.org/browse/UNDERTOW-1099 long active = activeRequestsUpdater.decrementAndGet(this); synchronized (lock) { if (active == 0) { shutdownComplete(); } } } else { activeRequestsUpdater.decrementAndGet(this); } }
private void decrementRequests() { if (shutdown) { //we don't read the request count until after checking the shutdown variable //otherwise we could read the request count as zero, a new request could state, and then we shutdown //see https://issues.jboss.org/browse/UNDERTOW-1099 long active = activeRequestsUpdater.decrementAndGet(this); synchronized (lock) { if (active == 0) { shutdownComplete(); } } } else { activeRequestsUpdater.decrementAndGet(this); } }
@Inject public UndertowServer(final org.jooby.spi.HttpHandler dispatcher, final Config conf, final Provider<SSLContext> sslContext) throws Exception { awaitShutdown = conf.getDuration("undertow.awaitShutdown", TimeUnit.MILLISECONDS); boolean http2 = conf.getBoolean("server.http2.enabled"); shutdown = new GracefulShutdownHandler(doHandler(dispatcher, conf)); Undertow.Builder ubuilder = configure(conf, io.undertow.Undertow.builder()) .addHttpListener(conf.getInt("application.port"), host(conf.getString("application.host"))); ubuilder.setServerOption(UndertowOptions.ENABLE_HTTP2, http2); boolean securePort = conf.hasPath("application.securePort"); if (securePort) { ubuilder.addHttpsListener(conf.getInt("application.securePort"), host(conf.getString("application.host")), sslContext.get()); } this.server = ubuilder.setHandler(shutdown) .build(); }
static public void shutdown() { // need to unregister the service if (config.enableRegistry && registry != null) { registry.unregister(serviceUrl); // Please don't remove the following line. When server is killed, the logback // won't work anymore. // Even debugger won't reach this point; however, the logic is executed // successfully here. System.out.println("unregister serviceUrl " + serviceUrl); if (logger.isInfoEnabled()) logger.info("unregister serviceUrl " + serviceUrl); } if (gracefulShutdownHandler != null) { logger.info("Starting graceful shutdown."); gracefulShutdownHandler.shutdown(); try { gracefulShutdownHandler.awaitShutdown(60 * 1000); } catch (InterruptedException e) { logger.error("Error occurred while waiting for pending requests to complete.", e); } logger.info("Graceful shutdown complete."); } ShutdownHookProvider[] shutdownHookProviders = SingletonServiceFactory.getBeans(ShutdownHookProvider.class); if (shutdownHookProviders != null) Arrays.stream(shutdownHookProviders).forEach(s -> s.onShutdown()); stop(); logger.info("Cleaning up before server shutdown"); }
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { activeRequestsUpdater.incrementAndGet(this); if (shutdown) { decrementRequests(); exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE); exchange.endExchange(); return; } exchange.addExchangeCompleteListener(listener); next.handleRequest(exchange); }
private void decrementRequests() { if (shutdown) { //we don't read the request count until after checking the shutdown variable //otherwise we could read the request count as zero, a new request could state, and then we shutdown //see https://issues.jboss.org/browse/UNDERTOW-1099 long active = activeRequestsUpdater.decrementAndGet(this); synchronized (lock) { if (active == 0) { shutdownComplete(); } } } else { activeRequestsUpdater.decrementAndGet(this); } }
static public void start() { // add shutdown hook here. addDaemonShutdownHook(); // add startup hooks here. StartupHookProvider[] startupHookProviders = SingletonServiceFactory.getBeans(StartupHookProvider.class); if (startupHookProviders != null) Arrays.stream(startupHookProviders).forEach(s -> s.onStartup()); // For backwards compatibility, check if a handler.yml has been included. If // not, default to original configuration. if (Handler.config == null || !Handler.config.isEnabled()) { HttpHandler handler = middlewareInit(); // register the graceful shutdown handler gracefulShutdownHandler = new GracefulShutdownHandler(handler); } else { // initialize the handlers, chains and paths Handler.init(); // register the graceful shutdown handler gracefulShutdownHandler = new GracefulShutdownHandler(new OrchestrationHandler()); } if (config.dynamicPort) { for (int i = config.minPort; i < config.maxPort; i++) { boolean b = bind(gracefulShutdownHandler, i); if (b) { break; } } } else { bind(gracefulShutdownHandler, -1); } }
@Override public void stop() throws Exception { shutdown.shutdown(); shutdown.awaitShutdown(awaitShutdown); server.stop(); }
public void shutdown() { activeRequestsUpdater.incrementAndGet(this); //the request count is never zero when shutdown is set to true shutdown = true; decrementRequests(); }
@Override public void start() { try { pippoDeploymentManager = createPippoDeploymentManager(); HttpHandler pippoHandler = pippoDeploymentManager.start(); HttpHandler contextHandler = createContextHandler(pippoHandler); GracefulShutdownHandler rootHandler = new GracefulShutdownHandler(contextHandler); server = createServer(rootHandler); String version = server.getClass().getPackage().getImplementationVersion(); log.info("Starting Undertow Server {} on port {}", version, getSettings().getPort()); server.start(); } catch (Exception e) { throw new PippoRuntimeException(e); } }
/** * Perform shutdown. * @param delay is delay to force * @throw InterruptedException if we have an interruption */ public void shutdown(Integer delay) throws InterruptedException { undertowShutdownHandlerWrapper.getGracefulShutdownHandler().shutdown(); undertowShutdownHandlerWrapper.getGracefulShutdownHandler().awaitShutdown(delay * 1000); } }
public void shutdown() { activeRequestsUpdater.incrementAndGet(this); //the request count is never zero when shutdown is set to true shutdown = true; decrementRequests(); }
/** * Returns a new handler that can be used to wait for all requests to finish before shutting down the server gracefully. * * @param next The next http handler * @return The graceful shutdown handler */ public static GracefulShutdownHandler gracefulShutdown(HttpHandler next) { return new GracefulShutdownHandler(next); }
handler.shutdown(); for (GracefulShutdownHandler handler : handlers) { try { handler.awaitShutdown(30 * 1000);
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { activeRequestsUpdater.incrementAndGet(this); if (shutdown) { decrementRequests(); exchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE); exchange.endExchange(); return; } exchange.addExchangeCompleteListener(listener); next.handleRequest(exchange); }
/** * Returns a new handler that can be used to wait for all requests to finish before shutting down the server gracefully. * * @param next The next http handler * @return The graceful shutdown handler */ public static GracefulShutdownHandler gracefulShutdown(HttpHandler next) { return new GracefulShutdownHandler(next); }
shutdownHandler.shutdown(); shutdownHandler.awaitShutdown(60 * 1000); // up to 1 minute } catch (InterruptedException ie) { LOGGER.error("Error while waiting for pending request to complete", ie);