@Override public void stopComponent() { // Stop server to free up any resources. LambdaUtils.tryTo(server::stop, ex -> logger.error(ex, "Failed to cleanly shutdown REST API.")); }
@Override public void close() throws Exception { // Submit all events whenever this context is closed. LambdaUtils.forEachTry(queuedTriggerEvents, triggerEventConsumer::submit, ex -> LOGGER.error(ex, "Could not submit TriggerEvent.")); queuedTriggerEvents.clear(); currentContext.remove(); }
@Override public void stopComponent() { // Release any connection to ElasticSearch. if (lowLevelClient != null) { LambdaUtils.tryTo(() -> lowLevelClient.close(), ex -> LOGGER.warning(ex, "Error while closing connections to ElasticSearch.")); } }
@Override public void startComponent() { // Initialize servlet using RESTEasy and it's Guice bridge. // The listener must be injected by the same Guice module which also binds the REST endpoints. ServletContextHandler servletHandler = new ServletContextHandler(); servletHandler.addEventListener(listener); servletHandler.addServlet(HttpServletDispatcher.class, "/*"); // Configure Jetty: Remove 'server' header from response and set listen port. HttpConfiguration httpConfig = new HttpConfiguration(); httpConfig.setSendServerVersion(false); ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(httpConfig)); connector.setPort(port); // Starting up Jetty to serve the REST API. server.addConnector(connector); server.setHandler(servletHandler); if (!LambdaUtils.tryTo(server::start, ex -> logger.error(ex, "Failed to start REST API."))) { throw new IllegalStateException("Failed to start REST API."); } }