I have an actor with the next behaviour. When i stash commands, they never
go back to mailbox.
Use versions *"akka-actor-typed"* and *"akka-persistence-typed"* 2.5.16
object Blog {
//#event
sealed trait BlogEvent
extends Serializable
final case class PostAdded(postId: String, content: PostContent)
extends BlogEvent
//#event
//#state
object BlogState {
val empty = BlogState(None)
}
final case class BlogState(content: Option[PostContent]) {
def withContent(newContent: PostContent): BlogState =
copy(content = Some(newContent))
def isEmpty: Boolean = content.isEmpty
def postId: String = content match {
case Some(c) ⇒ c.postId
case None ⇒ throw new IllegalStateException("postId unknown
before post is created")
}
}
//#state
//#commands
sealed trait BlogCommand
extends Serializable
final case class AddPost(content: PostContent, replyTo: ActorRef[
AddPostDone]) extends BlogCommand
final case class Accumulate(num: Int)
extends BlogCommand
final case class AddPostDone(postId: String)
final case class PostContent(postId: String, title: String, body:
String)
//#commands
def behavior: Behavior[BlogCommand] =
Behaviors.setup[BlogCommand] { ctx ⇒
val buffer = StashBuffer[BlogCommand](capacity = 1000)
//#initial-command-handler
val initial
: (ActorContext[BlogCommand], BlogState, BlogCommand) ⇒ Effect[
BlogEvent, BlogState] =
(ctx, state, cmd) ⇒
cmd match {
case AddPost(content, replyTo) ⇒
Effect
.persist(PostAdded(content.postId, content))
.thenRun { state2 ⇒
replyTo ! AddPostDone(content.postId)
buffer.unstashAll(ctx, this.behavior)
}
case a @ Accumulate(_) =>
buffer.stash(a)
Effect.none
case _ ⇒
Effect.unhandled
}
//#initial-command-handler
//#post-added-command-handler
val postAdded
: (ActorContext[BlogCommand], BlogState, BlogCommand) ⇒ Effect[
BlogEvent, BlogState] = {
(ctx, state, cmd) ⇒
cmd match {
case _: AddPost ⇒
Effect.unhandled
case Accumulate(num) ⇒
println(s"********Accumulate: $num**********")
Effect.none
case c ⇒
Effect.unhandled
}
}
//#post-added-command-handler
//#by-state-command-handler
val commandHandler
: (ActorContext[BlogCommand], BlogState, BlogCommand) ⇒ Effect[
BlogEvent, BlogState] =
CommandHandler.byState {
case state if state.isEmpty ⇒
initial
case state if !state.isEmpty ⇒
postAdded
}
//#by-state-command-handler
//#event-handler
val eventHandler: (BlogState, BlogEvent) ⇒ BlogState = { (state,
event) ⇒
event match {
case PostAdded(postId, content) ⇒
state.withContent(content)
}
}
//#event-handler
//#behavior
PersistentBehaviors.receive[BlogCommand, BlogEvent,
BlogState](persistenceId
= "Blog",
emptyState = BlogState.empty,
commandHandler,
eventHandler)
//#behavior
}
}
I try:
val blog = system.spawn(Blog.behavior, "Typed")
blog ! Accumulate(1)
blog ! Accumulate(2)
blog ! Accumulate(3)
blog ! AddPost(PostContent("id1", "title1", "body"), typedSystem.
deadLetters)
blog ! Accumulate(4)
The commands *Accumulate(1), Accumulate(2), Accumulate(3)* were stashed in*
initial-command-handler* but when do *buffer.unstashAll(ctx, this.behavior)*
the commands are lost, they do not go back to mailbox
--
*****************************************************************************************************
** New discussion forum: https://discuss.akka.io/ replacing akka-user
google-group soon.
** This group will soon be put into read-only mode, and replaced by
discuss.akka.io
** More details: https://akka.io/blog/news/2018/03/13/discuss.akka.io-announced
*****************************************************************************************************
>>>>>>>>>>
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.