Hi
Thanks a lot for the reply. I got to start working on a custom made
solution according to the line of thought you had provided. But according
to the suggestion *I couldn't get a hold of the actorref that was failing
in the decider function of the onetoonestrategy to include book keeping*.
I have included one way of book keeping by defining a *new strategy which
extends a normal supervisor strategy* class. *The default strategy for this
custom strategy class* is defined by a oneforonestrategy with 40 retrials
and a decider which when sent in a null pointer exception just tries to
restart. *The handle failure of the custom strategy class is also overriden*
in order to keep count of the numver of times the current child actor has
failed. If the number exceeds 5 , a error sent to an error queue and the
poststop method of the child actor class is called so that it kills the
child class.
Please suggest if I am missing something or if this is a work around and if
there is a simpler way to acheive the same.
Thanks,
Swathi
*Custom Supervisor Strategy* class defined to extend a normal
supervisorstrategy with overriden default strategy and handlefailure
methods to stop resending a message after five trials.
class CustomSupervisorStrategy extends SupervisorStrategy{
public static final SupervisorStrategy defaultStrategy() {
return new OneForOneStrategy(40,Duration.create("1 minute"),new
Function<Throwable, Directive>() {
Directive apply(Throwable t) {
if (t instanceof ArithmeticException) {
return resume();
} else if (t instanceof NullPointerException) {
println "this is null pointer exception 1"
return restart()
} else if (t instanceof IllegalArgumentException) {
return stop();
} else if(t instanceof ActorKilledException){
println t
return restart()
} else {
return escalate()
}
}
});
}
@Override
PartialFunction<Throwable, Directive> decider() {
return defaultDecider()
}
@Override
void handleChildTerminated(ActorContext actorContext, ActorRef
actorRef, scala.collection.Iterable<ActorRef> actorRefIterable) {
println "comes to child terminated"
defaultStrategy().handleChildTerminated(actorContext, actorRef,
actorRefIterable)
}
@Override
void processFailure(ActorContext actorContext, boolean b, ActorRef
actorRef, Throwable throwable, ChildRestartStats childRestartStats,
scala.collection.Iterable<ChildRestartStats> childRestartStatsIterable) {
defaultStrategy().processFailure(actorContext,b,actorRef,throwable,childRestartStats,childRestartStatsIterable)
}
@Override
public boolean handleFailure(ActorContext context,
ActorRef child,
java.lang.Throwable cause,
ChildRestartStats stats,
scala.collection.Iterable<ChildRestartStats> children){
println "reached the handle failure"
println stats
println children
if(stats.maxNrOfRetriesCount() < 5) {
defaultStrategy().handleFailure(context,child,cause,stats,children)
} else {
// TODO : define a service which can post this message to a jms error
queue.
child.postStop()
}
}
}
*RetrySupervisor class* - a supervisor class which is used to instantiate
the child class and define the strategy that will used to handle the
exceptions that occur in the child class
class RetrySupervisor extends UntypedActor{
private static CustomSupervisorStrategy strategy = new
CustomSupervisorStrategy()
@Override
public CustomSupervisorStrategy supervisorStrategy() {
return strategy;
}
public void onReceive(Object o) {
if (o instanceof Props) {
getSender().tell(getContext().actorOf((Props) o), getSelf());
} else if(o instanceof Terminated){
println "supervisor got this Terminated" + o
unhandled(o);
} else {
println "Supervisor got a random message"
}
}
}
*Retry Child class:* It is anormal class which throws an error if the sent
message is 42.
class RetryChild extends UntypedActor{
int state = 1;
public void onReceive(Object o) throws Exception {
if (o instanceof Exception) {
throw (Exception) o;
} else if (o instanceof Terminated) {
println "terminated"
} else if (o instanceof Integer) {
println "the current message " + o
if(o == 42) {
throw new NullPointerException()
}
} else if (o.equals("get")) {
println "in get function " + o
getSender().tell(state , getSelf());
} else {
println "In unhandled " + o
unhandled(o);
}
}
@Override
public void preRestart(Throwable reason, Option<Object> message ) {
getContext().self().tell(message.get(),ActorRef.noSender())
}
@Override
public void postStop() {
println "post stop simple"
}
}
*The funciton call to send a message for a failure scenario*:
Duration timeout = Duration.create(50,TimeUnit.SECONDS)
def system = ActorSystem.create("system")
Props superProps = Props.create(RetrySupervisor.class)
ActorRef retrySupervisor = system.actorOf(superProps, "retrySupervisor")
ActorRef retryChild = (ActorRef) Await.result(ask(retrySupervisor,
Props.create(RetryChild.class), 50000),timeout);
retryChild.tell(42 , ActorRef.noSender())
--
>>>>>>>>>> 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.