groovyx.gpars.actor
Class DefaultActor

java.lang.Object
  extended by groovyx.gpars.serial.WithSerialId
      extended by groovyx.gpars.actor.impl.MessageStream
          extended by groovyx.gpars.actor.Actor
              extended by groovyx.gpars.actor.AbstractLoopingActor
                  extended by groovyx.gpars.actor.DefaultActor
All Implemented Interfaces:
java.io.Serializable

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.

Author:
Vaclav Pech Date: Nov 4th 2010
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class groovyx.gpars.actor.Actor
Actor.MyRemoteHandle, Actor.RemoteActor
 
Nested classes/interfaces inherited from class groovyx.gpars.actor.impl.MessageStream
MessageStream.RemoteMessageStream, MessageStream.SendTo
 
Field Summary
private  groovy.lang.Closure afterLoopCode
           
private static java.lang.String EXPECTED
           
private  groovy.lang.Closure loopClosure
           
private  java.lang.Runnable loopCode
          Misused also for the code to run at start-up
private  java.util.concurrent.Callable<java.lang.Boolean> loopCondition
           
private  groovy.lang.Closure nextContinuation
           
private static long serialVersionUID
           
private  boolean started
           
 
Fields inherited from class groovyx.gpars.actor.AbstractLoopingActor
terminatingFlag
 
Fields inherited from class groovyx.gpars.actor.Actor
ACTOR_HAS_ALREADY_BEEN_STARTED, CANNOT_SEND_REPLIES_NO_SENDER_HAS_BEEN_REGISTERED, currentThread, parallelGroup, START_MESSAGE, STOP_MESSAGE, TERMINATE_MESSAGE, TIMEOUT, TIMEOUT_MESSAGE, timer
 
Fields inherited from class groovyx.gpars.serial.WithSerialId
serialHandle
 
Constructor Summary
DefaultActor()
          Creates an actor, which will execute its act() methods
DefaultActor(java.lang.Runnable code)
          Creates an actor, which will execute the supplied code
 
Method Summary
protected  void act()
          If no parameter is provided at construction time, the act() method becomes the actor's body
private  void doLoop(java.util.concurrent.Callable<java.lang.Boolean> condition, groovy.lang.Closure afterLoopCode, java.lang.Runnable code)
          Ensures that the supplied closure will be invoked repeatedly in a loop.
private  groovy.lang.Closure enhanceClosure(groovy.lang.Closure closure)
           
private  boolean evalLoopCondition()
           
protected  void handleStart()
          Called once the START_MESSAGE arrives.
private static void checkForBodyArguments(groovy.lang.Closure closure)
           
private static void checkForMessageHandlerArguments(groovy.lang.Closure code)
           
private static void checkForNull(java.lang.Runnable code)
           
protected  void loop(groovy.lang.Closure condition, groovy.lang.Closure afterLoopCode, java.lang.Runnable code)
          Ensures that the supplied closure will be invoked repeatedly in a loop.
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(int numberOfLoops, groovy.lang.Closure afterLoopCode, 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.
 void loop(java.lang.Runnable code)
          Ensures that the supplied closure will be invoked repeatedly in a loop.
(package private)  void onMessage(java.lang.Object message)
          Handles all incoming messages
 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, 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.
private  void runAfterLoopCode(groovy.lang.Closure afterLoopCode)
           
 Actor silentStart()
          Not supported by DefaultActor
 Actor start()
          Starts the Actor and sends it the START_MESSAGE to run any afterStart handlers.
 
Methods inherited from class groovyx.gpars.actor.AbstractLoopingActor
getCore, getSender, hasBeenStopped, initialize, isActive, isFair, makeFair, reply, replyIfExists, send, setParallelGroup, setTimeout, stop, sweepNextMessage, terminate
 
Methods inherited from class groovyx.gpars.actor.Actor
createActorMessage, createRemoteHandle, deregisterCurrentActorWithThread, getJoinLatch, getParallelGroup, handleException, handleInterrupt, handleTermination, handleTimeout, isActorThread, join, join, join, join, onStop, registerCurrentActorWithThread, sendAndContinue, sweepQueue, threadBoundActor
 
Methods inherited from class groovyx.gpars.actor.impl.MessageStream
call, getRemoteClass, leftShift, send, send, sendAndWait, sendAndWait, sendAndWait
 
Methods inherited from class groovyx.gpars.serial.WithSerialId
getOrCreateSerialHandle, writeReplace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nextContinuation

private groovy.lang.Closure nextContinuation

loopClosure

private groovy.lang.Closure loopClosure

loopCode

private java.lang.Runnable loopCode
Misused also for the code to run at start-up


loopCondition

private java.util.concurrent.Callable<java.lang.Boolean> loopCondition

afterLoopCode

private groovy.lang.Closure afterLoopCode

started

private boolean started

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

EXPECTED

private static final java.lang.String EXPECTED
See Also:
Constant Field Values
Constructor Detail

DefaultActor

public DefaultActor()
Creates an actor, which will execute its act() methods


DefaultActor

public DefaultActor(java.lang.Runnable code)
Creates an actor, which will execute the supplied code

Parameters:
code - A Runnable or Closure to be considered the actor's body
Method Detail

act

protected void act()
If no parameter is provided at construction time, the act() method becomes the actor's body


onMessage

final void onMessage(java.lang.Object message)
Handles all incoming messages

Parameters:
message - The current message to process

loop

public final void loop(java.lang.Runnable code)
Ensures that the supplied closure will be invoked repeatedly in a loop. The method never returns, but instead frees the processing thread back to the thread pool.

Parameters:
code - The closure to invoke repeatedly

loop

protected final void loop(int numberOfLoops,
                          java.lang.Runnable code)
Ensures that the supplied closure will be invoked repeatedly in a loop. The method never returns, but instead frees the processing thread back to the thread pool.

Parameters:
numberOfLoops - The loop will only be run the given number of times
code - The closure to invoke repeatedly

loop

protected final void loop(int numberOfLoops,
                          groovy.lang.Closure afterLoopCode,
                          java.lang.Runnable code)
Ensures that the supplied closure will be invoked repeatedly in a loop. The method never returns, but instead frees the processing thread back to the thread pool.

Parameters:
numberOfLoops - The loop will only be run the given number of times
afterLoopCode - Code to run after the main actor's loop finishes
code - The closure to invoke repeatedly

loop

protected final void loop(groovy.lang.Closure condition,
                          java.lang.Runnable code)
Ensures that the supplied closure will be invoked repeatedly in a loop. The method never returns, but instead frees the processing thread back to the thread pool.

Parameters:
condition - A condition to evaluate before each iteration starts. If the condition returns false, the loop exits.
code - The closure to invoke repeatedly

loop

protected final void loop(groovy.lang.Closure condition,
                          groovy.lang.Closure afterLoopCode,
                          java.lang.Runnable code)
Ensures that the supplied closure will be invoked repeatedly in a loop. The method never returns, but instead frees the processing thread back to the thread pool.

Parameters:
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 finishes
code - The closure to invoke repeatedly

doLoop

private void doLoop(java.util.concurrent.Callable<java.lang.Boolean> condition,
                    groovy.lang.Closure afterLoopCode,
                    java.lang.Runnable code)
Ensures that the supplied closure will be invoked repeatedly in a loop. The method never returns, but instead frees the processing thread back to the thread pool.

Parameters:
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 finishes
code - The closure to invoke repeatedly

runAfterLoopCode

private void runAfterLoopCode(groovy.lang.Closure afterLoopCode)

evalLoopCondition

private boolean evalLoopCondition()

react

public final 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. The method never returns, but instead frees the processing thread back to the thread pool.

Parameters:
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.

react

protected final 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. The method never returns, but instead frees the processing thread back to the thread pool.

Parameters:
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.

react

protected final 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. The method never returns, but instead frees the processing thread back to the thread pool.

Parameters:
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 parameter
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.

react

protected final void react(long timeout,
                           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. The method never returns, but instead frees the processing thread back to the thread pool. Also adds reply() and replyIfExists() methods to the currentActor and the message. These methods will call send() on the target actor (the sender of the original message). The reply()/replyIfExists() methods invoked on the actor will be sent to all currently processed messages, reply()/replyIfExists() invoked on a message will send a reply to the sender of that particular message only.

Parameters:
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.

silentStart

public Actor silentStart()
Not supported by DefaultActor

Overrides:
silentStart in class AbstractLoopingActor
Returns:
same actor

start

public Actor start()
Starts the Actor and sends it the START_MESSAGE to run any afterStart handlers. No messages can be sent or received before an Actor is started.

Overrides:
start in class AbstractLoopingActor
Returns:
same actor

handleStart

protected void handleStart()
Called once the START_MESSAGE arrives. We need to run the actor's body here, letting it set nextContinuation to hols the next message handler

Overrides:
handleStart in class Actor

enhanceClosure

private groovy.lang.Closure enhanceClosure(groovy.lang.Closure closure)

checkForNull

private static void checkForNull(java.lang.Runnable code)

checkForBodyArguments

private static void checkForBodyArguments(groovy.lang.Closure closure)

checkForMessageHandlerArguments

private static void checkForMessageHandlerArguments(groovy.lang.Closure code)

Copyright © 2008–2010 Václav Pech. All Rights Reserved.