I sometimes call recover on the returned Future to generate a message of my own, before doing the pipeTo.
On Wed, Aug 5, 2015 at 6:30 AM, Johan Andrén <[email protected]> wrote: > pipeTo will wrap failures in an akka.actor.Status.Failure and send that > to the actor that you direct the result to (self in this case). > > *Important note*: There is an error in your code where you access sender() > outside of the actor execution flow. For-comprehensions desugar to nested > calls of flatMap and map, which means that the expression to the right in t > <- Future { sender } is actually executed later, possibly on another > thread and at a time when sender()returns a different sender because the > actor is processing another message. You can find some more examples of > this anti pattern here: > http://doc.akka.io/docs/akka/snapshot/general/jmm.html#Actors_and_shared_mutable_state > > The safe way to do it would be something like: > > val from = sender() > > val selfMessage: Future[PutDocument] = > > for { > > r <- { > > val indexer = client execute { > > index into es_index_id id raw_id fields( > > "version_vector" -> List() > > ) opType CREATE > > } > > indexer > > } > > } yield {PutDocument(r, from)} > > > selfMessage pipeTo self > > -- > Johan Andrén > Typesafe - Reactive apps on the JVM > Twitter: @apnylle > > On Thursday, July 16, 2015 at 2:48:21 AM UTC+2, [email protected] > wrote: >> >> I'm using the following code: >> >> val selfMessage: Future[PutDocument] = >> for { >> r <- { >> val indexer = client execute { >> index into es_index_id id raw_id fields( >> "version_vector" -> List() >> ) opType CREATE >> } >> indexer >> } >> >> t <- Future{sender} >> } yield {PutDocument(r, t)} >> >> selfMessage pipeTo self >> >> >> The purpose of this block is essentially to attempt to put a document >> with a particular id into elastic search. In the case a document with the >> same id exists, the client execute block returns a >> RemoteTransportException. This doesn't satisfy the PutDocument case class >> which consists of an IndexResponse and an ActorRef. I'm unsure about how to >> catch this failure and pipe an error message to self instead. >> >> Thanks! >> >> . >> > -- > >>>>>>>>>> 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 http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/d/optout. > -- >>>>>>>>>> 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 http://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.
