Class BehaviorActor
- java.lang.Object
-
- co.paralleluniverse.actors.ActorImpl<Message>
-
- co.paralleluniverse.actors.Actor<java.lang.Object,java.lang.Void>
-
- co.paralleluniverse.actors.behaviors.BehaviorActor
-
- All Implemented Interfaces:
ActorBuilder<java.lang.Object,java.lang.Void>,Joinable<java.lang.Void>,Port<java.lang.Object>,PortAutoCloseable,ReceivePort<java.lang.Object>,Stranded,SuspendableCallable<java.lang.Void>,java.io.Serializable,java.lang.AutoCloseable
- Direct Known Subclasses:
EventSourceActor,FiniteStateMachineActor,ServerActor,SupervisorActor
public abstract class BehaviorActor extends Actor<java.lang.Object,java.lang.Void> implements java.io.Serializable
A general behavior-actor class, extended by all behaviors. Behaviors are actor templates encapsulating common useful actor patterns. This provides standard, sane, actor lifecycle methods, as well as other useful services (like a logger object).- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class co.paralleluniverse.actors.ActorImpl
ActorImpl.ActorLifecycleListener
-
Nested classes/interfaces inherited from interface co.paralleluniverse.strands.channels.ReceivePort
ReceivePort.EOFException
-
-
Field Summary
-
Fields inherited from class co.paralleluniverse.actors.ActorImpl
flightRecorder, ref
-
-
Constructor Summary
Constructors Modifier Constructor Description BehaviorActor()Creates a new behavior actor.BehaviorActor(Initializer initializer)Creates a new behavior actor.BehaviorActor(Initializer initializer, MailboxConfig mailboxConfig)Creates a new behavior actor.BehaviorActor(MailboxConfig mailboxConfig)Creates a new behavior actor.BehaviorActor(java.lang.String name)Creates a new behavior actor.BehaviorActor(java.lang.String name, Initializer initializer)Creates a new behavior actor.BehaviorActor(java.lang.String name, Initializer initializer, MailboxConfig mailboxConfig)Creates a new behavior actor.protectedBehaviorActor(java.lang.String name, Initializer initializer, Strand strand, MailboxConfig mailboxConfig)Creates a new behavior actor.BehaviorActor(java.lang.String name, MailboxConfig mailboxConfig)Creates a new behavior actor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidbehavior()Called bydoRun()as the body of the logic.protected voidcheckCodeSwap()Tests whether this actor has been upgraded via hot code-swapping.protected java.lang.VoiddoRun()An actor must implement this method, which contains the actor's logic.protected InitializergetInitializer()Theinitializerpassed at construction which performs initialization and termination.protected java.lang.ObjecthandleLifecycleMessage(LifecycleMessage m)This method is called by this class during a call to any of thereceivemethods if aLifecycleMessageis found in the mailbox.protected voidhandleMessage(java.lang.Object message)Called by the defaultbehavior()method to handle each incoming message.protected voidinit()Called byonStartto initialize the actor.booleanisRunning()abstract org.slf4j.Loggerlog()TheLoggerobject associated with this actor.protected BehaviormakeRef(ActorRef<java.lang.Object> ref)protected voidonStart()This method is called by theBehaviorActorat the beginning ofdoRun().protected voidonTerminate(java.lang.Throwable cause)This method is called by theBehaviorActorat the end ofdoRun().Behaviorref()Returns the ActorRef to this actor, if it has been started.protected Behaviorself()Returns the ActorRef to this actor, if it has been started.protected voidshutdown()Causes this actor to shut down.Behaviorspawn()Starts a new fiber and runs the actor in it.Behaviorspawn(FiberFactory ff)Starts a new fiber using the given scheduler and runs the actor in it.Behaviorspawn(StrandFactory sf)Starts a new fiber using the given scheduler and runs the actor in it.BehaviorspawnThread()Starts a new thread and runs the actor in it.protected voidterminate(java.lang.Throwable cause)Called byonTerminateto terminate the actor.-
Methods inherited from class co.paralleluniverse.actors.Actor
addLifecycleListener, build, checkThrownIn, close, currentActor, filterMessage, get, get, getActor, getDeathCause, getMonitor, getName, getQueueLength, getStrand, hire, hire, internalSend, internalSendNonSuspendable, interrupt, isClosed, isDone, isInActor, isRegistered, isStarted, join, join, link, linked, mailbox, migrate, migrateAndRestart, monitor, monitorAddDeath, monitorAddMessage, monitorResetSkippedMessages, monitorSkippedMessage, newActor, newActor, onCodeChange, readResolve, receive, receive, receive, register, register, reinstantiate, removeLifecycleListener, removeObserverListeners, run, sendSync, setForwardWatch, setMonitor, setName, setStrand, start, stopMonitor, throwIn, toString, tryReceive, trySend, unlink, unlinked, unregister, unwatch, verifyInActor, verifyOnActorStrand, watch, writeReplace
-
Methods inherited from class co.paralleluniverse.actors.ActorImpl
close, getLifecycleListener, getMailbox, isRecordingLevel, record, record, record, record, record, record, record, sendOrInterrupt
-
-
-
-
Constructor Detail
-
BehaviorActor
protected BehaviorActor(java.lang.String name, Initializer initializer, Strand strand, MailboxConfig mailboxConfig)Creates a new behavior actor.- Parameters:
name- the actor name (may benull).initializer- an optional delegate object that will be run upon actor initialization and termination. May benull.strand- this actor's strand.mailboxConfig- this actor's mailbox settings.
-
BehaviorActor
public BehaviorActor(java.lang.String name, Initializer initializer, MailboxConfig mailboxConfig)Creates a new behavior actor.- Parameters:
name- the actor name (may benull).initializer- an optional delegate object that will be run upon actor initialization and termination. May benull.mailboxConfig- this actor's mailbox settings.
-
BehaviorActor
public BehaviorActor(java.lang.String name, Initializer initializer)Creates a new behavior actor.- Parameters:
name- the actor name (may benull).initializer- an optional delegate object that will be run upon actor initialization and termination. May benull.
-
BehaviorActor
public BehaviorActor(Initializer initializer, MailboxConfig mailboxConfig)
Creates a new behavior actor.- Parameters:
initializer- an optional delegate object that will be run upon actor initialization and termination. May benull.mailboxConfig- this actor's mailbox settings.
-
BehaviorActor
public BehaviorActor(Initializer initializer)
Creates a new behavior actor.- Parameters:
initializer- an optional delegate object that will be run upon actor initialization and termination. May benull.
-
BehaviorActor
public BehaviorActor(java.lang.String name, MailboxConfig mailboxConfig)Creates a new behavior actor.- Parameters:
name- the actor name (may benull).mailboxConfig- this actor's mailbox settings.
-
BehaviorActor
public BehaviorActor(java.lang.String name)
Creates a new behavior actor.- Parameters:
name- the actor name (may benull).
-
BehaviorActor
public BehaviorActor(MailboxConfig mailboxConfig)
Creates a new behavior actor.- Parameters:
mailboxConfig- this actor's mailbox settings.
-
BehaviorActor
public BehaviorActor()
Creates a new behavior actor.
-
-
Method Detail
-
ref
public Behavior ref()
Description copied from class:ActorReturns the ActorRef to this actor, if it has been started.
-
self
protected Behavior self()
Description copied from class:ActorReturns the ActorRef to this actor, if it has been started.
-
spawn
public Behavior spawn(StrandFactory sf)
Description copied from class:ActorStarts a new fiber using the given scheduler and runs the actor in it. The fiber's name will be set to this actor's name.
-
spawn
public Behavior spawn(FiberFactory ff)
Description copied from class:ActorStarts a new fiber using the given scheduler and runs the actor in it. The fiber's name will be set to this actor's name.
-
spawn
public Behavior spawn()
Description copied from class:ActorStarts a new fiber and runs the actor in it. The fiber's name will be set to this actor's name.
-
spawnThread
public Behavior spawnThread()
Description copied from class:ActorStarts a new thread and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawnThreadin classActor<java.lang.Object,java.lang.Void>- Returns:
- This actors' ActorRef
-
shutdown
protected void shutdown()
Causes this actor to shut down.
-
getInitializer
protected Initializer getInitializer()
Theinitializerpassed at construction which performs initialization and termination.
-
onStart
protected void onStart() throws java.lang.InterruptedException, SuspendExecutionThis method is called by theBehaviorActorat the beginning ofdoRun(). By default, this method callsinit().- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
onTerminate
protected void onTerminate(java.lang.Throwable cause) throws java.lang.InterruptedException, SuspendExecutionThis method is called by theBehaviorActorat the end ofdoRun(). By default, this method callsterminate().- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
init
protected void init() throws java.lang.InterruptedException, SuspendExecutionCalled byonStartto initialize the actor. By default, this method callsinitializer.init()if the initializer in non-null; otherwise it does nothing.- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
terminate
protected void terminate(java.lang.Throwable cause) throws SuspendExecutionCalled byonTerminateto terminate the actor. By default, this method callsinitializer.terminateif the initializer in non-null; otherwise it does nothing.- Throws:
SuspendExecution
-
log
public abstract org.slf4j.Logger log()
TheLoggerobject associated with this actor.
-
behavior
protected void behavior() throws java.lang.InterruptedException, SuspendExecutionCalled bydoRun()as the body of the logic. By default, this implementation runs code similar to:while (isRunning()) handleMessage(receive());- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
handleMessage
protected void handleMessage(java.lang.Object message) throws java.lang.InterruptedException, SuspendExecutionCalled by the defaultbehavior()method to handle each incoming message. By default, this method does nothing.- Parameters:
message- the message received by the actor.- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
checkCodeSwap
protected void checkCodeSwap() throws SuspendExecutionDescription copied from class:ActorTests whether this actor has been upgraded via hot code-swapping. If a new version of this actor is found, this method never returns (a specialErroris thrown which causes the actor to restart).- Overrides:
checkCodeSwapin classActor<java.lang.Object,java.lang.Void>- Throws:
SuspendExecution
-
isRunning
public boolean isRunning()
-
doRun
protected java.lang.Void doRun() throws java.lang.InterruptedException, SuspendExecutionAn actor must implement this method, which contains the actor's logic. This method begins executing on the actor's strand.Upon a hot code-swap, this method is re-executed, so it is this method's responsibility to check this actor's state (which may not be blank after a code-swap) when it begins.
This implementation calls
onStart()when it begins,behavior()for the body, andonTerminate()upon termination. The implementation runs code similar to the following:try { onStart(); behavior(); } catch (InterruptedException e) { if (shutdownCalled) { onTerminate(null); return null; } else { onTerminate(e); throw e; } } catch (Exception e) { log().info("Exception!", e); onTerminate(e); throw Exceptions.rethrow(e); } onTerminate(null);- Specified by:
doRunin classActor<java.lang.Object,java.lang.Void>- Returns:
- The actor's return value, which can be obtained with
Actor.get(). - Throws:
java.lang.InterruptedExceptionSuspendExecution
-
handleLifecycleMessage
protected java.lang.Object handleLifecycleMessage(LifecycleMessage m)
This method is called by this class during a call to any of thereceivemethods if aLifecycleMessageis found in the mailbox. By default, if the message is anExitMessageand itswatchisnull, i.e. it's a result of alinkrather than awatch, it will throw aLifecycleException, which will, in turn, cause this exception to be thrown by the call toreceive. This method is not allowed to block. If you want to block as a result of a lifecycle message, return the message from this method (rather than returningnull), and have it processed by the caller toreceive.This implementation respects
ShutdownMessageand, upon receiving it, callsshutdown().- Overrides:
handleLifecycleMessagein classActor<java.lang.Object,java.lang.Void>- Parameters:
m- the message- Returns:
nullif the message has been processed and should not be returned byreceive
-
-