org.codehaus.gpars

groovyx.gpars.actor
[Java] Class Actor

java.lang.Object
  groovyx.gpars.serial.WithSerialId
      groovyx.gpars.actor.impl.MessageStream
          groovyx.gpars.actor.Actor

public abstract class Actor
extends MessageStream

Actors are active objects, which borrow a thread from a thread pool. The Actor interface provides means to send messages to the actor, start and stop the background thread as well as check its status.

Authors:
Vaclav Pech, Alex Tkachman


Nested Class Summary
static class Actor.MyRemoteHandle

static class Actor.RemoteActor

 
Field Summary
protected static java.lang.String ACTOR_HAS_ALREADY_BEEN_STARTED

private static java.lang.String AFTER_START

static java.lang.String CANNOT_SEND_REPLIES_NO_SENDER_HAS_BEEN_REGISTERED

private static Object[] EMPTY_ARGUMENTS

private static java.lang.String ON_DELIVERY_ERROR

private static java.lang.String RESPONDS_TO

protected static ActorMessage START_MESSAGE

protected static ActorMessage STOP_MESSAGE

protected static ActorMessage TERMINATE_MESSAGE

static java.lang.String TIMEOUT

protected static ActorMessage TIMEOUT_MESSAGE

private static java.lang.ThreadLocal currentActorPerThread

Maps each thread to the actor it currently processes.

protected java.lang.Thread currentThread

private DataflowExpression joinLatch

private groovy.lang.Closure onStop

protected PGroup parallelGroup

The parallel group to which the message stream belongs

private static long serialVersionUID

protected static java.util.Timer timer

Timer holding timeouts for react methods

 
Fields inherited from class MessageStream
serialVersionUID
 
Fields inherited from class WithSerialId
serialHandle, serialVersionUID
 
Constructor Summary
protected Actor()

protected Actor(DataflowExpression joinLatch)

Constructor to be used by deserialization

protected Actor(DataflowExpression joinLatch, PGroup parallelGroup)

 
Method Summary
private boolean callDynamic(java.lang.String method, Object[] args)

protected ActorMessage createActorMessage(java.lang.Object message)

protected RemoteHandle createRemoteHandle(SerialHandle handle, SerialContext host)

protected static void deregisterCurrentActorWithThread()

Deregisters the actor registered from the thread

DataflowExpression getJoinLatch()

Join-point for this actor

PGroup getParallelGroup()

Retrieves the group to which the actor belongs

protected void handleException(java.lang.Throwable exception)

protected void handleInterrupt(java.lang.InterruptedException exception)

protected void handleStart()

protected void handleTermination()

protected void handleTimeout()

protected boolean hasBeenStopped()

boolean isActive()

Checks the current status of the Actor.

boolean isActorThread()

Checks whether the current thread is the actor's current thread.

void join()

Joins the actor.

void join(MessageStream listener)

Notify listener when finished

void join(long timeout, java.util.concurrent.TimeUnit unit)

Joins the actor.

void join(groovy.time.BaseDuration duration)

Joins the actor.

void onStop(groovy.lang.Closure onStop)

Set on stop handler for this actor

protected static void registerCurrentActorWithThread(Actor currentActor)

Registers the actor with the current thread

MessageStream sendAndContinue(T message, groovy.lang.Closure closure)

Sends a message and execute continuation when reply became available.

void setParallelGroup(PGroup group)

Sets the parallel group.

Actor silentStart()

Starts the Actor without sending the START_MESSAGE message to speed the start-up.

Actor start()

Starts the Actor and sends it the START_MESSAGE to run any afterStart handlers.

Actor stop()

Send message to stop to the Actor.

protected ActorMessage sweepNextMessage()

Removes the head of the message queue

java.util.List sweepQueue()

Clears the message queue returning all the messages it held.

Actor terminate()

Terminates the Actor.

static Actor threadBoundActor()

Retrieves the actor registered with the current thread

 
Methods inherited from class MessageStream
call, getRemoteClass, leftShift, reInterrupt, send, send, send, sendAndWait, sendAndWait, sendAndWait
 
Methods inherited from class WithSerialId
createRemoteHandle, getOrCreateSerialHandle, getRemoteClass, writeReplace
 
Methods inherited from class java.lang.Object
java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#wait(long, int), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Field Detail

ACTOR_HAS_ALREADY_BEEN_STARTED

protected static final java.lang.String ACTOR_HAS_ALREADY_BEEN_STARTED


AFTER_START

private static final java.lang.String AFTER_START


CANNOT_SEND_REPLIES_NO_SENDER_HAS_BEEN_REGISTERED

public static final java.lang.String CANNOT_SEND_REPLIES_NO_SENDER_HAS_BEEN_REGISTERED


EMPTY_ARGUMENTS

private static final Object[] EMPTY_ARGUMENTS


ON_DELIVERY_ERROR

private static final java.lang.String ON_DELIVERY_ERROR


RESPONDS_TO

private static final java.lang.String RESPONDS_TO


START_MESSAGE

protected static final ActorMessage START_MESSAGE


STOP_MESSAGE

protected static final ActorMessage STOP_MESSAGE


TERMINATE_MESSAGE

protected static final ActorMessage TERMINATE_MESSAGE


TIMEOUT

@SuppressWarnings({"ConstantDeclaredInAbstractClass"})
public static final java.lang.String TIMEOUT


TIMEOUT_MESSAGE

protected static final ActorMessage TIMEOUT_MESSAGE


currentActorPerThread

private static final java.lang.ThreadLocal currentActorPerThread
Maps each thread to the actor it currently processes. Used in the send() method to remember the sender of each message for potential replies


currentThread

protected java.lang.Thread currentThread


joinLatch

private final DataflowExpression joinLatch


onStop

private groovy.lang.Closure onStop


parallelGroup

protected PGroup parallelGroup
The parallel group to which the message stream belongs


serialVersionUID

private static final long serialVersionUID


timer

protected static final java.util.Timer timer
Timer holding timeouts for react methods


 
Constructor Detail

Actor

protected Actor()


Actor

protected Actor(DataflowExpression joinLatch)
Constructor to be used by deserialization
Parameters:
joinLatch - The instance of DataflowExpression to use for join operation


Actor

protected Actor(DataflowExpression joinLatch, PGroup parallelGroup)


 
Method Detail

callDynamic

private boolean callDynamic(java.lang.String method, Object[] args)


createActorMessage

protected final ActorMessage createActorMessage(java.lang.Object message)


createRemoteHandle

@Override
protected RemoteHandle createRemoteHandle(SerialHandle handle, SerialContext host)


deregisterCurrentActorWithThread

protected static void deregisterCurrentActorWithThread()
Deregisters the actor registered from the thread


getJoinLatch

public DataflowExpression getJoinLatch()
Join-point for this actor
Returns:
The DataflowExpression instance, which is used to join this actor


getParallelGroup

public final PGroup getParallelGroup()
Retrieves the group to which the actor belongs
Returns:
The group


handleException

@SuppressWarnings({"UseOfSystemOutOrSystemErr"})
protected void handleException(java.lang.Throwable exception)


handleInterrupt

@SuppressWarnings({"TypeMayBeWeakened", "UseOfSystemOutOrSystemErr"})
protected void handleInterrupt(java.lang.InterruptedException exception)


handleStart

@SuppressWarnings("unchecked")
protected void handleStart()


handleTermination

protected void handleTermination()


handleTimeout

protected void handleTimeout()


hasBeenStopped

protected boolean hasBeenStopped()


isActive

public boolean isActive()
Checks the current status of the Actor.
Returns:
current status of the Actor.


isActorThread

public final boolean isActorThread()
Checks whether the current thread is the actor's current thread.
Returns:
True if invoked from within an actor thread


join

public final void join()
Joins the actor. Waits for its termination.
throws:
InterruptedException when interrupted while waiting


join

public final void join(MessageStream listener)
Notify listener when finished
throws:
InterruptedException if interrupted while waiting
Parameters:
listener - listener to notify


join

public final void join(long timeout, java.util.concurrent.TimeUnit unit)
Joins the actor. Waits for its termination.
throws:
InterruptedException if interrupted while waiting
Parameters:
timeout - timeout
unit - units of timeout


join

public final void join(groovy.time.BaseDuration duration)
Joins the actor. Waits for its termination.
throws:
InterruptedException if interrupted while waiting
Parameters:
duration - timeout to wait


onStop

public final void onStop(groovy.lang.Closure onStop)
Set on stop handler for this actor
Parameters:
onStop - The code to invoke when stopping


registerCurrentActorWithThread

protected static void registerCurrentActorWithThread(Actor currentActor)
Registers the actor with the current thread
Parameters:
currentActor - The actor to register


sendAndContinue

@SuppressWarnings({"AssignmentToMethodParameter"})
public final MessageStream sendAndContinue(T message, groovy.lang.Closure closure)
Sends a message and execute continuation when reply became available.
throws:
InterruptedException if interrupted while waiting
Parameters:
message - message to send
closure - closure to execute when reply became available
Returns:
The message that came in reply to the original send.


setParallelGroup

public void setParallelGroup(PGroup group)
Sets the parallel group. It can only be invoked before the actor is started.
Parameters:
group - new group


silentStart

public Actor silentStart()
Starts the Actor without sending the START_MESSAGE message to speed the start-up. The potential custom afterStart handlers won't be run. No messages can be sent or received before an Actor is started.
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.
Returns:
same actor


stop

public Actor stop()
Send message to stop to the Actor. The actor will finish processing the current message and all unprocessed messages will be passed to the afterStop method, if such exists. No new messages will be accepted since that point. Has no effect if the Actor is not started.
Returns:
same actor


sweepNextMessage

protected ActorMessage sweepNextMessage()
Removes the head of the message queue
Returns:
The head message, or null, if the message queue is empty


sweepQueue

@SuppressWarnings("unchecked")
final java.util.List sweepQueue()
Clears the message queue returning all the messages it held.
Returns:
The messages stored in the queue


terminate

public Actor terminate()
Terminates the Actor. Unprocessed messages will be passed to the afterStop method, if exists. No new messages will be accepted since that point. Has no effect if the Actor is not started.
Returns:
same actor


threadBoundActor

public static Actor threadBoundActor()
Retrieves the actor registered with the current thread
Returns:
The associated actor


 

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