public abstract class AbstractActorWithStash extends AbstractActor implements Stash
Actor base class that should be extended to create an actor with a stash.
The stash enables an actor to temporarily stash away messages that can not or should not be handled using the actor's current behavior.
Example:public class MyActorWithStash extends AbstractActorWithStash { int count = 0; public MyActorWithStash() { receive(ReceiveBuilder. match(String.class, s -> { if (count < 0) { sender().tell(new Integer(s.length()), self()); } else if (count == 2) { count = -1; unstashAll(); } else { count += 1; stash(); }}).build() ); } }Note that the subclasses of
AbstractActorWithStash
by default request a Deque based mailbox since this class
implements the RequiresMessageQueue
marker interface.
You can override the default mailbox provided when DequeBasedMessageQueueSemantics
are requested via config:
akka.actor.mailbox.requirements { "akka.dispatch.BoundedDequeBasedMessageQueueSemantics" = your-custom-mailbox }Alternatively, you can add your own requirement marker to the actor and configure a mailbox type to be used for your marker.
For a Stash
based actor that enforces unbounded deques see AbstractActorWithUnboundedStash
.
There is also an unrestricted version AbstractActorWithUnrestrictedStash
that does not
enforce the mailbox type.
This is an EXPERIMENTAL feature and is subject to change until it has received more real world testing.
Actor.emptyBehavior$
Constructor and Description |
---|
AbstractActorWithStash() |
Modifier and Type | Method and Description |
---|---|
akka.actor.ActorCell |
actorCell() |
int |
capacity() |
scala.collection.immutable.Vector<Envelope> |
clearStash()
INTERNAL API.
|
ActorContext |
context()
INTERNAL API.
|
void |
enqueueFirst(Envelope envelope)
Enqueues
envelope at the first position in the mailbox. |
DequeBasedMessageQueueSemantics |
mailbox()
INTERNAL API.
|
void |
prepend(scala.collection.immutable.Seq<Envelope> others)
Prepends
others to this stash. |
ActorRef |
self()
INTERNAL API.
|
void |
stash()
Adds the current message (the message that the actor received last) to the
actor's stash.
|
scala.collection.immutable.Vector<Envelope> |
theStash() |
void |
unstash()
Prepends the oldest message in the stash to the mailbox, and then removes that
message from the stash.
|
void |
unstashAll()
Prepends all messages in the stash to the mailbox, and then clears the stash.
|
void |
unstashAll(scala.Function1<java.lang.Object,java.lang.Object> filterPredicate)
INTERNAL API.
|
emptyBehavior, getContext, receive, receive
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
postStop, preRestart
aroundPostRestart, aroundPostStop, aroundPreRestart, aroundPreStart, aroundReceive, context, noSender, postRestart, preStart, receive, self, sender, supervisorStrategy, unhandled
public ActorContext context()
Context of the actor that uses this stash.
public ActorRef self()
Self reference of the actor that uses this stash.
public scala.collection.immutable.Vector<Envelope> theStash()
public akka.actor.ActorCell actorCell()
public int capacity()
public DequeBasedMessageQueueSemantics mailbox()
The actor's deque-based message queue.
mailbox.queue
is the underlying Deque
.
public void stash()
StashOverflowException
- in case of a stash capacity violationjava.lang.IllegalStateException
- if the same message is stashed more than oncepublic void prepend(scala.collection.immutable.Seq<Envelope> others)
others
to this stash. This method is optimized for a large stash and
small others
.public void unstash()
Messages from the stash are enqueued to the mailbox until the capacity of the
mailbox (if any) has been reached. In case a bounded mailbox overflows, a
MessageQueueAppendFailedException
is thrown.
The unstashed message is guaranteed to be removed from the stash regardless
if the unstash()
call successfully returns or throws an exception.
public void unstashAll()
Messages from the stash are enqueued to the mailbox until the capacity of the
mailbox (if any) has been reached. In case a bounded mailbox overflows, a
MessageQueueAppendFailedException
is thrown.
The stash is guaranteed to be empty after calling unstashAll()
.
public void unstashAll(scala.Function1<java.lang.Object,java.lang.Object> filterPredicate)
Prepends selected messages in the stash, applying filterPredicate
, to the
mailbox, and then clears the stash.
Messages from the stash are enqueued to the mailbox until the capacity of the
mailbox (if any) has been reached. In case a bounded mailbox overflows, a
MessageQueueAppendFailedException
is thrown.
The stash is guaranteed to be empty after calling unstashAll(Any => Boolean)
.
filterPredicate
- only stashed messages selected by this predicate are
prepended to the mailbox.public scala.collection.immutable.Vector<Envelope> clearStash()
Clears the stash and and returns all envelopes that have not been unstashed.
public void enqueueFirst(Envelope envelope)
envelope
at the first position in the mailbox. If the message contained in
the envelope is a Terminated
message, it will be ensured that it can be re-received
by the actor.