From 2a39a0f17d82ecf7c75840d53960c42c6ea2c6ac Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Fri, 11 Aug 2017 14:05:30 +0200 Subject: HONEYCOMB-381 - Usage of ShutdownHandler for jvpp components and notification producer Change-Id: I70e749de5b290c84e86a28625d5ec79c98de80f2 Signed-off-by: Jan Srnicek --- vpp-common/vpp-common-integration/pom.xml | 5 ++++ .../common/integration/JVppCoreProvider.java | 17 ++++------- .../common/integration/JVppRegistryProvider.java | 33 +++++++++++----------- .../common/integration/VppCommonModuleTest.java | 6 +++- 4 files changed, 32 insertions(+), 29 deletions(-) (limited to 'vpp-common') diff --git a/vpp-common/vpp-common-integration/pom.xml b/vpp-common/vpp-common-integration/pom.xml index 11b1ae1f1..6bdbe2d6a 100644 --- a/vpp-common/vpp-common-integration/pom.xml +++ b/vpp-common/vpp-common-integration/pom.xml @@ -63,6 +63,11 @@ minimal-distribution-core ${honeycomb.min.distro.version} + + io.fd.honeycomb + cfg-init + ${project.version} + ${project.groupId} naming-context-api diff --git a/vpp-common/vpp-common-integration/src/main/java/io/fd/hc2vpp/common/integration/JVppCoreProvider.java b/vpp-common/vpp-common-integration/src/main/java/io/fd/hc2vpp/common/integration/JVppCoreProvider.java index 4945bb98e..609611252 100644 --- a/vpp-common/vpp-common-integration/src/main/java/io/fd/hc2vpp/common/integration/JVppCoreProvider.java +++ b/vpp-common/vpp-common-integration/src/main/java/io/fd/hc2vpp/common/integration/JVppCoreProvider.java @@ -18,11 +18,12 @@ package io.fd.hc2vpp.common.integration; import com.google.inject.Inject; import io.fd.honeycomb.binding.init.ProviderTrait; -import java.io.IOException; +import io.fd.honeycomb.data.init.ShutdownHandler; import io.fd.vpp.jvpp.JVppRegistry; import io.fd.vpp.jvpp.core.JVppCoreImpl; import io.fd.vpp.jvpp.core.future.FutureJVppCore; import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade; +import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,21 +38,15 @@ public final class JVppCoreProvider extends ProviderTrait { @Inject private JVppRegistry registry; + @Inject + private ShutdownHandler shutdownHandler; + @Override protected FutureJVppCoreFacade create() { try { final JVppCoreImpl jVpp = new JVppCoreImpl(); // Free jvpp-core plugin's resources on shutdown - Runtime.getRuntime().addShutdownHook(new Thread() { - - @Override - public void run() { - LOG.info("Unloading jvpp-core plugin"); - jVpp.close(); - LOG.info("Successfully unloaded jvpp-core plugin"); - } - }); - + shutdownHandler.register("jvpp-core", jVpp); LOG.info("Successfully loaded jvpp-core plugin"); return new FutureJVppCoreFacade(registry, jVpp); } catch (IOException e) { diff --git a/vpp-common/vpp-common-integration/src/main/java/io/fd/hc2vpp/common/integration/JVppRegistryProvider.java b/vpp-common/vpp-common-integration/src/main/java/io/fd/hc2vpp/common/integration/JVppRegistryProvider.java index 8837b7fe8..47c512898 100644 --- a/vpp-common/vpp-common-integration/src/main/java/io/fd/hc2vpp/common/integration/JVppRegistryProvider.java +++ b/vpp-common/vpp-common-integration/src/main/java/io/fd/hc2vpp/common/integration/JVppRegistryProvider.java @@ -17,11 +17,12 @@ package io.fd.hc2vpp.common.integration; import com.google.inject.Inject; -import io.fd.honeycomb.binding.init.ProviderTrait; import io.fd.hc2vpp.common.translate.util.VppStatusListener; -import java.io.IOException; +import io.fd.honeycomb.binding.init.ProviderTrait; +import io.fd.honeycomb.data.init.ShutdownHandler; import io.fd.vpp.jvpp.JVppRegistry; import io.fd.vpp.jvpp.JVppRegistryImpl; +import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +38,8 @@ public final class JVppRegistryProvider extends ProviderTrait { private VppConfigAttributes config; @Inject private VppStatusListener vppStatus; + @Inject + private ShutdownHandler shutdownHandler; @Override protected JVppRegistryImpl create() { @@ -46,23 +49,19 @@ public final class JVppRegistryProvider extends ProviderTrait { // Closing JVpp connection with shutdown hook to erase the connection from VPP so HC will be able // to connect next time. If JVM is force closed, this will not be executed and VPP connection // with name from config will stay open and prevent next startup of HC to success - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - LOG.info("Disconnecting from VPP"); - if (vppStatus.isDown()) { - LOG.info("VPP is down. JVppRegistry cleanup is not needed. Exiting"); - return; - } - try { - registry.close(); - LOG.info("Successfully disconnected from VPP as {}", config.jvppConnectionName); - } catch (Exception e) { - LOG.warn("Unable to properly close jvpp registry", e); - } + shutdownHandler.register("jvpp-registry", () -> { + LOG.info("Disconnecting from VPP"); + if (vppStatus.isDown()) { + LOG.info("VPP is down. JVppRegistry cleanup is not needed. Exiting"); + return; + } + try { + registry.close(); + LOG.info("Successfully disconnected from VPP as {}", config.jvppConnectionName); + } catch (Exception e) { + LOG.warn("Unable to properly close jvpp registry", e); } }); - LOG.info("JVpp connection opened successfully as: {}", config.jvppConnectionName); return registry; } catch (IOException e) { diff --git a/vpp-common/vpp-common-integration/src/test/java/io/fd/hc2vpp/common/integration/VppCommonModuleTest.java b/vpp-common/vpp-common-integration/src/test/java/io/fd/hc2vpp/common/integration/VppCommonModuleTest.java index 540145beb..b1aab05ce 100644 --- a/vpp-common/vpp-common-integration/src/test/java/io/fd/hc2vpp/common/integration/VppCommonModuleTest.java +++ b/vpp-common/vpp-common-integration/src/test/java/io/fd/hc2vpp/common/integration/VppCommonModuleTest.java @@ -29,10 +29,10 @@ import com.google.inject.name.Named; import com.google.inject.testing.fieldbinder.Bind; import com.google.inject.testing.fieldbinder.BoundFieldModule; import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer; +import io.fd.honeycomb.data.init.ShutdownHandler; import io.fd.honeycomb.translate.read.ReaderFactory; import java.util.HashSet; import java.util.Set; -import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.opendaylight.controller.md.sal.binding.api.DataBroker; @@ -44,6 +44,10 @@ public class VppCommonModuleTest { @Mock private DataBroker honeycombContext; + @Mock + @Bind + private ShutdownHandler shutdownHandler; + @Inject private Set readerFactories = new HashSet<>(); -- cgit 1.2.3-korg