Scenario:
How do I alert the sender , after 3 retries are done by back off supervisor ? The parent can be any actor calling the trait(that has backoff supervisor).I have a trait backoffsupervisor which has def to call the supervisor . When there is an exception thrown from an actor class, it calls this trait def(does 3 retries with supervisor strategy). Now what I would like is to respond to the calling actor after 3 retries are done( I am unable to use the sender() option). Once the actor receives that 3 retries are done, it will log the error info into mongo db.
trait backoff{
def getbackoff(props:Props):Props{
BackoffSupervisor.props(
BackoffOpts.onFailure(
props,
props.getClass.getSimpleName,
1.seconds,
1.seconds,
0
)
.withSupervisorStrategy(OneForOneStrategy(maxNrOfRetries = 3){
case _ :Exception=> //println(s"inside back off and exception is ${this.getClass.getName}")
SupervisorStrategy.restart
})
)
}
}
//com.scala.callingActor
import com.scala.AnotherActor.{Result,fail}
object CallingActor extends backoff{
def props={val actorProps=Props(classOf[CallingActor])
getbackoff(actorprops)}
}
class CallingActor extends Actor{
override def recieve:Recieve={
case Result=>context.parent ! fail
throws Exception("somrthing happened")}
}
Options I tried : saved the actorref in a val and sent it to back off supervisor. Editted code below:
- //custom exception to send actor ref
- case class MyException(msg:String,ref:ActorRef) extends Exception
- trait backoff{
- def getbackoff(props:Props):Props{
- BackoffSupervisor.props(
- BackoffOpts.onFailure(
- props,
- props.getClass.getSimpleName,
- 1.seconds,
- 1.seconds,
- 0
- )
- .withSupervisorStrategy(OneForOneStrategy(maxNrOfRetries = 3){
- case e :MyException=> println(s"inside back off and exception is ${this.getClass.getName}")
- e.ref !“retriesDone”
- SupervisorStrategy.restart
- })
- )
- }
- }
- import com.scala.AnotherActor.{Result,fail}
- object CallingActor extends backoff{
- def props={val actorProps=Props(classOf[CallingActor])
- getbackoff(actorprops)}
- }
- class CallingActor extends Actor{
- override def recieve:Recieve={
- case Result=>context.parent ! fail
- throw MyException(“somrthing happened”,context.self)
- case message:String=>println(s"message= ${message}")
- }
- }
The message is going to dead letters and not passed to the sender. I also tried sending the message to parent of the sender by passing parent actor ref, but that didn’t work either.Any other suggestion? to resolve this issue