2 * Copyright (c) 2017 Inocybe Technologies 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.datastore.actors;
10 import akka.actor.ActorRef;
11 import akka.actor.Cancellable;
12 import akka.actor.PoisonPill;
13 import akka.actor.Props;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.base.Preconditions;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
18 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
19 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistrationReply;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import scala.concurrent.duration.Duration;
24 * Actor co-located with a shard. It exists only to terminate the registration when
25 * asked to do so via {@link CloseDataTreeNotificationListenerRegistration}.
27 public final class DataTreeNotificationListenerRegistrationActor extends AbstractUntypedActor {
29 static long killDelay = TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS);
31 private ListenerRegistration<?> registration;
32 private Runnable onClose;
33 private boolean closed;
34 private Cancellable killSchedule;
37 protected void handleReceive(Object message) throws Exception {
38 if (message instanceof CloseDataTreeNotificationListenerRegistration) {
39 closeListenerRegistration();
40 if (isValidSender(getSender())) {
41 getSender().tell(CloseDataTreeNotificationListenerRegistrationReply.getInstance(), getSelf());
43 } else if (message instanceof SetRegistration) {
44 registration = ((SetRegistration)message).registration;
45 onClose = ((SetRegistration)message).onClose;
47 closeListenerRegistration();
50 unknownMessage(message);
54 private void closeListenerRegistration() {
56 if (registration != null) {
61 if (killSchedule == null) {
62 killSchedule = getContext().system().scheduler().scheduleOnce(Duration.create(killDelay,
63 TimeUnit.MILLISECONDS), getSelf(), PoisonPill.getInstance(), getContext().dispatcher(),
69 public static Props props() {
70 return Props.create(DataTreeNotificationListenerRegistrationActor.class);
73 public static class SetRegistration {
74 private final ListenerRegistration<?> registration;
75 private final Runnable onClose;
77 public SetRegistration(final ListenerRegistration<?> registration, final Runnable onClose) {
78 this.registration = Preconditions.checkNotNull(registration);
79 this.onClose = Preconditions.checkNotNull(onClose);