X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fcommon%2Factor%2FAbstractUntypedActor.java;h=6bd7a053b0bcc9480cd7ff42d3f67379a06ef805;hp=21a0cb6a889a78cf31910198eadc79f6c53f10e3;hb=12fcdfe39aa26dcba7fd3bb4d4c68e3d02e65c51;hpb=fc54ab8853d36fb1d7aebf2a09ef10567e66aa0d diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/common/actor/AbstractUntypedActor.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/common/actor/AbstractUntypedActor.java index 21a0cb6a88..6bd7a053b0 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/common/actor/AbstractUntypedActor.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/common/actor/AbstractUntypedActor.java @@ -8,46 +8,57 @@ package org.opendaylight.controller.cluster.common.actor; +import akka.actor.ActorRef; import akka.actor.UntypedActor; -import akka.event.Logging; -import akka.event.LoggingAdapter; +import org.eclipse.jdt.annotation.NonNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -public abstract class AbstractUntypedActor extends UntypedActor { - protected final LoggingAdapter LOG = - Logging.getLogger(getContext().system(), this); +public abstract class AbstractUntypedActor extends UntypedActor implements ExecuteInSelfActor { + // The member name should be lower case but it's referenced in many subclasses. Suppressing the CS warning for now. + @SuppressWarnings("checkstyle:MemberName") + protected final Logger LOG = LoggerFactory.getLogger(getClass()); - public AbstractUntypedActor() { - if(LOG.isDebugEnabled()) { - LOG.debug("Actor created {}", getSelf()); - } - getContext(). - system(). - actorSelection("user/termination-monitor"). - tell(new Monitor(getSelf()), getSelf()); + protected AbstractUntypedActor() { + LOG.debug("Actor created {}", getSelf()); + getContext().system().actorSelection("user/termination-monitor").tell(new Monitor(getSelf()), getSelf()); + } + @Override + public final void executeInSelf(@NonNull final Runnable runnable) { + final ExecuteInSelfMessage message = new ExecuteInSelfMessage(runnable); + self().tell(message, ActorRef.noSender()); } - @Override public void onReceive(Object message) throws Exception { - final String messageType = message.getClass().getSimpleName(); - if(LOG.isDebugEnabled()) { -// LOG.debug("Received message {}", messageType); - } - handleReceive(message); - if(LOG.isDebugEnabled()) { -// LOG.debug("Done handling message {}", messageType); + @Override + public final void onReceive(final Object message) { + if (message instanceof ExecuteInSelfMessage) { + ((ExecuteInSelfMessage) message).run(); + } else { + handleReceive(message); } } - protected abstract void handleReceive(Object message) throws Exception; + /** + * Receive and handle an incoming message. If the implementation does not handle this particular message, + * it should call {@link #ignoreMessage(Object)} or {@link #unknownMessage(Object)}. + * + * @param message the incoming message + */ + protected abstract void handleReceive(Object message); - protected void ignoreMessage(Object message) { - LOG.debug("Unhandled message {} ", message); + protected final void ignoreMessage(final Object message) { + LOG.debug("Ignoring unhandled message {}", message); } - protected void unknownMessage(Object message) throws Exception { - if(LOG.isDebugEnabled()) { - LOG.debug("Received unhandled message {}", message); - } + protected final void unknownMessage(final Object message) { + LOG.debug("Received unhandled message {}", message); unhandled(message); } + + protected boolean isValidSender(final ActorRef sender) { + // If the caller passes in a null sender (ActorRef.noSender()), akka translates that to the + // deadLetters actor. + return sender != null && !getContext().system().deadLetters().equals(sender); + } }