36b2866210cadb84d9db0ab28c5a8137236254a7
[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
9 package org.opendaylight.controller.cluster.common.actor;
10
11 import akka.event.Logging;
12 import akka.event.LoggingAdapter;
13 import akka.persistence.UntypedPersistentActor;
14
15 public abstract class AbstractUntypedPersistentActor extends UntypedPersistentActor {
16
17     protected final LoggingAdapter LOG =
18         Logging.getLogger(getContext().system(), this);
19
20     public AbstractUntypedPersistentActor() {
21         if(LOG.isDebugEnabled()) {
22             LOG.debug("Actor created {}", getSelf());
23         }
24         getContext().
25             system().
26             actorSelection("user/termination-monitor").
27             tell(new Monitor(getSelf()), getSelf());
28
29     }
30
31
32     @Override public void onReceiveCommand(Object message) throws Exception {
33         final String messageType = message.getClass().getSimpleName();
34         if(LOG.isDebugEnabled()) {
35             LOG.debug("Received message {}", messageType);
36         }
37         handleCommand(message);
38         if(LOG.isDebugEnabled()) {
39             LOG.debug("Done handling message {}", messageType);
40         }
41
42     }
43
44     @Override public void onReceiveRecover(Object message) throws Exception {
45         final String messageType = message.getClass().getSimpleName();
46         if(LOG.isDebugEnabled()) {
47             LOG.debug("Received message {}", messageType);
48         }
49         handleRecover(message);
50         if(LOG.isDebugEnabled()) {
51             LOG.debug("Done handling message {}", messageType);
52         }
53
54     }
55
56     protected abstract void handleRecover(Object message) throws Exception;
57
58     protected abstract void handleCommand(Object message) throws Exception;
59
60     protected void ignoreMessage(Object message) {
61         LOG.debug("Unhandled message {} ", message);
62     }
63
64     protected void unknownMessage(Object message) throws Exception {
65         if(LOG.isDebugEnabled()) {
66             LOG.debug("Received unhandled message {}", message);
67         }
68         unhandled(message);
69     }
70 }