2 * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.common.actor;
11 import akka.actor.ActorRef;
12 import akka.persistence.UntypedPersistentActor;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 public abstract class AbstractUntypedPersistentActor extends UntypedPersistentActor implements ExecuteInSelfActor {
20 // The member name should be lower case but it's referenced in many subclasses. Suppressing the CS warning for now.
21 @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
22 @SuppressWarnings("checkstyle:MemberName")
23 protected final Logger LOG = LoggerFactory.getLogger(getClass());
25 protected AbstractUntypedPersistentActor() {
26 LOG.trace("Actor created {}", getSelf());
27 getContext().system().actorSelection("user/termination-monitor").tell(new Monitor(getSelf()), getSelf());
31 public final void executeInSelf(@NonNull final Runnable runnable) {
32 final ExecuteInSelfMessage message = new ExecuteInSelfMessage(runnable);
33 LOG.trace("Scheduling execution of {}", message);
34 self().tell(message, ActorRef.noSender());
38 public final void onReceiveCommand(final Object message) throws Exception {
39 final String messageType = message.getClass().getSimpleName();
40 LOG.trace("Received message {}", messageType);
42 if (message instanceof ExecuteInSelfMessage) {
43 LOG.trace("Executing {}", message);
44 ((ExecuteInSelfMessage) message).run();
46 handleCommand(message);
49 LOG.trace("Done handling message {}", messageType);
53 public final void onReceiveRecover(final Object message) throws Exception {
54 final String messageType = message.getClass().getSimpleName();
55 LOG.trace("Received message {}", messageType);
56 handleRecover(message);
57 LOG.trace("Done handling message {}", messageType);
60 protected abstract void handleRecover(Object message) throws Exception;
62 protected abstract void handleCommand(Object message) throws Exception;
64 protected void ignoreMessage(final Object message) {
65 LOG.debug("Unhandled message {} ", message);
68 protected void unknownMessage(final Object message) {
69 LOG.debug("Received unhandled message {}", message);