|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object akka.actor.UntypedActor
public abstract class UntypedActor
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 class Reply {
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");
} else if (msg.equals("ErrorKernelWithDirectReply")) {
// Send work to one-off child which will reply directly to original sender
getContext().actorOf(new Props(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(new Props(Worker.class)).tell("DoWorkAndReplyToMe");
} else throw new IllegalArgumentException("Unknown message: " + 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);
} else throw new IllegalArgumentException("Unknown message: " + 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 'self' reference. |
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. |
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 |
---|
context, noSender, self, sender, unhandled |
Constructor Detail |
---|
public UntypedActor()
Method Detail |
---|
public abstract void onReceive(java.lang.Object message)
public UntypedActorContext getContext()
public ActorRef getSelf()
public ActorRef getSender()
public SupervisorStrategy supervisorStrategy()
supervisorStrategy
in interface Actor
public void preStart()
preStart
in interface Actor
public void postStop()
postStop
in interface Actor
public void preRestart(java.lang.Throwable reason, scala.Option<java.lang.Object> message)
postStop()
.'''
Is called on a crashed Actor right BEFORE it is restarted to allow clean
up of resources before Actor is terminated.
preRestart
in interface Actor
reason
- the Throwable that caused the restart to happenmessage
- optionally the current message the actor processed when failing, if applicable
Is called on a crashed Actor right BEFORE it is restarted to allow clean
up of resources before Actor is terminated.public void postRestart(java.lang.Throwable reason)
preStart()
.
Is called right AFTER restart on the newly created Actor to allow reinitialization after an Actor crash.
postRestart
in interface Actor
reason
- the Throwable that caused the restart to happen
Is called right AFTER restart on the newly created Actor to allow reinitialization after an Actor crash.public final scala.PartialFunction<java.lang.Object,scala.runtime.BoxedUnit> receive()
Actor
receive
in interface Actor
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |