public final class DefaultActor<T>
extends Object
implements Actor
Default implementation of Actor.
Each actor runs on a dedicated worker thread (virtual on JDK 21+) and dispatches messages sequentially from a FIFO mailbox, guaranteeing thread-safe state access without locks. The mailbox is a LinkedBlockingDeque so that ActorContext.unstashAll can re-inject deferred messages at the head of the queue; a Semaphore gates send-side capacity for bounded mailboxes (the deque itself is internally unbounded). A message's permit is released as soon as the worker takes it from the deque — so the bound applies to the queue, not to messages currently being processed or held in the stash buffer; the stash buffer itself is unbounded.
T - the message type| Type Params | Return Type | Name and description |
|---|---|---|
|
public static Actor<?> |
currentlyExecuting()Implementation hook for Actor.currentSelf. |
|
public boolean |
isActive() |
|
public boolean |
isTerminated() |
|
public Actor<T> |
onError(BiConsumer<Throwable, ? super T> handler) |
|
public Actor<T> |
onError(TriConsumer<ActorContext<T>, Throwable, ? super T> handler) |
<T, R> |
public static Actor<T> |
reactor(Function<T, R> handler, ActorOptions options)Starts a reactor whose handler is handler and whose context is ignored.
|
<T, R> |
public static Actor<T> |
reactor(ReactorHandler<T, R> handler, ActorOptions options)Starts a reactor that calls handler with the actor context and the message.
|
|
public void |
send(T message) |
<R> |
public Awaitable<R> |
sendAndGet(T message) |
<T, S> |
public static Actor<T> |
stateful(S initialState, BiFunction<S, T, S> handler, ActorOptions options)Starts a stateful actor at initialState. |
<T, S> |
public static Actor<T> |
stateful(S initialState, StatefulHandler<S, T> handler, ActorOptions options)Starts a stateful actor at initialState whose handler receives the context,
the committed state, and the message. |
|
public void |
stop() |
|
public String |
toString() |
Implementation hook for Actor.currentSelf.
Starts a reactor whose handler is handler and whose context is ignored.
handler and options must be non-null. The value returned by
handler is bound as the reply of Actor.sendAndGet.
Construction submits the worker to options' executor, or to
AsyncSupport.getExecutor when that executor is null.
T - the message typeR - the type handler returns and sendAndGet bindshandler - applied to each messageoptions - mailbox, executor, and failure options Starts a reactor that calls handler with the actor context and the message.
Both arguments must be non-null. The handler's return value is bound as the reply
of Actor.sendAndGet. The worker is submitted as
reactor(Function, ActorOptions) submits it.
T - the message typeR - the type handler returns and sendAndGet bindshandler - applied to the context and each messageoptions - mailbox, executor, and failure options Starts a stateful actor at initialState. initialState may be null.
handler and options must be non-null. The handler is called with
the committed state and the message; the context is not passed to it. The value
it returns is committed as the next state and bound as the reply of
Actor.sendAndGet only after a successful dispatch. A stash or a
thrown exception discards that pending state. The worker is submitted as
reactor(Function, ActorOptions) submits it.
T - the message typeS - the state typeinitialState - state seen by the first handler call; may be nullhandler - computes the next state from the current state and the messageoptions - mailbox, executor, and failure options Starts a stateful actor at initialState whose handler receives the context,
the committed state, and the message. initialState may be null.
handler and options must be non-null. The returned state is
committed and bound as the Actor.sendAndGet reply only after a
successful dispatch; a stash or a thrown exception discards it. The worker is
submitted as reactor(Function, ActorOptions) submits it.
T - the message typeS - the state typeinitialState - state seen by the first handler call; may be nullhandler - computes the next state from the context, current state, and messageoptions - mailbox, executor, and failure options