akka.agent

Agent

class Agent[T] extends AnyRef

The Agent class was inspired by agents in Clojure.

Agents provide asynchronous change of individual locations. Agents are bound to a single storage location for their lifetime, and only allow mutation of that location (to a new state) to occur as a result of an action. Update actions are functions that are asynchronously applied to the Agent's state and whose return value becomes the Agent's new state. The state of an Agent should be immutable.

While updates to Agents are asynchronous, the state of an Agent is always immediately available for reading by any thread (using get or apply) without any messages.

Agents are reactive. The update actions of all Agents get interleaved amongst threads in a thread pool. At any point in time, at most one send action for each Agent is being executed. Actions dispatched to an agent from another thread will occur in the order they were sent, potentially interleaved with actions dispatched to the same agent from other sources.

If an Agent is used within an enclosing transaction, then it will participate in that transaction. Agents are integrated with the STM - any dispatches made in a transaction are held until that transaction commits, and are discarded if it is retried or aborted.

Example of usage:

val agent = Agent(5)

agent send (_ * 2)

...

val result = agent()
// use result ...

agent.close


Agent is also monadic, which means that you can compose operations using for-comprehensions. In monadic usage the original agents are not touched but new agents are created. So the old values (agents) are still available as-is. They are so-called 'persistent'.

Example of monadic usage:

val agent1 = Agent(3)
val agent2 = Agent(5)

for (value <- agent1) {
  result = value + 1
}

val agent3 = for (value <- agent1) yield value + 1

val agent4 = for {
  value1 <- agent1
  value2 <- agent2
} yield value1 + value2

agent1.close
agent2.close
agent3.close
agent4.close
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Hide All
  2. Show all
  1. Agent
  2. AnyRef
  3. Any
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Agent(initialValue: T, system: ActorSystem)

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. def alter(f: Function[T, T], timeout: Long): Future[T]

    Java API Dispatch a function to update the internal state, and return a Future where that new state can be obtained within the given timeout

  7. def alter(f: (T) ⇒ T)(timeout: Timeout): Future[T]

    Dispatch a function to update the internal state, and return a Future where that new state can be obtained within the given timeout.

  8. def alterOff(f: Function[T, T], timeout: Long): Unit

    Java API: Dispatch a function to update the internal state but on its own thread, and return a Future where that new state can be obtained within the given timeout.

    Java API: Dispatch a function to update the internal state but on its own thread, and return a Future where that new state can be obtained within the given timeout. This does not use the reactive thread pool and can be used for long-running or blocking operations. Dispatches using either alterOff or alter will still be executed in order.

  9. def alterOff(f: (T) ⇒ T)(timeout: Timeout): Future[T]

    Dispatch a function to update the internal state but on its own thread, and return a Future where that new state can be obtained within the given timeout.

    Dispatch a function to update the internal state but on its own thread, and return a Future where that new state can be obtained within the given timeout. This does not use the reactive thread pool and can be used for long-running or blocking operations. Dispatches using either alterOff or alter will still be executed in order.

  10. def apply(): T

    Read the internal state of the agent.

  11. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  12. def await(implicit timeout: Timeout): T

    Gets this agent's value after all currently queued updates have completed.

  13. def clone(): AnyRef

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  14. def close(): Unit

    Closes the agents and makes it eligible for garbage collection.

    Closes the agents and makes it eligible for garbage collection. A closed agent cannot accept any send actions.

  15. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  16. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  17. def finalize(): Unit

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  18. def flatMap[B](f: Function[T, Agent[B]]): Agent[B]

    Java API: Flatmap this agent to a new agent, applying the function to the internal state.

    Java API: Flatmap this agent to a new agent, applying the function to the internal state. Does not change the value of this agent.

  19. def flatMap[B](f: (T) ⇒ Agent[B]): Agent[B]

    Flatmap this agent to a new agent, applying the function to the internal state.

    Flatmap this agent to a new agent, applying the function to the internal state. Does not change the value of this agent.

  20. def foreach(f: Procedure[T]): Unit

    Java API: Applies the function to the internal state.

    Java API: Applies the function to the internal state. Does not change the value of this agent.

  21. def foreach[U](f: (T) ⇒ U): Unit

    Applies the function to the internal state.

    Applies the function to the internal state. Does not change the value of this agent.

  22. def future(implicit timeout: Timeout): Future[T]

    A future to the current value that will be completed after any currently queued updates.

  23. def get(): T

    Read the internal state of the agent.

  24. final def getClass(): java.lang.Class[_]

    Definition Classes
    AnyRef → Any
  25. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  26. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  27. def map[B](f: Function[T, B]): Agent[B]

    Java API: Map this agent to a new agent, applying the function to the internal state.

    Java API: Map this agent to a new agent, applying the function to the internal state. Does not change the value of this agent.

  28. def map[B](f: (T) ⇒ B): Agent[B]

    Map this agent to a new agent, applying the function to the internal state.

    Map this agent to a new agent, applying the function to the internal state. Does not change the value of this agent.

  29. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  30. final def notify(): Unit

    Definition Classes
    AnyRef
  31. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  32. def resume(): Unit

    Resumes processing of send actions for the agent.

  33. def send(f: Function[T, T]): Unit

    Java API: Dispatch a function to update the internal state.

  34. def send(newValue: T): Unit

    Dispatch a new value for the internal state.

    Dispatch a new value for the internal state. Behaves the same as sending a function (x => newValue).

  35. def send(f: (T) ⇒ T): Unit

    Dispatch a function to update the internal state.

  36. def sendOff(f: Function[T, T]): Unit

    Java API: Dispatch a function to update the internal state but on its own thread.

    Java API: Dispatch a function to update the internal state but on its own thread. This does not use the reactive thread pool and can be used for long-running or blocking operations. Dispatches using either sendOff or send will still be executed in order.

  37. def sendOff(f: (T) ⇒ T): Unit

    Dispatch a function to update the internal state but on its own thread.

    Dispatch a function to update the internal state but on its own thread. This does not use the reactive thread pool and can be used for long-running or blocking operations. Dispatches using either sendOff or send will still be executed in order.

  38. def suspend(): Unit

    Suspends processing of send actions for the agent.

  39. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  40. def toString(): String

    Definition Classes
    AnyRef → Any
  41. def update(newValue: T): Unit

    Dispatch a new value for the internal state.

    Dispatch a new value for the internal state. Behaves the same as sending a function (x => newValue).

  42. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  43. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  44. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()

Inherited from AnyRef

Inherited from Any