akka.agent

Agent

abstract 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.

Example of usage:

val agent = Agent(5)

agent send (_ * 2)

...

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


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

DEPRECATED STM SUPPORT

Agents participating in enclosing STM transaction is a deprecated feature in 2.3.

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.

Source
Agent.scala
Linear Supertypes
Type Hierarchy Learn more about scaladoc diagrams
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Agent
  2. AnyRef
  3. Any
Implicitly
  1. by any2stringadd
  2. by any2stringfmt
  3. by any2ArrowAssoc
  4. by any2Ensuring
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Agent()

Abstract Value Members

  1. abstract def alter(f: (T) ⇒ T): Future[T]

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

    Dispatch a function to update the internal state, and return a Future where that new state can be obtained. In Java, pass in an instance of akka.dispatch.Mapper.

  2. abstract def alter(newValue: T): Future[T]

    Dispatch an update to the internal state, and return a Future where that new state can be obtained.

    Dispatch an update to the internal state, and return a Future where that new state can be obtained. In Java, pass in an instance of akka.dispatch.Mapper.

  3. abstract def alterOff(f: (T) ⇒ T)(implicit ec: ExecutionContext): 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.

    Dispatch a function to update the internal state but on its own thread, and return a Future where that new state can be obtained. 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. In Java, pass in an instance of akka.dispatch.Mapper.

  4. abstract 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. In Java, pass in an instance of akka.dispatch.Mapper.

  5. abstract 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. In Java, pass in an instance of akka.dispatch.Foreach.

  6. abstract def future(): Future[T]

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

  7. abstract def get(): T

    Java API: Read the internal state of the agent.

  8. abstract 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. In Java, pass in an instance of akka.dispatch.Mapper.

  9. abstract def send(f: (T) ⇒ T): Unit

    Dispatch a function to update the internal state.

    Dispatch a function to update the internal state. In Java, pass in an instance of akka.dispatch.Mapper.

  10. abstract 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).

  11. abstract def sendOff(f: (T) ⇒ T)(implicit ec: ExecutionContext): 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. In Java, pass in an instance of akka.dispatch.Mapper.

Concrete 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. def +(other: String): String

    Implicit information
    This member is added by an implicit conversion from Agent[T] to StringAdd performed by method any2stringadd in scala.Predef.
    Definition Classes
    StringAdd
  5. def ->[B](y: B): (Agent[T], B)

    Implicit information
    This member is added by an implicit conversion from Agent[T] to ArrowAssoc[Agent[T]] performed by method any2ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  6. final def ==(arg0: AnyRef): Boolean

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

    Definition Classes
    Any
  8. def apply(): T

    Read the internal state of the agent.

  9. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  10. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  11. def ensuring(cond: (Agent[T]) ⇒ Boolean, msg: ⇒ Any): Agent[T]

    Implicit information
    This member is added by an implicit conversion from Agent[T] to Ensuring[Agent[T]] performed by method any2Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  12. def ensuring(cond: (Agent[T]) ⇒ Boolean): Agent[T]

    Implicit information
    This member is added by an implicit conversion from Agent[T] to Ensuring[Agent[T]] performed by method any2Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  13. def ensuring(cond: Boolean, msg: ⇒ Any): Agent[T]

    Implicit information
    This member is added by an implicit conversion from Agent[T] to Ensuring[Agent[T]] performed by method any2Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  14. def ensuring(cond: Boolean): Agent[T]

    Implicit information
    This member is added by an implicit conversion from Agent[T] to Ensuring[Agent[T]] performed by method any2Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  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[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  18. def formatted(fmtstr: String): String

    Implicit information
    This member is added by an implicit conversion from Agent[T] to StringFormat performed by method any2stringfmt in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  19. final def getClass(): Class[_]

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

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

    Definition Classes
    Any
  22. final def ne(arg0: AnyRef): Boolean

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

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

    Definition Classes
    AnyRef
  25. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  26. def toString(): String

    Definition Classes
    AnyRef → Any
  27. final def wait(): Unit

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

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

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  30. def [B](y: B): (Agent[T], B)

    Implicit information
    This member is added by an implicit conversion from Agent[T] to ArrowAssoc[Agent[T]] performed by method any2ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc

Shadowed Implicit Value Members

  1. val self: Any

    Implicit information
    This member is added by an implicit conversion from Agent[T] to StringAdd performed by method any2stringadd in scala.Predef.
    Shadowing
    This implicitly inherited member is ambiguous. One or more implicitly inherited members have similar signatures, so calling this member may produce an ambiguous implicit conversion compiler error.
    To access this member you can use a type ascription:
    (agent: StringAdd).self
    Definition Classes
    StringAdd
  2. val self: Any

    Implicit information
    This member is added by an implicit conversion from Agent[T] to StringFormat performed by method any2stringfmt in scala.Predef.
    Shadowing
    This implicitly inherited member is ambiguous. One or more implicitly inherited members have similar signatures, so calling this member may produce an ambiguous implicit conversion compiler error.
    To access this member you can use a type ascription:
    (agent: StringFormat).self
    Definition Classes
    StringFormat

Deprecated Value Members

  1. def x: Agent[T]

    Implicit information
    This member is added by an implicit conversion from Agent[T] to ArrowAssoc[Agent[T]] performed by method any2ArrowAssoc in scala.Predef.
    Shadowing
    This implicitly inherited member is ambiguous. One or more implicitly inherited members have similar signatures, so calling this member may produce an ambiguous implicit conversion compiler error.
    To access this member you can use a type ascription:
    (agent: ArrowAssoc[Agent[T]]).x
    Definition Classes
    ArrowAssoc
    Annotations
    @deprecated
    Deprecated

    (Since version 2.10.0) Use leftOfArrow instead

  2. def x: Agent[T]

    Implicit information
    This member is added by an implicit conversion from Agent[T] to Ensuring[Agent[T]] performed by method any2Ensuring in scala.Predef.
    Shadowing
    This implicitly inherited member is ambiguous. One or more implicitly inherited members have similar signatures, so calling this member may produce an ambiguous implicit conversion compiler error.
    To access this member you can use a type ascription:
    (agent: Ensuring[Agent[T]]).x
    Definition Classes
    Ensuring
    Annotations
    @deprecated
    Deprecated

    (Since version 2.10.0) Use resultOfEnsuring instead

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion any2stringadd from Agent[T] to StringAdd

Inherited by implicit conversion any2stringfmt from Agent[T] to StringFormat

Inherited by implicit conversion any2ArrowAssoc from Agent[T] to ArrowAssoc[Agent[T]]

Inherited by implicit conversion any2Ensuring from Agent[T] to Ensuring[Agent[T]]

Ungrouped