akka.actor
Class UntypedActor

java.lang.Object
  extended by akka.actor.UntypedActor
All Implemented Interfaces:
Actor
Direct Known Subclasses:
UntypedActorWithStash, UntypedActorWithUnboundedStash, UntypedActorWithUnrestrictedStash, UntypedConsumerActor, UntypedPersistentActor, UntypedPersistentView, UntypedProcessor, UntypedProducerActor, UntypedTransactor, UntypedView

public abstract class UntypedActor
extends java.lang.Object
implements Actor

Actor base trait that should be extended by or mixed to create an Actor with the semantics of the 'Actor Model': http://en.wikipedia.org/wiki/Actor_model

This class is the Java cousin to the Actor Scala interface. Subclass this abstract class to create a MDB-style untyped actor.

An actor has a well-defined (non-cyclic) life-cycle. - ''RUNNING'' (created and started actor) - can receive messages - ''SHUTDOWN'' (when 'stop' or 'exit' is invoked) - can't do anything

The Actor's own ActorRef is available as getSelf(), the current message’s sender as getSender() and the UntypedActorContext as getContext(). The only abstract method is onReceive() which is invoked for each processed message unless dynamically overridden using getContext().become().

Here is an example on how to create and use an UntypedActor:


  public class SampleUntypedActor extends UntypedActor {

    public static class Reply implements java.io.Serializable {
      final public ActorRef sender;
      final public Result result;
      Reply(ActorRef sender, Result result) {
        this.sender = sender;
        this.result = result;
      }
    }

   private static SupervisorStrategy strategy = new OneForOneStrategy(10, Duration.create("1 minute"),
     new Function<Throwable, Directive>() {
       @Override
       public Directive apply(Throwable t) {
         if (t instanceof ArithmeticException) {
           return resume();
         } else if (t instanceof NullPointerException) {
           return restart();
         } else if (t instanceof IllegalArgumentException) {
           return stop();
         } else {
           return escalate();
         }
       }
     });

   @Override
   public SupervisorStrategy supervisorStrategy() {
     return strategy;
    }

    public void onReceive(Object message) throws Exception {
      if (message instanceof String) {
        String msg = (String) message;

        if (msg.equals("UseSender")) {
          // Reply to original sender of message
          getSender().tell(msg, getSelf());

        } else if (msg.equals("SendToSelf")) {
          // Send message to the actor itself recursively
          getSelf().tell("SomeOtherMessage", getSelf());

        } else if (msg.equals("ErrorKernelWithDirectReply")) {
          // Send work to one-off child which will reply directly to original sender
          getContext().actorOf(Props.create(Worker.class)).tell("DoSomeDangerousWork", getSender());

        } else if (msg.equals("ErrorKernelWithReplyHere")) {
          // Send work to one-off child and collect the answer, reply handled further down
          getContext().actorOf(Props.create(Worker.class)).tell("DoWorkAndReplyToMe", getSelf());

        } else {
          unhandled(message);
        }

      } else if (message instanceof Reply) {

        final Reply reply = (Reply) message;
        // might want to do some processing/book-keeping here
        reply.sender.tell(reply.result, getSelf());

      } else {
        unhandled(message);
      }
    }
  }
 


Nested Class Summary
 
Nested classes/interfaces inherited from interface akka.actor.Actor
Actor.emptyBehavior$
 
Constructor Summary
UntypedActor()
           
 
Method Summary
 UntypedActorContext getContext()
          Returns this UntypedActor's UntypedActorContext The UntypedActorContext is not thread safe so do not expose it outside of the UntypedActor.
 ActorRef getSelf()
          Returns the ActorRef for this actor.
 ActorRef getSender()
          The reference sender Actor of the currently processed message.
abstract  void onReceive(java.lang.Object message)
          To be implemented by concrete UntypedActor, this defines the behavior of the UntypedActor.
 void postRestart(java.lang.Throwable reason)
          User overridable callback: By default it calls preStart().
 void postStop()
          User overridable callback.
 void preRestart(java.lang.Throwable reason, scala.Option<java.lang.Object> message)
          User overridable callback: '''By default it disposes of all children and then calls postStop().'''
 void preStart()
          User overridable callback.
 scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> receive()
          This defines the initial actor behavior, it must return a partial function with the actor logic.
 SupervisorStrategy supervisorStrategy()
          User overridable definition the strategy to use for supervising child actors.
 void unhandled(java.lang.Object message)
          Recommended convention is to call this method if the message isn't handled in onReceive(java.lang.Object) (e.g.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface akka.actor.Actor
aroundPostRestart, aroundPostStop, aroundPreRestart, aroundPreStart, aroundReceive, context, self, sender
 

Constructor Detail

UntypedActor

public UntypedActor()
Method Detail

onReceive

public abstract void onReceive(java.lang.Object message)
To be implemented by concrete UntypedActor, this defines the behavior of the UntypedActor.

Parameters:
message - (undocumented)

getContext

public UntypedActorContext getContext()
Returns this UntypedActor's UntypedActorContext The UntypedActorContext is not thread safe so do not expose it outside of the UntypedActor.

Returns:
(undocumented)

getSelf

public ActorRef getSelf()
Returns the ActorRef for this actor.

Returns:
(undocumented)

getSender

public ActorRef getSender()
The reference sender Actor of the currently processed message. This is always a legal destination to send to, even if there is no logical recipient for the reply, in which case it will be sent to the dead letter mailbox.

Returns:
(undocumented)

supervisorStrategy

public SupervisorStrategy supervisorStrategy()
User overridable definition the strategy to use for supervising child actors.

Specified by:
supervisorStrategy in interface Actor
Returns:
(undocumented)

preStart

public void preStart()
User overridable callback.

Is called when an Actor is started. Actor are automatically started asynchronously when created. Empty default implementation.

Specified by:
preStart in interface Actor

postStop

public void postStop()
User overridable callback.

Is called asynchronously after 'actor.stop()' is invoked. Empty default implementation.

Specified by:
postStop in interface Actor

preRestart

public void preRestart(java.lang.Throwable reason,
                       scala.Option<java.lang.Object> message)
User overridable callback: '''By default it disposes of all children and then calls postStop().'''

Is called on a crashed Actor right BEFORE it is restarted to allow clean up of resources before Actor is terminated.

Specified by:
preRestart in interface Actor
Parameters:
reason - (undocumented)
message - (undocumented)

postRestart

public void postRestart(java.lang.Throwable reason)
User overridable callback: By default it calls preStart().

Is called right AFTER restart on the newly created Actor to allow reinitialization after an Actor crash.

Specified by:
postRestart in interface Actor
Parameters:
reason - (undocumented)

receive

public final scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> receive()
Description copied from interface: Actor
This defines the initial actor behavior, it must return a partial function with the actor logic.

Specified by:
receive in interface Actor
Returns:
(undocumented)

unhandled

public void unhandled(java.lang.Object message)
Recommended convention is to call this method if the message isn't handled in onReceive(java.lang.Object) (e.g. unknown message type). By default it fails with either a DeathPactException (in case of an unhandled Terminated message) or publishes an UnhandledMessage to the actor's system's EventStream.

Specified by:
unhandled in interface Actor
Parameters:
message - (undocumented)