org.curjent.impl.agent
Class Messengers

java.lang.Object
  extended by org.curjent.impl.agent.Messengers

final class Messengers
extends Object

Runs messengers in background threads. Each messenger has an associated task to execute agent calls.

See Also:
Controller, AgentTasks, Executor

Field Summary
private  boolean busy
          true when the last attempt to acquire a task was unsuccessful.
private static ThreadLocal<Messenger> current
          The Messenger, if any, associated with the current thread.
(package private) static Executor DEFAULT_EXECUTOR
          Default executor for running messengers in background threads.
private  Executor executor
          Current executor for starting new messengers.
private  boolean pending
          true while the executor starts up a new messenger.
(package private)  AgentTasks tasks
          Source for agent tasks.
private  HashSet<Thread> threads
          The set of threads running messengers.
 
Constructor Summary
Messengers(AgentTasks tasks)
          Initializes the messengers.
 
Method Summary
(package private) static Messenger current()
          Returns the messenger associated with the current thread or null if none.
(package private)  Executor getExecutor()
          Returns the current executor.
(package private)  boolean isExecutorBusy()
          Returns true if an execute operation is pending.
(package private)  boolean isTasksBusy()
          Returns true if the last call acquire returned null.
(package private)  void release(Messenger messenger)
          Releases a messenger and its task.
(package private)  void setExecutor(Executor executor)
          Changes the executor.
(package private)  void start(Controller controller)
          Attempts to start a new messenger.
(package private)  boolean startable()
          Returns true if neither the tasks nor the executor is busy.
(package private)  void started(Messenger messenger)
          Clears the pending flag.
(package private)  Thread[] threads()
          Returns the set of threads running messengers or null if none.
 String toString()
          Returns the number of executing threads.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_EXECUTOR

static final Executor DEFAULT_EXECUTOR
Default executor for running messengers in background threads.

See Also:
Agent.getDefaultExecutor()

tasks

final AgentTasks tasks
Source for agent tasks. A messenger cannot be started unless a task can be acquired.


executor

private volatile Executor executor
Current executor for starting new messengers.

See Also:
AgentConfig.getExecutor()

busy

private boolean busy
true when the last attempt to acquire a task was unsuccessful. No more messengers can start while busy. Cleared when a messenger is released.

See Also:
AgentTasks.acquire(), release(Messenger)

pending

private boolean pending
true while the executor starts up a new messenger. Cleared after the messenger starts. No more messengers can start while pending. This prevents too many threads starting up too quickly with not enough to do.

See Also:
Executor.execute(Runnable), Messenger.run()

current

private static final ThreadLocal<Messenger> current
The Messenger, if any, associated with the current thread.


threads

private final HashSet<Thread> threads
The set of threads running messengers.

Constructor Detail

Messengers

Messengers(AgentTasks tasks)
Initializes the messengers.

Method Detail

startable

boolean startable()
Returns true if neither the tasks nor the executor is busy.


isTasksBusy

boolean isTasksBusy()
Returns true if the last call acquire returned null.


isExecutorBusy

boolean isExecutorBusy()
Returns true if an execute operation is pending.


start

void start(Controller controller)
     throws Throwable
Attempts to start a new messenger. Returns immediately if either busy or pending. Otherwise, attempts to acquire a new task. If AgentTasks.acquire() returns null, sets the busy flag and returns. If a task is returned, a new Messenger is started with the executor and pending is set to true.

Throws:
Throwable

started

void started(Messenger messenger)
Clears the pending flag. Called when a pending Messenger starts running.

See Also:
Messenger.run()

release

void release(Messenger messenger)
       throws Throwable
Releases a messenger and its task. Clears the busy flag. Called when no messages are currently available for the messenger to execute.

Throws:
Throwable

threads

Thread[] threads()
Returns the set of threads running messengers or null if none.


current

static Messenger current()
Returns the messenger associated with the current thread or null if none.


getExecutor

Executor getExecutor()
Returns the current executor.

See Also:
AgentConfig.getExecutor()

setExecutor

void setExecutor(Executor executor)
Changes the executor.

Throws:
NullPointerException - executor is null.
See Also:
AgentConfig.setExecutor(Executor)

toString

public String toString()
Returns the number of executing threads.

Overrides:
toString in class Object


Copyright 2009-2011 Tom Landon
Apache License 2.0