change the type of the handler function, and go down the rabbit hole a bit. Add a Procedure2[T1,T2] to the Java API, and add JavaEventHandler that gives access from java to the EventHandler, add docs for configuring the handler in declarative Supervision for Scala and Java.
This commit is contained in:
parent
41ef2843b1
commit
b96eca5868
6 changed files with 100 additions and 6 deletions
35
akka-actor/src/main/java/akka/event/JavaEventHandler.java
Normal file
35
akka-actor/src/main/java/akka/event/JavaEventHandler.java
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package akka.event;
|
||||
|
||||
|
||||
import akka.actor.ActorRef;
|
||||
|
||||
/**
|
||||
* Java API for Akka EventHandler
|
||||
*/
|
||||
|
||||
public class JavaEventHandler {
|
||||
|
||||
|
||||
public static void notify(Object message){
|
||||
EventHandler$.MODULE$.notify(message);
|
||||
}
|
||||
|
||||
public static void debug(ActorRef instance, Object message){
|
||||
EventHandler$.MODULE$.debug(instance, message);
|
||||
}
|
||||
|
||||
public static void info(ActorRef instance, Object message){
|
||||
EventHandler$.MODULE$.info(instance,message);
|
||||
}
|
||||
|
||||
public static void warning(ActorRef instance, Object message){
|
||||
EventHandler$.MODULE$.warning(instance,message);
|
||||
}
|
||||
|
||||
public static void error(ActorRef instance, Object message){
|
||||
EventHandler$.MODULE$.debug(instance,message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -99,7 +99,7 @@ case class SupervisorFactory(val config: SupervisorConfig) {
|
|||
*
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
sealed class Supervisor(handler: FaultHandlingStrategy, maxRestartsHandler: MaximumNumberOfRestartsWithinTimeRangeReached => Unit) {
|
||||
sealed class Supervisor(handler: FaultHandlingStrategy, maxRestartsHandler: (ActorRef, MaximumNumberOfRestartsWithinTimeRangeReached) => Unit) {
|
||||
import Supervisor._
|
||||
|
||||
private val _childActors = new ConcurrentHashMap[String, List[ActorRef]]
|
||||
|
|
@ -156,7 +156,7 @@ sealed class Supervisor(handler: FaultHandlingStrategy, maxRestartsHandler: Maxi
|
|||
*
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
final class SupervisorActor private[akka] (handler: FaultHandlingStrategy, maxRestartsHandler: MaximumNumberOfRestartsWithinTimeRangeReached => Unit) extends Actor {
|
||||
final class SupervisorActor private[akka] (handler: FaultHandlingStrategy, maxRestartsHandler: (ActorRef,MaximumNumberOfRestartsWithinTimeRangeReached) => Unit) extends Actor {
|
||||
self.faultHandler = handler
|
||||
|
||||
|
||||
|
|
@ -170,7 +170,7 @@ final class SupervisorActor private[akka] (handler: FaultHandlingStrategy, maxRe
|
|||
}
|
||||
|
||||
def receive = {
|
||||
case max@MaximumNumberOfRestartsWithinTimeRangeReached(_,_,_,_) => maxRestartsHandler(max)
|
||||
case max@MaximumNumberOfRestartsWithinTimeRangeReached(_,_,_,_) => maxRestartsHandler(self, max)
|
||||
case unknown => throw new SupervisorException(
|
||||
"SupervisorActor can not respond to messages.\n\tUnknown message [" + unknown + "]")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ package akka.config
|
|||
|
||||
import akka.dispatch.MessageDispatcher
|
||||
import akka.actor.{MaximumNumberOfRestartsWithinTimeRangeReached, ActorRef}
|
||||
import akka.japi.Procedure
|
||||
import akka.japi.{Procedure2, Procedure}
|
||||
|
||||
case class RemoteAddress(val hostname: String, val port: Int)
|
||||
|
||||
|
|
@ -22,10 +22,10 @@ object Supervision {
|
|||
sealed abstract class LifeCycle extends ConfigElement
|
||||
sealed abstract class FaultHandlingStrategy(val trapExit: List[Class[_ <: Throwable]]) extends ConfigElement
|
||||
|
||||
case class SupervisorConfig(restartStrategy: FaultHandlingStrategy, worker: List[Server], maxRestartsHandler: MaximumNumberOfRestartsWithinTimeRangeReached => Unit = {max=>()}) extends Server {
|
||||
case class SupervisorConfig(restartStrategy: FaultHandlingStrategy, worker: List[Server], maxRestartsHandler: (ActorRef,MaximumNumberOfRestartsWithinTimeRangeReached)=> Unit = {(aRef,max)=>()}) extends Server {
|
||||
//Java API
|
||||
def this(restartStrategy: FaultHandlingStrategy, worker: Array[Server]) = this(restartStrategy,worker.toList)
|
||||
def this(restartStrategy: FaultHandlingStrategy, worker: Array[Server], restartHandler:Procedure[MaximumNumberOfRestartsWithinTimeRangeReached]) = this(restartStrategy,worker.toList, {max=>restartHandler.apply(max)})
|
||||
def this(restartStrategy: FaultHandlingStrategy, worker: Array[Server], restartHandler:Procedure2[ActorRef,MaximumNumberOfRestartsWithinTimeRangeReached]) = this(restartStrategy,worker.toList, {(aRef,max)=>restartHandler.apply(aRef,max)})
|
||||
}
|
||||
|
||||
class Supervise(val actorRef: ActorRef, val lifeCycle: LifeCycle, val registerAsRemoteService: Boolean = false) extends Server {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,12 @@ trait Procedure[T] {
|
|||
def apply(param: T): Unit
|
||||
}
|
||||
|
||||
/** A Procedure is like a Function, but it doesn't produce a return value
|
||||
*/
|
||||
trait Procedure2[T1,T2] {
|
||||
def apply(param: T1, param2:T2): Unit
|
||||
}
|
||||
|
||||
/**
|
||||
* An executable piece of code that takes no parameters and doesn't return any value.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -125,6 +125,36 @@ The Actor’s supervision can be declaratively defined by creating a ‘Supervis
|
|||
|
||||
Supervisors created like this are implicitly instantiated and started.
|
||||
|
||||
To cofigure a handler function for when the actor underlying the supervisor recieves a MaximumNumberOfRestartsWithinTimeRangeReached message, you can specify
|
||||
a Procedure2<ActorRef,MaximumNumberOfRestartsWithinTimeRangeReached> when creating the SupervisorConfig. This handler will be called with the ActorRef of the supervisor and the
|
||||
MaximumNumberOfRestartsWithinTimeRangeReached message.
|
||||
|
||||
.. code-block:: java
|
||||
|
||||
import static akka.config.Supervision.*;
|
||||
import static akka.actor.Actors.*;
|
||||
import akka.event.JavaEventHandler;
|
||||
|
||||
Procedure2<ActorRef, MaximumNumberOfRestartsWithinTimeRangeReached> handler = new Procedure2<ActorRef, MaximumNumberOfRestartsWithinTimeRangeReached>() {
|
||||
public void apply(ActorRef ref, MaximumNumberOfRestartsWithinTimeRangeReached max) {
|
||||
JavaEventHandler.error(ref, max);
|
||||
}
|
||||
};
|
||||
|
||||
Supervisor supervisor = new Supervisor(
|
||||
new SupervisorConfig(
|
||||
new AllForOneStrategy(new Class[]{Exception.class}, 3, 5000),
|
||||
new Supervise[] {
|
||||
new Supervise(
|
||||
actorOf(MyActor1.class),
|
||||
permanent()),
|
||||
Supervise(
|
||||
actorOf(MyActor2.class),
|
||||
permanent())
|
||||
},handler));
|
||||
|
||||
|
||||
|
||||
You can link and unlink actors from a declaratively defined supervisor using the 'link' and 'unlink' methods:
|
||||
|
||||
.. code-block:: java
|
||||
|
|
|
|||
|
|
@ -121,6 +121,29 @@ The Actor's supervision can be declaratively defined by creating a "Supervisor'
|
|||
|
||||
Supervisors created like this are implicitly instantiated and started.
|
||||
|
||||
To cofigure a handler function for when the actor underlying the supervisor recieves a MaximumNumberOfRestartsWithinTimeRangeReached message, you can specify a function of type
|
||||
(ActorRef, MaximumNumberOfRestartsWithinTimeRangeReached) => Unit when creating the SupervisorConfig. This handler will be called with the ActorRef of the supervisor and the
|
||||
MaximumNumberOfRestartsWithinTimeRangeReached message.
|
||||
|
||||
|
||||
.. code-block:: scala
|
||||
|
||||
val handler = {
|
||||
(supervisor:ActorRef,max:MaximumNumberOfRestartsWithinTimeRangeReached) => EventHandler.notify(supervisor,max)
|
||||
}
|
||||
|
||||
val supervisor = Supervisor(
|
||||
SupervisorConfig(
|
||||
AllForOneStrategy(List(classOf[Exception]), 3, 1000),
|
||||
Supervise(
|
||||
actorOf[MyActor1],
|
||||
Permanent) ::
|
||||
Supervise(
|
||||
actorOf[MyActor2],
|
||||
Permanent) ::
|
||||
Nil), handler)
|
||||
|
||||
|
||||
You can link and unlink actors from a declaratively defined supervisor using the 'link' and 'unlink' methods:
|
||||
|
||||
.. code-block:: scala
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue