From a8b9c1ad9df23ff33beea1ba80caadcca19391c5 Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Wed, 7 Mar 2018 15:03:35 +0100 Subject: Fix Restconf server shutdown Change-Id: Ia7c0fc2d269ae88170297ef22476c8a61121238a Signed-off-by: Marek Gradzki --- .../northbound/restconf/JettyServerStarter.java | 47 ++++++++++++++++------ .../northbound/restconf/RestconfModule.java | 4 +- 2 files changed, 36 insertions(+), 15 deletions(-) (limited to 'infra/northbound/restconf/src') diff --git a/infra/northbound/restconf/src/main/java/io/fd/honeycomb/northbound/restconf/JettyServerStarter.java b/infra/northbound/restconf/src/main/java/io/fd/honeycomb/northbound/restconf/JettyServerStarter.java index 1bd50bb22..7f5673caf 100644 --- a/infra/northbound/restconf/src/main/java/io/fd/honeycomb/northbound/restconf/JettyServerStarter.java +++ b/infra/northbound/restconf/src/main/java/io/fd/honeycomb/northbound/restconf/JettyServerStarter.java @@ -23,6 +23,7 @@ import com.google.inject.Inject; import com.google.inject.name.Named; import io.fd.honeycomb.binding.init.ProviderTrait; +import io.fd.honeycomb.data.init.ShutdownHandler; import javax.annotation.Nullable; import org.eclipse.jetty.server.Server; @@ -31,7 +32,7 @@ import org.opendaylight.netconf.sal.rest.api.RestConnector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -class JettyServerStarter extends ProviderTrait { +class JettyServerStarter extends ProviderTrait { private static final Logger LOG = LoggerFactory.getLogger(JettyServerStarter.class); @@ -54,21 +55,41 @@ class JettyServerStarter extends ProviderTrait { @Named(RESTCONF_HTTPS) private ServerConnector httpsConnectorInit; + @Inject + private ShutdownHandler shutdownHandler; + @Override - protected ServerInit create() { - try { - LOG.info("Starting RESTCONF Jetty server"); - server.start(); - LOG.info("RESTCONF Jetty server successfully started"); - } catch (Exception e) { - LOG.error("Unable to start RESTCONF Jetty server", e); - throw new IllegalStateException("Unable to start RESTCONF Jetty server", e); + protected RestconfJettyServer create() { + final RestconfJettyServer jettyServer = new RestconfJettyServer(server); + jettyServer.start(); + shutdownHandler.register(RestconfJettyServer.class.getCanonicalName(), jettyServer); + return jettyServer; + } + + final class RestconfJettyServer implements AutoCloseable { + private final Server server; + + private RestconfJettyServer(final Server server) { + this.server = server; } - return new ServerInit() { - }; - } + private void start() { + try { + LOG.info("Starting RESTCONF Jetty server"); + server.start(); + LOG.info("RESTCONF Jetty server successfully started"); + } catch (Exception e) { + LOG.error("Unable to start RESTCONF Jetty server", e); + throw new IllegalStateException("Unable to start RESTCONF Jetty server", e); + } + } - interface ServerInit { + @Override + public void close() throws Exception { + LOG.info("Stopping RESTCONF Jetty server"); + server.stop(); + server.destroy(); + LOG.info("RESTCONF Jetty server successfully stopped"); + } } } diff --git a/infra/northbound/restconf/src/main/java/io/fd/honeycomb/northbound/restconf/RestconfModule.java b/infra/northbound/restconf/src/main/java/io/fd/honeycomb/northbound/restconf/RestconfModule.java index 9f9ecf7a2..1a59b7298 100644 --- a/infra/northbound/restconf/src/main/java/io/fd/honeycomb/northbound/restconf/RestconfModule.java +++ b/infra/northbound/restconf/src/main/java/io/fd/honeycomb/northbound/restconf/RestconfModule.java @@ -19,7 +19,7 @@ package io.fd.honeycomb.northbound.restconf; import com.google.inject.Singleton; import com.google.inject.name.Names; import io.fd.honeycomb.northbound.NorthboundAbstractModule; -import io.fd.honeycomb.northbound.restconf.JettyServerStarter.ServerInit; +import io.fd.honeycomb.northbound.restconf.JettyServerStarter.RestconfJettyServer; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.opendaylight.netconf.sal.rest.api.RestConnector; @@ -54,6 +54,6 @@ public class RestconfModule extends NorthboundAbstractModule