2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.akka.impl;
10 import akka.actor.ActorSystem;
11 import akka.actor.Props;
12 import akka.actor.Terminated;
13 import akka.dispatch.OnComplete;
14 import com.typesafe.config.Config;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.controller.cluster.ActorSystemProvider;
19 import org.opendaylight.controller.cluster.ActorSystemProviderListener;
20 import org.opendaylight.controller.cluster.common.actor.QuarantinedMonitorActor;
21 import org.opendaylight.controller.cluster.datastore.TerminationMonitor;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.util.ListenerRegistry;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import scala.concurrent.Await;
27 import scala.concurrent.ExecutionContext;
28 import scala.concurrent.Future;
29 import scala.concurrent.duration.FiniteDuration;
31 public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseable {
32 private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
33 private static final Logger LOG = LoggerFactory.getLogger(ActorSystemProviderImpl.class);
35 private final @NonNull ActorSystem actorSystem;
36 private final ListenerRegistry<ActorSystemProviderListener> listeners = ListenerRegistry.create();
38 public ActorSystemProviderImpl(
39 final ClassLoader classLoader, final Props quarantinedMonitorActorProps, final Config akkaConfig) {
40 LOG.info("Creating new ActorSystem");
42 actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME, akkaConfig, classLoader);
43 actorSystem.actorOf(Props.create(TerminationMonitor.class), TerminationMonitor.ADDRESS);
44 actorSystem.actorOf(quarantinedMonitorActorProps, QuarantinedMonitorActor.ADDRESS);
48 public ActorSystem getActorSystem() {
53 public ListenerRegistration<ActorSystemProviderListener> registerActorSystemProviderListener(
54 final ActorSystemProviderListener listener) {
55 return listeners.register(listener);
58 public Future<Terminated> asyncClose() {
59 LOG.info("Shutting down ActorSystem");
61 final Future<Terminated> ret = actorSystem.terminate();
62 ret.onComplete(new OnComplete<Terminated>() {
64 public void onComplete(final Throwable failure, final Terminated success) throws Throwable {
65 if (failure != null) {
66 LOG.warn("ActorSystem failed to shut down", failure);
68 LOG.info("ActorSystem shut down");
71 }, ExecutionContext.global());
75 public void close(final FiniteDuration wait) throws TimeoutException, InterruptedException {
76 Await.result(asyncClose(), wait);
80 public void close() throws TimeoutException, InterruptedException {
81 close(FiniteDuration.create(10, TimeUnit.SECONDS));