|
org.codehaus.gpars | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object groovyx.gpars.serial.WithSerialId groovyx.gpars.actor.impl.MessageStream groovyx.gpars.actor.Actor groovyx.gpars.actor.AbstractLoopingActor groovyx.gpars.actor.DefaultActor
public class DefaultActor extends AbstractLoopingActor
The DefaultActor class is the base for all stateful actors, who need to maintain implicit state between subsequent message arrivals. Allowing the actor creator to structure code in a continuation-like style with message retrieval react commands mixed within normal code makes implementation of some algorithms particularly easy.
The DefaultActor upon start-up will grab a thread from the associated actor thread pool and run its body. The body is either the parameter passed to the constructor, or the act() method, if no parameter has been set. The parameter takes precedence over the act() method. Once a react() method call is discovered within the actor's body, its Closure-typed parameter will be scheduled for processing on next message arrival. To preserve the actor principle of at-most-one active thread per actor, the next message, however, will only be handled once the currently run code finishes and frees the current thread. It is thus advisable to avoid code after call to react(). The loop() method will ensure its body is executed repeatedly, until the actor either finishes or an optional loop condition is not met.
Field Summary | |
---|---|
private static java.lang.String |
EXPECTED
|
private groovy.lang.Closure |
afterLoopCode
|
private groovy.lang.Closure |
loopClosure
|
private java.lang.Runnable |
loopCode
|
private java.util.concurrent.Callable |
loopCondition
|
private groovy.lang.Closure |
nextContinuation
|
private static long |
serialVersionUID
|
private boolean |
started
|
Fields inherited from class AbstractLoopingActor | |
---|---|
core, currentSender, currentTimerTask, serialVersionUID, stoppedFlag, terminatedFlag, terminatingFlag, timeoutCounter |
Fields inherited from class MessageStream | |
---|---|
serialVersionUID |
Fields inherited from class WithSerialId | |
---|---|
serialHandle, serialVersionUID |
Constructor Summary | |
DefaultActor()
|
|
DefaultActor(java.lang.Runnable code)
If no parameter is provided at construction time, the act() method becomes the actor's body |
Method Summary | |
---|---|
protected void
|
act()
Handles all incoming messages |
private static void
|
checkForBodyArguments(groovy.lang.Closure closure)
|
private static void
|
checkForMessageHandlerArguments(groovy.lang.Closure code)
|
private static void
|
checkForNull(java.lang.Runnable code)
|
private void
|
doLoop(java.util.concurrent.Callable condition, groovy.lang.Closure afterLoopCode, java.lang.Runnable code)
|
private groovy.lang.Closure
|
enhanceClosure(groovy.lang.Closure closure)
|
private boolean
|
evalLoopCondition()
Schedules an ActorAction to take the next message off the message queue and to pass it on to the supplied closure. |
protected void
|
handleStart()
|
void
|
loop(java.lang.Runnable code)
Ensures that the supplied closure will be invoked repeatedly in a loop. |
protected void
|
loop(int numberOfLoops, java.lang.Runnable code)
Ensures that the supplied closure will be invoked repeatedly in a loop. |
protected void
|
loop(int numberOfLoops, groovy.lang.Closure afterLoopCode, java.lang.Runnable code)
|
protected void
|
loop(groovy.lang.Closure condition, java.lang.Runnable code)
Ensures that the supplied closure will be invoked repeatedly in a loop. |
protected void
|
loop(groovy.lang.Closure condition, groovy.lang.Closure afterLoopCode, java.lang.Runnable code)
|
void
|
onMessage(java.lang.Object message)
|
void
|
react(groovy.lang.Closure code)
Schedules an ActorAction to take the next message off the message queue and to pass it on to the supplied closure. |
protected void
|
react(groovy.time.Duration duration, groovy.lang.Closure code)
Schedules an ActorAction to take the next message off the message queue and to pass it on to the supplied closure. |
protected void
|
react(long timeout, java.util.concurrent.TimeUnit timeUnit, groovy.lang.Closure code)
Schedules an ActorAction to take the next message off the message queue and to pass it on to the supplied closure. |
protected void
|
react(long timeout, groovy.lang.Closure code)
Not supported by DefaultActor |
private void
|
runAfterLoopCode(groovy.lang.Closure afterLoopCode)
|
Actor
|
silentStart()
Starts the Actor and sends it the START_MESSAGE to run any afterStart handlers. |
Actor
|
start()
Called once the START_MESSAGE arrives. |
Methods inherited from class AbstractLoopingActor | |
---|---|
cancelCurrentTimeoutTask, doStart, getCore, getSender, hasBeenStopped, initialize, isActive, isFair, makeFair, reply, replyIfExists, runEnhancedWithoutRepliesOnMessages, send, setParallelGroup, setTimeout, silentStart, start, stop, sweepNextMessage, terminate |
Methods inherited from class MessageStream | |
---|---|
call, getRemoteClass, leftShift, reInterrupt, send, send, send, sendAndWait, sendAndWait, sendAndWait |
Methods inherited from class WithSerialId | |
---|---|
createRemoteHandle, getOrCreateSerialHandle, getRemoteClass, writeReplace |
Field Detail |
---|
private static final java.lang.String EXPECTED
private groovy.lang.Closure afterLoopCode
private groovy.lang.Closure loopClosure
private java.lang.Runnable loopCode
private java.util.concurrent.Callable loopCondition
private groovy.lang.Closure nextContinuation
private static final long serialVersionUID
private boolean started
Constructor Detail |
---|
public DefaultActor()
public DefaultActor(java.lang.Runnable code)
Method Detail |
---|
protected void act()
message
- The current message to process
private static void checkForBodyArguments(groovy.lang.Closure closure)
private static void checkForMessageHandlerArguments(groovy.lang.Closure code)
private static void checkForNull(java.lang.Runnable code)
@java.lang.SuppressWarningswhile (!terminatingFlag && nextContinuation == null && (loopCondition == null || evalLoopCondition())) { private void doLoop(java.util.concurrent.Callable condition, groovy.lang.Closure afterLoopCode, java.lang.Runnable code)
private groovy.lang.Closure enhanceClosure(groovy.lang.Closure closure)
private boolean evalLoopCondition()
code
- The code to handle the next message. The reply() and replyIfExists() methods are available inside
the closure to send a reply back to the actor, which sent the original message.
@java.lang.Overridethrow e; protected void handleStart()
public final void loop(@groovy.lang.DelegatesTo java.lang.Runnable code)
code
- The closure to invoke repeatedly
protected final void loop(int numberOfLoops, @groovy.lang.DelegatesTo java.lang.Runnable code)
numberOfLoops
- The loop will only be run the given number of timesafterLoopCode
- Code to run after the main actor's loop finishescode
- The closure to invoke repeatedly
protected final void loop(int numberOfLoops, @groovy.lang.DelegatesToprocessing thread back to the thr groovy.lang.Closure afterLoopCode, @groovy.lang.DelegatesTo java.lang.Runnable code)
protected final void loop(groovy.lang.Closure condition, @groovy.lang.DelegatesTo java.lang.Runnable code)
condition
- A condition to evaluate before each iteration starts. If the condition returns false, the loop exits.afterLoopCode
- Code to run after the main actor's loop finishescode
- The closure to invoke repeatedly
protected final void loop(groovy.lang.Closure condition, @groovy.lang.DelegatesTotedly groovy.lang.Closure afterLoopCode, @groovy.lang.DelegatesTo java.lang.Runnable code)
final void onMessage(java.lang.Object message)
public final void react(@groovy.lang.DelegatesTo groovy.lang.Closure code)
duration
- Time to wait at most for a message to arrive. The actor terminates if a message doesn't arrive within the given timeout.
The TimeCategory DSL to specify timeouts must be enabled explicitly inside the Actor's act() method.code
- The code to handle the next message. The reply() and replyIfExists() methods are available inside
the closure to send a reply back to the actor, which sent the original message.
protected final void react(groovy.time.Duration duration, @groovy.lang.DelegatesTo groovy.lang.Closure code)
timeout
- Time in milliseconds to wait at most for a message to arrive. The actor terminates if a message doesn't arrive within the given timeout.timeUnit
- a TimeUnit determining how to interpret the timeout parametercode
- The code to handle the next message. The reply() and replyIfExists() methods are available inside
the closure to send a reply back to the actor, which sent the original message.
protected final void react(long timeout, java.util.concurrent.TimeUnit timeUnit, @groovy.lang.DelegatesTo.class) final Closure code) { groovy.lang.Closure code)
timeout
- Time in milliseconds to wait at most for a message to arrive. The actor terminates if a message doesn't arrive within the given timeout.code
- The code to handle the next message. The reply() and replyIfExists() methods are available inside
the closure to send a reply back to the actor, which sent the original message.
protected final void react(long timeout, @groovy.lang.DelegatesTo groovy.lang.Closure code)
private void runAfterLoopCode(groovy.lang.Closure afterLoopCode)
@java.lang.Override public Actor silentStart()
@java.lang.Overrideif (startCode != null) { public Actor start()
Copyright © 2008–2013 Václav Pech. All Rights Reserved.