Re: [akka-user] Message processed by Actors
Hi, I will make some tests with Stash trait, it can be a good solution in my use case. Thanking for sharing your idea :) Le lundi 12 février 2018 19:13:53 UTC+1, Justin du coeur a écrit : > > On Mon, Feb 12, 2018 at 11:26 AM, Kilic Ali-Firat > wrote: > >> Hi Akka team, >> > > I'm not the Akka team, but... > > >> case class Worker() extends Actor { >> >> >> trait Message >> case class M1(x : Int) extends Message >> case class M2(x : Int) extends Message >> >> >> trait Result >> case Sucess(m : Message) extends Result >> case Failed(m : Message) extends Result >> >> >> def processM1(m : M1) : Future[Result] >> >> >> def processM2(m : M2) : Future[Result] >> >> >> override def receive = { >> case m : M1 => this.processM1(m) pipeTo sender() >> case m : M2 => this.processM2(m) pipeTo sender() >> } >> } >> >> >> >> >> Imagine that mailbox of a worker *has 4 messages : M1, M2, M3, M4.* >> >> It will process the mailbox messages per message starting by M1, then M2, >> then M3 and finally M4. >> >> My question about order processing is : Do an Actor wait the completion >> of current message before passing to the next one in the mailbox ? >> > > Depends on your definitions. Keep in mind that Akka is focused on > *synchronous* processing -- if the message processing is synchronous, then > yes, a given Actor will only process one at a time. > > The problem is, you're spawning off separate threads with those Futures. > Akka has no insight or control over those, so it doesn't and can't wait for > them to be finished. That's why you are seeing the behavior that you are. > > >> If I'm a wrong, Does Akka have a mechanism to wait for the completion of >> current message processing before passing to the next one ? >> > > I don't know of any built-in way to do this. The pattern I usually see > (and have sometimes used myself) is to: > > * Mix the Stash trait in; > * When I start processing, become() a different state that stash()es all > messages except the Result; > * When processing finishes (once you've dealt with the Result), > unstashAll() and unbecome() back to the main state. > > So basically, it isn't "waiting", it's storing all of the received > messages off to the side in the interim, and them putting them back into > the mailbox when you're ready to continue. You get an effect similar to > waiting, but without blocking anything. > -- >> 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 akka-user+unsubscr...@googlegroups.com. To post to this group, send email to akka-user@googlegroups.com. Visit this group at https://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.
Re: [akka-user] Message processed by Actors
On Mon, Feb 12, 2018 at 11:26 AM, Kilic Ali-Firat wrote: > Hi Akka team, > I'm not the Akka team, but... > case class Worker() extends Actor { > > > trait Message > case class M1(x : Int) extends Message > case class M2(x : Int) extends Message > > > trait Result > case Sucess(m : Message) extends Result > case Failed(m : Message) extends Result > > > def processM1(m : M1) : Future[Result] > > > def processM2(m : M2) : Future[Result] > > > override def receive = { > case m : M1 => this.processM1(m) pipeTo sender() > case m : M2 => this.processM2(m) pipeTo sender() > } > } > > > > > Imagine that mailbox of a worker *has 4 messages : M1, M2, M3, M4.* > > It will process the mailbox messages per message starting by M1, then M2, > then M3 and finally M4. > > My question about order processing is : Do an Actor wait the completion of > current message before passing to the next one in the mailbox ? > Depends on your definitions. Keep in mind that Akka is focused on *synchronous* processing -- if the message processing is synchronous, then yes, a given Actor will only process one at a time. The problem is, you're spawning off separate threads with those Futures. Akka has no insight or control over those, so it doesn't and can't wait for them to be finished. That's why you are seeing the behavior that you are. > If I'm a wrong, Does Akka have a mechanism to wait for the completion of > current message processing before passing to the next one ? > I don't know of any built-in way to do this. The pattern I usually see (and have sometimes used myself) is to: * Mix the Stash trait in; * When I start processing, become() a different state that stash()es all messages except the Result; * When processing finishes (once you've dealt with the Result), unstashAll() and unbecome() back to the main state. So basically, it isn't "waiting", it's storing all of the received messages off to the side in the interim, and them putting them back into the mailbox when you're ready to continue. You get an effect similar to waiting, but without blocking anything. -- >> 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 akka-user+unsubscr...@googlegroups.com. To post to this group, send email to akka-user@googlegroups.com. Visit this group at https://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.
[akka-user] Message processed by Actors
Hi Akka team, I have a question about messages processing by an Actor. In the context of my Akka application, I defined an Actor with a similar behavior : case class Worker() extends Actor { trait Message case class M1(x : Int) extends Message case class M2(x : Int) extends Message trait Result case Sucess(m : Message) extends Result case Failed(m : Message) extends Result def processM1(m : M1) : Future[Result] def processM2(m : M2) : Future[Result] override def receive = { case m : M1 => this.processM1(m) pipeTo sender() case m : M2 => this.processM2(m) pipeTo sender() } } Imagine that mailbox of a worker *has 4 messages : M1, M2, M3, M4.* It will process the mailbox messages per message starting by M1, then M2, then M3 and finally M4. My question about order processing is : Do an Actor wait the completion of current message before passing to the next one in the mailbox ? Based on Akka mechanism, the answer seems to be no. If the actor system dispatcher has enough threads, it can process M1, M2, M3 and M4 in parallel but with respecting order of processing. Did I am wrong ? If I'm a wrong, Does Akka have a mechanism to wait for the completion of current message processing before passing to the next one ? In my application, worker actor defined in my question is a member of a ClusterGroup and I'm pushing messages to it so I was wondering if you have any advices to reach a such behaviour ? Alifirat Kilic. -- >> 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 akka-user+unsubscr...@googlegroups.com. To post to this group, send email to akka-user@googlegroups.com. Visit this group at https://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.