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.japi.Procedure;
12 import akka.persistence.SnapshotSelectionCriteria;
13 import akka.persistence.UntypedPersistentActor;
14 import org.opendaylight.controller.cluster.DataPersistenceProvider;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 public abstract class AbstractUntypedPersistentActor extends UntypedPersistentActor {
20 protected final Logger LOG = LoggerFactory.getLogger(getClass());
22 public AbstractUntypedPersistentActor() {
23 if(LOG.isTraceEnabled()) {
24 LOG.trace("Actor created {}", getSelf());
28 actorSelection("user/termination-monitor").
29 tell(new Monitor(getSelf()), getSelf());
34 @Override public void onReceiveCommand(Object message) throws Exception {
35 final String messageType = message.getClass().getSimpleName();
36 if(LOG.isTraceEnabled()) {
37 LOG.trace("Received message {}", messageType);
39 handleCommand(message);
40 if(LOG.isTraceEnabled()) {
41 LOG.trace("Done handling message {}", messageType);
46 @Override public void onReceiveRecover(Object message) throws Exception {
47 final String messageType = message.getClass().getSimpleName();
48 if(LOG.isTraceEnabled()) {
49 LOG.trace("Received message {}", messageType);
51 handleRecover(message);
52 if(LOG.isTraceEnabled()) {
53 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(Object message) {
63 LOG.debug("Unhandled message {} ", message);
66 protected void unknownMessage(Object message) throws Exception {
67 if(LOG.isDebugEnabled()) {
68 LOG.debug("Received unhandled message {}", message);
73 protected class PersistentDataProvider implements DataPersistenceProvider {
75 public PersistentDataProvider(){
80 public boolean isRecoveryApplicable() {
85 public <T> void persist(T o, Procedure<T> procedure) {
86 AbstractUntypedPersistentActor.this.persist(o, procedure);
90 public void saveSnapshot(Object o) {
91 AbstractUntypedPersistentActor.this.saveSnapshot(o);
95 public void deleteSnapshots(SnapshotSelectionCriteria criteria) {
96 AbstractUntypedPersistentActor.this.deleteSnapshots(criteria);
100 public void deleteMessages(long sequenceNumber) {
101 AbstractUntypedPersistentActor.this.deleteMessages(sequenceNumber);
105 protected class NonPersistentDataProvider implements DataPersistenceProvider {
107 public NonPersistentDataProvider(){
112 public boolean isRecoveryApplicable() {
117 public <T> void persist(T o, Procedure<T> procedure) {
120 } catch (Exception e) {
121 LOG.error("An unexpected error occurred", e);
126 public void saveSnapshot(Object o) {
130 public void deleteSnapshots(SnapshotSelectionCriteria criteria) {
135 public void deleteMessages(long sequenceNumber) {