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.event.Logging;
12 import akka.event.LoggingAdapter;
13 import akka.japi.Procedure;
14 import akka.persistence.SnapshotSelectionCriteria;
15 import akka.persistence.UntypedPersistentActor;
16 import org.opendaylight.controller.cluster.DataPersistenceProvider;
18 public abstract class AbstractUntypedPersistentActor extends UntypedPersistentActor {
20 protected final LoggingAdapter LOG =
21 Logging.getLogger(getContext().system(), this);
23 public AbstractUntypedPersistentActor() {
24 if(LOG.isDebugEnabled()) {
25 LOG.debug("Actor created {}", getSelf());
29 actorSelection("user/termination-monitor").
30 tell(new Monitor(getSelf()), getSelf());
35 @Override public void onReceiveCommand(Object message) throws Exception {
36 final String messageType = message.getClass().getSimpleName();
37 if(LOG.isDebugEnabled()) {
38 LOG.debug("Received message {}", messageType);
40 handleCommand(message);
41 if(LOG.isDebugEnabled()) {
42 LOG.debug("Done handling message {}", messageType);
47 @Override public void onReceiveRecover(Object message) throws Exception {
48 final String messageType = message.getClass().getSimpleName();
49 if(LOG.isDebugEnabled()) {
50 LOG.debug("Received message {}", messageType);
52 handleRecover(message);
53 if(LOG.isDebugEnabled()) {
54 LOG.debug("Done handling message {}", messageType);
59 protected abstract void handleRecover(Object message) throws Exception;
61 protected abstract void handleCommand(Object message) throws Exception;
63 protected void ignoreMessage(Object message) {
64 LOG.debug("Unhandled message {} ", message);
67 protected void unknownMessage(Object message) throws Exception {
68 if(LOG.isDebugEnabled()) {
69 LOG.debug("Received unhandled message {}", message);
74 protected class PersistentDataProvider implements DataPersistenceProvider {
76 public PersistentDataProvider(){
81 public boolean isRecoveryApplicable() {
86 public <T> void persist(T o, Procedure<T> procedure) {
87 AbstractUntypedPersistentActor.this.persist(o, procedure);
91 public void saveSnapshot(Object o) {
92 AbstractUntypedPersistentActor.this.saveSnapshot(o);
96 public void deleteSnapshots(SnapshotSelectionCriteria criteria) {
97 AbstractUntypedPersistentActor.this.deleteSnapshots(criteria);
101 public void deleteMessages(long sequenceNumber) {
102 AbstractUntypedPersistentActor.this.deleteMessages(sequenceNumber);
106 protected class NonPersistentDataProvider implements DataPersistenceProvider {
108 public NonPersistentDataProvider(){
113 public boolean isRecoveryApplicable() {
118 public <T> void persist(T o, Procedure<T> procedure) {
121 } catch (Exception e) {
122 LOG.error(e, "An unexpected error occurred");
127 public void saveSnapshot(Object o) {
131 public void deleteSnapshots(SnapshotSelectionCriteria criteria) {
136 public void deleteMessages(long sequenceNumber) {