Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / AbstractUntypedPersistentActor.java
1 /*
2  * Copyright (c) 2014 Cisco 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.common.actor;
9
10 import akka.actor.ActorRef;
11 import akka.persistence.AbstractPersistentActor;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 // FIXME: override getContext(), getSelf() and others to be final to get rid of
18 //        SpotBugs MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR violation
19 public abstract class AbstractUntypedPersistentActor extends AbstractPersistentActor implements ExecuteInSelfActor {
20
21     // The member name should be lower case but it's referenced in many subclasses. Suppressing the CS warning for now.
22     @SuppressWarnings("checkstyle:MemberName")
23     @SuppressFBWarnings("SLF4J_LOGGER_SHOULD_BE_PRIVATE")
24     protected final Logger LOG = LoggerFactory.getLogger(getClass());
25
26     @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "Akka class design")
27     protected AbstractUntypedPersistentActor() {
28         LOG.trace("Actor created {}", getSelf());
29         getContext().system().actorSelection("user/termination-monitor").tell(new Monitor(getSelf()), getSelf());
30     }
31
32     @Override
33     public final void executeInSelf(@NonNull final Runnable runnable) {
34         final ExecuteInSelfMessage message = new ExecuteInSelfMessage(runnable);
35         LOG.trace("Scheduling execution of {}", message);
36         self().tell(message, ActorRef.noSender());
37     }
38
39     @Override
40     public final Receive createReceive() {
41         return receiveBuilder()
42                 .match(ExecuteInSelfMessage.class, ExecuteInSelfMessage::run)
43                 .matchAny(this::handleCommand)
44                 .build();
45     }
46
47     @Override
48     public final Receive createReceiveRecover() {
49         return receiveBuilder().matchAny(this::handleRecover).build();
50     }
51
52     protected abstract void handleRecover(Object message) throws Exception;
53
54     protected abstract void handleCommand(Object message) throws Exception;
55
56     protected void ignoreMessage(final Object message) {
57         LOG.debug("Unhandled message {} ", message);
58     }
59
60     protected void unknownMessage(final Object message) {
61         LOG.debug("Received unhandled message {}", message);
62         unhandled(message);
63     }
64 }