groovyx.gpars.actor
Class AbstractLoopingActor

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
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
DefaultActor, DynamicDispatchActor, InternalActor, ReactiveActor

public abstract class AbstractLoopingActor
extends Actor

Wraps all actors that repeatedly loop through incoming messages and hold no implicit state between subsequent messages.

Author:
Vaclav Pech Date: Aug 23, 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  AsyncMessagingCore core
          Holds the particular instance of async messaging core to use
private  MessageStream currentSender
           
private  ActorTimerTask currentTimerTask
           
private static long serialVersionUID
           
private  boolean stoppedFlag
           
private  boolean terminatedFlag
           
(package private)  boolean terminatingFlag
           
private  int timeoutCounter
           
 
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
AbstractLoopingActor()
           
 
Method Summary
private  void cancelCurrentTimeoutTask()
           
private  Actor doStart()
           
(package private)  AsyncMessagingCore getCore()
           
protected  MessageStream getSender()
          Retrieves the sender actor of the currently processed message.
protected  boolean hasBeenStopped()
           
protected  void initialize(groovy.lang.Closure code)
          Builds the async messaging core using the supplied code handler
 boolean isActive()
          Checks the current status of the Actor.
 boolean isFair()
          Retrieves the actor's fairness flag Fair actors give up the thread after processing each message, non-fair actors keep a thread until their message queue is empty.
 void makeFair()
          Makes the actor fair.
protected  void reply(java.lang.Object message)
          Sends a reply to all currently processed messages.
protected  void replyIfExists(java.lang.Object message)
          Sends a reply to all currently processed messages, which have been sent by an actor.
private  void runEnhancedWithoutRepliesOnMessages(ActorMessage message, groovy.lang.Closure code, java.lang.Object arguments)
           
 MessageStream send(java.lang.Object message)
          Send message to stream and return immediately
 void setParallelGroup(groovyx.gpars.group.PGroup group)
          Sets the parallel group.
protected  void setTimeout(long timeout)
           
 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
 Actor terminate()
          Terminates the Actor.
 
Methods inherited from class groovyx.gpars.actor.Actor
createActorMessage, createRemoteHandle, deregisterCurrentActorWithThread, getJoinLatch, getParallelGroup, handleException, handleInterrupt, handleStart, 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

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

stoppedFlag

private volatile boolean stoppedFlag

terminatedFlag

private volatile boolean terminatedFlag

terminatingFlag

volatile boolean terminatingFlag

currentTimerTask

private ActorTimerTask currentTimerTask

timeoutCounter

private int timeoutCounter

currentSender

private MessageStream currentSender

core

private AsyncMessagingCore core
Holds the particular instance of async messaging core to use

Constructor Detail

AbstractLoopingActor

public AbstractLoopingActor()
Method Detail

getCore

final AsyncMessagingCore getCore()

initialize

protected final void initialize(groovy.lang.Closure code)
Builds the async messaging core using the supplied code handler

Parameters:
code - Code to run on each message

isFair

public final boolean isFair()
Retrieves the actor's fairness flag Fair actors give up the thread after processing each message, non-fair actors keep a thread until their message queue is empty. Non-fair actors tend to perform better than fair ones.

Returns:
True for fair actors, false for non-fair ones. actors are non-fair by default.

makeFair

public final void makeFair()
Makes the actor fair. Actors are non-fair by default. Fair actors give up the thread after processing each message, non-fair actors keep a thread until their message queue is empty. Non-fair actors tend to perform better than fair ones.


setTimeout

protected final void setTimeout(long timeout)

cancelCurrentTimeoutTask

private void cancelCurrentTimeoutTask()

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.

Specified by:
silentStart in class Actor
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.

Specified by:
start in class Actor
Returns:
same actor

doStart

private Actor doStart()

stop

public final Actor stop()
Description copied from class: Actor
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.

Specified by:
stop in class Actor
Returns:
same actor

terminate

public final Actor terminate()
Description copied from class: Actor
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.

Specified by:
terminate in class Actor
Returns:
same actor

isActive

public final boolean isActive()
Description copied from class: Actor
Checks the current status of the Actor.

Specified by:
isActive in class Actor
Returns:
current status of the Actor.

hasBeenStopped

protected final boolean hasBeenStopped()
Specified by:
hasBeenStopped in class Actor

sweepNextMessage

protected ActorMessage sweepNextMessage()
Removes the head of the message queue

Specified by:
sweepNextMessage in class Actor
Returns:
The head message, or null, if the message queue is empty

send

public MessageStream send(java.lang.Object message)
Description copied from class: MessageStream
Send message to stream and return immediately

Specified by:
send in class MessageStream
Parameters:
message - message to send
Returns:
always return message stream itself

setParallelGroup

public void setParallelGroup(groovyx.gpars.group.PGroup group)
Description copied from class: Actor
Sets the parallel group. It can only be invoked before the actor is started.

Overrides:
setParallelGroup in class Actor
Parameters:
group - new group

getSender

protected final MessageStream getSender()
Retrieves the sender actor of the currently processed message.

Returns:
The sender of the currently processed message or null, if the message was not sent by an actor
Throws:
ActorReplyException - If some of the replies failed to be sent.

reply

protected final void reply(java.lang.Object message)
Sends a reply to all currently processed messages. Throws ActorReplyException if some messages have not been sent by an actor. For such cases use replyIfExists().

Parameters:
message - reply message
Throws:
ActorReplyException - If some of the replies failed to be sent.

replyIfExists

protected final void replyIfExists(java.lang.Object message)
Sends a reply to all currently processed messages, which have been sent by an actor. Ignores potential errors when sending the replies, like no sender or sender already stopped.

Parameters:
message - reply message

runEnhancedWithoutRepliesOnMessages

private void runEnhancedWithoutRepliesOnMessages(ActorMessage message,
                                                 groovy.lang.Closure code,
                                                 java.lang.Object arguments)

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