Thank you Patrik,

  The reason I put Thread.sleep() here is to simulate the real use case in 
which our KidActor needs to call a third party API and throw an Exception 
if something goes wrong(like unauthorized token). The API calling might 
take long time before the exception happen. 

  Could you please advise me what's the best practice in this case if I 
want ParentActor make decision according to what exceptions thrown from 
KidActor?

Best,
Yan



On Tuesday, May 10, 2016 at 2:38:03 PM UTC-5, Patrik Nordwall wrote:
>
> Isn't that because you have not thrown the exception until after the sleep?
>
> In general, don't block actor execution since it will not be able to react 
> on any messages while it's blocked. Supervision is also based on messages.
>
> /Patrik
>
> On Tue, May 10, 2016 at 9:20 PM, Yan Pei <yan...@gmail.com <javascript:>> 
> wrote:
>
>> Below is a simple AKKA SupervisorStrategy example:
>>
>> The SupervisorStrategy catchs the exception thrown from ChildActor with 
>> short time sleep in ChildActor.java, but it will not catch it if the 
>> Thread.sleep() is too long like below. The message will not be delivered to 
>> ChildActor for the long time sleeping.
>>
>> public class ParentActor extends UntypedActor {
>> private ActorRef r;
>> private static SupervisorStrategy strategy = new OneForOneStrategy(10,
>> Duration.create("10 second"), new Function<Throwable, Directive>() {
>> @Override
>> public Directive apply(final Throwable t) {
>> if (t instanceof NullPointerException) {
>> return restart();
>> } else if (t instanceof IllegalArgumentException) {
>> return escalate();
>> } else {
>> return stop();
>> }
>> }
>> });
>>
>> @Override
>> public SupervisorStrategy supervisorStrategy() {
>> return strategy;
>> }
>>
>> public ParentActor() {
>> r = 
>> getContext().actorOf(FromConfig.getInstance().props(Props.create(ChildActor.class).withRouter(new
>>  
>> RoundRobinPool(1))), "child-actor");
>> }
>>
>> @Override
>> public void onReceive(final Object msg) throws Exception {
>> if (msg instanceof String) {
>> r.tell(msg, getSender());
>> }{
>> unhandled(msg);
>> }
>>
>> }
>>
>> }
>>
>> public class ChildActor extends UntypedActor {
>>
>> @Override
>> public void onReceive(final Object msg)  {
>> if(msg instanceof String) {
>> try{
>> Thread.sleep(100000l);
>> }catch(Exception e) {}
>> throw new NullPointerException("testing");
>> }
>> }
>> }
>>
>>
>>
>>
>>
>> -- 
>> >>>>>>>>>> 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+...@googlegroups.com <javascript:>.
>> To post to this group, send email to akka...@googlegroups.com 
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
>
> Patrik Nordwall
> Akka Tech Lead
> Lightbend <http://www.lightbend.com/> -  Reactive apps on the JVM
> Twitter: @patriknw
>
>

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

Reply via email to