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 org.eclipse.jdt.annotation.NonNull;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 public abstract class AbstractUntypedPersistentActor extends UntypedPersistentActor implements ExecuteInSelfActor {
19 // The member name should be lower case but it's referenced in many subclasses. Suppressing the CS warning for now.
20 @SuppressWarnings("checkstyle:MemberName")
21 protected final Logger LOG = LoggerFactory.getLogger(getClass());
23 protected AbstractUntypedPersistentActor() {
24 LOG.trace("Actor created {}", getSelf());
25 getContext().system().actorSelection("user/termination-monitor").tell(new Monitor(getSelf()), getSelf());
29 public final void executeInSelf(@NonNull final Runnable runnable) {
30 final ExecuteInSelfMessage message = new ExecuteInSelfMessage(runnable);
31 LOG.trace("Scheduling execution of {}", message);
32 self().tell(message, ActorRef.noSender());
36 public final void onReceiveCommand(final Object message) throws Exception {
37 final String messageType = message.getClass().getSimpleName();
38 LOG.trace("Received message {}", messageType);
40 if (message instanceof ExecuteInSelfMessage) {
41 LOG.trace("Executing {}", message);
42 ((ExecuteInSelfMessage) message).run();
44 handleCommand(message);
47 LOG.trace("Done handling message {}", messageType);
51 public final void onReceiveRecover(final Object message) throws Exception {
52 final String messageType = message.getClass().getSimpleName();
53 LOG.trace("Received message {}", messageType);
54 handleRecover(message);
55 LOG.trace("Done handling message {}", messageType);
58 protected abstract void handleRecover(Object message) throws Exception;
60 protected abstract void handleCommand(Object message) throws Exception;
62 protected void ignoreMessage(final Object message) {
63 LOG.debug("Unhandled message {} ", message);
66 protected void unknownMessage(final Object message) {
67 LOG.debug("Received unhandled message {}", message);