2017-02-08 5 views
1

향후 호출에서 뭔가를 실행 중입니다. 성공적인 통화가 끝나면 결과를 발신자에게 돌려 주거나 향후 통화에 실패하면 배우를 실패합니다. 감독자 전략이있는 RoundRobinPool이 구현 된 부모에 의해 처리됩니다.미래 호출이 실패 할 경우 액터에 실패합니다.

다음은 코드 단편입니다.

private def getData(sender: ActorRef): Unit = { 

    dao.getData().mapTo[List[Data]].map(result => sender ! result) 
     .onFailure { 
     case e: NullPointerExcetpion => { 
      println("+++++++ Throwing exception") 
     // throwning the exception from here doesn't cause the supervisor to restart this actor 
      throw t 
     } 
     } 

     // throwing the exception from here makes the supervisor strategy to take action 
     throw new NullPointerExcetpion 

    } 

미래가 예외를 반환하면 어떻게 할 수 있습니까?

건배

Utsav

+0

당신은 "액터 실패"무엇을 의미합니까? –

+0

다시 사용자에게 전파하고 배우를 죽입니다. 감독자가 액터를 다시 시작하게하는 동작입니다. null 포인터 예외의 경우 SupervisorStrategy.Restart가 감독자로 사용됩니다. – Utsav

+0

더 중요한 것은 Future 함수 밖에서 명시 적으로 오류를 던질 때 왜 작동하는 것입니까? – Utsav

답변

1

문제는 onFailure 콜백 액터가 실행되는 한, 임의의 스레드에서 발생하고 있지 않은 점이다. 당신이 할 수있는 파이프는 자신에게 결과 다음 던져 :

case class MyFailure(e: Throwable) 

def receive: { 
    case MyFailure(e) => throw e 
} 

private def getData(sender: ActorRef): Unit = { 
    dao 
    .getData() 
    .mapTo[List[Data]] 
    .recover { 
    case e => MyFailure(e) 
    } 
    .pipeTo(self) 
} 

또는 @jrudolph로

제안 :

def receive: { 
    case Status.Failure(e) => throw e 
} 

private def getData(sender: ActorRef): Unit = { 
    dao 
    .getData() 
    .mapTo[List[Data]] 
    .pipeTo(self) 
} 
+1

또는'pipeTo'에 의해 제공되는'Status.Failure (ex)'메시지를 받아서'recover'도하지 않아도됩니다. – jrudolph

+0

@jrudolph 추가되었습니다. –

+0

실제 미래의 통화에서 계속지도를 유지할 수 있습니까? – Utsav

관련 문제