Eliminate dead letters message when there's no sender
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / AbstractUntypedActor.java
index 50726bff2e618e310fbfd035fa9193a6844ae479..ca303a5c12ee54749b1ddcbaf24e1cb4bf8f522b 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.controller.cluster.common.actor;
 
+import akka.actor.ActorRef;
 import akka.actor.UntypedActor;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -21,18 +22,31 @@ public abstract class AbstractUntypedActor extends UntypedActor {
     }
 
     @Override
-    public void onReceive(Object message) throws Exception {
+    public final void onReceive(Object message) throws Exception {
         handleReceive(message);
     }
 
+    /**
+     * 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 Incoming message
+     * @throws Exception
+     */
     protected abstract void handleReceive(Object message) throws Exception;
 
-    protected void ignoreMessage(Object message) {
-        LOG.debug("Unhandled message {}", message);
+    protected final void ignoreMessage(Object message) {
+        LOG.debug("Ignoring unhandled message {}", message);
     }
 
-    protected void unknownMessage(Object message) throws Exception {
+    protected final void unknownMessage(Object message) {
         LOG.debug("Received unhandled message {}", message);
         unhandled(message);
     }
+
+    protected boolean isValidSender(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);
+    }
 }