Thanks for your answer Endre,

My purpose is to try sending the received command to some external resource 
(e.g. mq) asynchronously and persist the command only in case of an 
exception with the sending so that it could be replayed (resent) at a later 
time - that's why the persist done inside the recover. 

Using the pipe pattern sounds like a perfect solution.

Joel 

On Wednesday, May 6, 2015 at 7:30:37 PM UTC+3, Akka Team wrote:
>
> Hi Joel,
>
>
> On Wed, May 6, 2015 at 6:22 PM, Joel <[email protected] <javascript:>> 
> wrote:
>
>> Hi,
>>
>> Calling the persist or persistAsync from a future created during 
>> receiveCommand does not call the preceding handler and doesn't produce an 
>> error event neither.
>>
>> class PublisherActor extends PersistentActor {
>>   def receiveCommand: Receive = {
>>     case cmd: MyCmd => {
>>       println("cmd received!")
>>       send(cmd) recover { // send is an async operation returning a future
>>         case t => {
>>           println("persisting...")
>>           persistAsync(cmd)(c => println("persisted!!!")
>>         }
>>       }
>>     }
>>     case _ => println("received other cmd!")
>>   }
>> }
>>
>> running the code above (when the send future throws an exception) prints:
>> cmd received!
>> persisting...
>>
>> but "persisted!!!" is not printed - moving the persistAsync line of code 
>> outside the future call (before 'send') works fine, any ideas why calling 
>> inside the future does not work for me?
>>
>
> You are closing over actor state in a Future callback. This is an absolute 
> no-go, you should never do that. The problem is that the future callback 
> will run on a different thread, accessing the actor state unsafely, and 
> concurrently. Both of these violate the basic integrity of the actor 
> resulting in undefined behavior.
>
> If you want to act on a result of a Future inside an actor, you should use 
> the pipe pattern.
>
> Also, recover is for recovering from an exception (see the signature: 
> recover[U >: T](pf: PartialFunction[Throwable, U])), your recover action 
> will not run if the future finished successfully.
>
> -Endre
>  
>
>>
>> Thanks,
>> Joel
>>
>> -- 
>> >>>>>>>>>> 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] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Akka Team
> Typesafe - Reactive apps on the JVM
> Blog: letitcrash.com
> Twitter: @akkateam
>  

-- 
>>>>>>>>>>      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.

Reply via email to