6e805b33d7893045b431f3314cd18525a3bce5e4
[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 java.util.concurrent.TimeoutException;
15 import org.opendaylight.controller.cluster.ActorSystemProvider;
16 import org.opendaylight.controller.cluster.ActorSystemProviderListener;
17 import org.opendaylight.controller.cluster.common.actor.QuarantinedMonitorActor;
18 import org.opendaylight.controller.cluster.datastore.TerminationMonitor;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.util.ListenerRegistry;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import scala.concurrent.Await;
24 import scala.concurrent.duration.FiniteDuration;
25
26 public class ActorSystemProviderImpl implements ActorSystemProvider, AutoCloseable {
27     private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
28     private static final Logger LOG = LoggerFactory.getLogger(ActorSystemProviderImpl.class);
29
30     private final ActorSystem actorSystem;
31     private final ListenerRegistry<ActorSystemProviderListener> listeners = ListenerRegistry.create();
32
33     public ActorSystemProviderImpl(
34             final ClassLoader classLoader, final Props quarantinedMonitorActorProps, final Config akkaConfig) {
35         LOG.info("Creating new ActorSystem");
36
37         actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME, akkaConfig, classLoader);
38
39         actorSystem.actorOf(Props.create(TerminationMonitor.class), TerminationMonitor.ADDRESS);
40         actorSystem.actorOf(quarantinedMonitorActorProps, QuarantinedMonitorActor.ADDRESS);
41     }
42
43     @Override
44     public ActorSystem getActorSystem() {
45         return actorSystem;
46     }
47
48     @Override
49     public ListenerRegistration<ActorSystemProviderListener> registerActorSystemProviderListener(
50             final ActorSystemProviderListener listener) {
51         return listeners.register(listener);
52     }
53
54     @Override
55     public void close() throws TimeoutException, InterruptedException {
56         LOG.info("Shutting down ActorSystem");
57         Await.result(actorSystem.terminate(), FiniteDuration.create(10, TimeUnit.SECONDS));
58     }
59 }