Use ActorSystem.terminate()
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / config / yang / config / actor_system_provider / impl / ActorSystemProviderImpl.java
index 22d728dd75df7130b6c7f63ea4791004e2e21cb2..1ab67384f17112d0ac527dc00099ed851676be54 100644 (file)
@@ -8,42 +8,60 @@
 package org.opendaylight.controller.config.yang.config.actor_system_provider.impl;
 
 import akka.actor.ActorSystem;
+import akka.actor.Props;
+import akka.japi.Effect;
 import akka.osgi.BundleDelegatingClassLoader;
+import com.typesafe.config.Config;
 import com.typesafe.config.ConfigFactory;
 import java.util.concurrent.TimeUnit;
 import org.opendaylight.controller.cluster.ActorSystemProvider;
 import org.opendaylight.controller.cluster.ActorSystemProviderListener;
 import org.opendaylight.controller.cluster.common.actor.AkkaConfigurationReader;
 import org.opendaylight.controller.cluster.common.actor.FileAkkaConfigurationReader;
+import org.opendaylight.controller.cluster.common.actor.QuarantinedMonitorActor;
+import org.opendaylight.controller.cluster.datastore.TerminationMonitor;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.util.ListenerRegistry;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import scala.concurrent.Await;
 import scala.concurrent.duration.Duration;
 
 public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseable {
     private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
     private static final String CONFIGURATION_NAME = "odl-cluster-data";
     static final Logger LOG = LoggerFactory.getLogger(ActorSystemProviderImpl.class);
-
-    private ActorSystem actorSystem;
-    private final BundleDelegatingClassLoader classLoader;
+    private final ActorSystem actorSystem;
     private final ListenerRegistry<ActorSystemProviderListener> listeners = new ListenerRegistry<>();
 
-    public ActorSystemProviderImpl(BundleContext bundleContext) {
+    public ActorSystemProviderImpl(final BundleContext bundleContext) {
         LOG.info("Creating new ActorSystem");
 
-        classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
-                Thread.currentThread().getContextClassLoader());
+        final Bundle bundle = bundleContext.getBundle();
 
-        createActorSystem();
-    }
+        final BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundle, Thread.currentThread()
+                .getContextClassLoader());
+
+        final AkkaConfigurationReader configurationReader = new FileAkkaConfigurationReader();
+        final Config akkaConfig = ConfigFactory.load(configurationReader.read()).getConfig(CONFIGURATION_NAME);
+
+        actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME, akkaConfig, classLoader);
+
+        actorSystem.actorOf(Props.create(TerminationMonitor.class), TerminationMonitor.ADDRESS);
+
+        actorSystem.actorOf(QuarantinedMonitorActor.props(new Effect() {
+
+            @Override
+            public void apply() throws Exception {
+                // restart the entire karaf container
+                LOG.warn("Restarting karaf container");
+                System.setProperty("karaf.restart", "true");
+                bundleContext.getBundle(0).stop();
+            }
+        }), QuarantinedMonitorActor.ADDRESS);
 
-    private void createActorSystem() {
-        AkkaConfigurationReader configurationReader = new FileAkkaConfigurationReader();
-        actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME,
-                ConfigFactory.load(configurationReader.read()).getConfig(CONFIGURATION_NAME), classLoader);
     }
 
     @Override
@@ -53,7 +71,7 @@ public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseab
 
     @Override
     public ListenerRegistration<ActorSystemProviderListener> registerActorSystemProviderListener(
-            ActorSystemProviderListener listener) {
+            final ActorSystemProviderListener listener) {
         return listeners.register(listener);
     }
 
@@ -61,11 +79,10 @@ public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseab
     public void close() {
         LOG.info("Shutting down ActorSystem");
 
-        actorSystem.shutdown();
         try {
-            actorSystem.awaitTermination(Duration.create(10, TimeUnit.SECONDS));
+            Await.result(actorSystem.terminate(), Duration.create(10, TimeUnit.SECONDS));
         } catch (Exception e) {
             LOG.warn("Error awaiting actor termination", e);
         }
     }
-}
\ No newline at end of file
+}