diff options
Diffstat (limited to 'infra/northbound/restconf/src')
2 files changed, 36 insertions, 15 deletions
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<JettyServerStarter.ServerInit> { +class JettyServerStarter extends ProviderTrait<JettyServerStarter.RestconfJettyServer> { private static final Logger LOG = LoggerFactory.getLogger(JettyServerStarter.class); @@ -54,21 +55,41 @@ class JettyServerStarter extends ProviderTrait<JettyServerStarter.ServerInit> { @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<RestconfConfigurati .toProvider(HttpsConnectorProvider.class) .in(Singleton.class); bind(RestConnector.class).toProvider(RestconfProvider.class).in(Singleton.class); - bind(ServerInit.class).toProvider(JettyServerStarter.class).asEagerSingleton(); + bind(RestconfJettyServer.class).toProvider(JettyServerStarter.class).asEagerSingleton(); } } |