Migrate to ListenerRegistry.create()
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / akka / impl / ActorSystemProviderImpl.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.akka.impl;
9
10 import akka.actor.ActorSystem;
11 import akka.actor.Props;
12 import com.typesafe.config.Config;
13 import java.util.concurrent.TimeUnit;
14 import org.opendaylight.controller.cluster.ActorSystemProvider;
15 import org.opendaylight.controller.cluster.ActorSystemProviderListener;
16 import org.opendaylight.controller.cluster.common.actor.QuarantinedMonitorActor;
17 import org.opendaylight.controller.cluster.datastore.TerminationMonitor;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.util.ListenerRegistry;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import scala.concurrent.Await;
23 import scala.concurrent.duration.FiniteDuration;
24
25 public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseable {
26     private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
27     private static final Logger LOG = LoggerFactory.getLogger(ActorSystemProviderImpl.class);
28
29     private final ActorSystem actorSystem;
30     private final ListenerRegistry<ActorSystemProviderListener> listeners = ListenerRegistry.create();
31
32     public ActorSystemProviderImpl(
33             final ClassLoader classLoader, final Props quarantinedMonitorActorProps, final Config akkaConfig) {
34         LOG.info("Creating new ActorSystem");
35
36         actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME, akkaConfig, classLoader);
37
38         actorSystem.actorOf(Props.create(TerminationMonitor.class), TerminationMonitor.ADDRESS);
39         actorSystem.actorOf(quarantinedMonitorActorProps, QuarantinedMonitorActor.ADDRESS);
40     }
41
42     @Override
43     public ActorSystem getActorSystem() {
44         return actorSystem;
45     }
46
47     @Override
48     public ListenerRegistration<ActorSystemProviderListener> registerActorSystemProviderListener(
49             final ActorSystemProviderListener listener) {
50         return listeners.register(listener);
51     }
52
53     @Override
54     @SuppressWarnings("checkstyle:IllegalCatch")
55     public void close() {
56         LOG.info("Shutting down ActorSystem");
57
58         try {
59             Await.result(actorSystem.terminate(), FiniteDuration.create(10, TimeUnit.SECONDS));
60         } catch (final Exception e) {
61             LOG.warn("Error awaiting actor termination", e);
62         }
63     }
64 }