diff --git a/akka-typed/src/main/java/akka/typed/javadsl/Actor.java b/akka-typed/src/main/java/akka/typed/javadsl/Actor.java index c98363eb93..fb0f84013b 100644 --- a/akka-typed/src/main/java/akka/typed/javadsl/Actor.java +++ b/akka-typed/src/main/java/akka/typed/javadsl/Actor.java @@ -192,7 +192,7 @@ public abstract class Actor { * to reuse the previous behavior, including the hint that the message has not * been handled. This hint may be used by composite behaviors that delegate * (partial) handling to other behaviors. - * + * * @return pseudo-behavior marking “unhandled” */ static public Behavior unhandled() { @@ -405,7 +405,7 @@ public abstract class Actor { * By default, this method returns `unhandled`. */ public Behavior onSignal(Signal msg) throws Exception { - return Actor.unhandled(); + return unhandled(); } } diff --git a/akka-typed/src/main/scala/akka/typed/Behavior.scala b/akka-typed/src/main/scala/akka/typed/Behavior.scala index 258fd317c2..0a7b377e12 100644 --- a/akka-typed/src/main/scala/akka/typed/Behavior.scala +++ b/akka-typed/src/main/scala/akka/typed/Behavior.scala @@ -79,6 +79,39 @@ abstract class ExtensibleBehavior[T] extends Behavior[T] { object Behavior { + /** + * Return this behavior from message processing in order to advise the + * system to reuse the previous behavior. This is provided in order to + * avoid the allocation overhead of recreating the current behavior where + * that is not necessary. + */ + def same[T]: Behavior[T] = SameBehavior.asInstanceOf[Behavior[T]] + /** + * Return this behavior from message processing in order to advise the + * system to reuse the previous behavior, including the hint that the + * message has not been handled. This hint may be used by composite + * behaviors that delegate (partial) handling to other behaviors. + */ + def unhandled[T]: Behavior[T] = UnhandledBehavior.asInstanceOf[Behavior[T]] + /** + * Return this behavior from message processing to signal that this actor + * shall terminate voluntarily. If this actor has created child actors then + * these will be stopped as part of the shutdown procedure. The PostStop + * signal that results from stopping this actor will NOT be passed to the + * current behavior, it will be effectively ignored. + */ + def stopped[T]: Behavior[T] = StoppedBehavior.asInstanceOf[Behavior[T]] + + /** + * A behavior that treats every incoming message as unhandled. + */ + def empty[T]: Behavior[T] = EmptyBehavior.asInstanceOf[Behavior[T]] + + /** + * A behavior that ignores every incoming message and returns “same”. + */ + def ignore[T]: Behavior[T] = IgnoreBehavior.asInstanceOf[Behavior[T]] + /** * INTERNAL API. */ diff --git a/akka-typed/src/main/scala/akka/typed/patterns/Restarter.scala b/akka-typed/src/main/scala/akka/typed/patterns/Restarter.scala index 3afdf29e3c..7e44bb718e 100644 --- a/akka-typed/src/main/scala/akka/typed/patterns/Restarter.scala +++ b/akka-typed/src/main/scala/akka/typed/patterns/Restarter.scala @@ -7,7 +7,7 @@ package patterns import scala.reflect.ClassTag import scala.util.control.NonFatal import akka.event.Logging -import akka.typed.Behavior._ +import akka.typed.scaladsl.Actor import akka.typed.scaladsl.Actor._ /** @@ -22,7 +22,7 @@ final case class Restarter[T, Thr <: Throwable: ClassTag](initialBehavior: Behav private def restart(ctx: ActorContext[T], startedBehavior: Behavior[T]): Behavior[T] = { try Behavior.interpretSignal(startedBehavior, ctx, PreRestart) catch { case NonFatal(_) ⇒ } // no need to canonicalize, it's done in the calling methods - validateAsInitial(undefer(initialBehavior, ctx)) + Behavior.validateAsInitial(Behavior.undefer(initialBehavior, ctx)) } private def canonical(b: Behavior[T], ctx: ActorContext[T]): Behavior[T] = @@ -32,8 +32,8 @@ final case class Restarter[T, Thr <: Throwable: ClassTag](initialBehavior: Behav else if (b eq behavior) Same else { b match { - case d: DeferredBehavior[_] ⇒ canonical(Behavior.undefer(d, ctx), ctx) - case b ⇒ Restarter[T, Thr](initialBehavior, resume)(b) + case d: Behavior.DeferredBehavior[_] ⇒ canonical(Behavior.undefer(d, ctx), ctx) + case b ⇒ Restarter[T, Thr](initialBehavior, resume)(b) } } @@ -79,12 +79,12 @@ final case class MutableRestarter[T, Thr <: Throwable: ClassTag](initialBehavior private[this] var current: Behavior[T] = _ private def startCurrent(ctx: ActorContext[T]) = { // we need to undefer it if needed the first time we have access to a context - if (current eq null) current = validateAsInitial(undefer(initialBehavior, ctx)) + if (current eq null) current = Behavior.validateAsInitial(Behavior.undefer(initialBehavior, ctx)) } private def restart(ctx: ActorContext[T]): Behavior[T] = { try Behavior.interpretSignal(current, ctx, PreRestart) catch { case NonFatal(_) ⇒ } - validateAsInitial(undefer(initialBehavior, ctx)) + Behavior.validateAsInitial(Behavior.undefer(initialBehavior, ctx)) } override def management(ctx: ActorContext[T], signal: Signal): Behavior[T] = { diff --git a/akka-typed/src/main/scala/akka/typed/scaladsl/Actor.scala b/akka-typed/src/main/scala/akka/typed/scaladsl/Actor.scala index 95165a789a..9015d450ae 100644 --- a/akka-typed/src/main/scala/akka/typed/scaladsl/Actor.scala +++ b/akka-typed/src/main/scala/akka/typed/scaladsl/Actor.scala @@ -287,7 +287,7 @@ object Actor { * avoid the allocation overhead of recreating the current behavior where * that is not necessary. */ - def Same[T]: Behavior[T] = SameBehavior.asInstanceOf[Behavior[T]] + def Same[T]: Behavior[T] = Behavior.same /** * Return this behavior from message processing in order to advise the @@ -295,13 +295,7 @@ object Actor { * message has not been handled. This hint may be used by composite * behaviors that delegate (partial) handling to other behaviors. */ - def Unhandled[T]: Behavior[T] = UnhandledBehavior.asInstanceOf[Behavior[T]] - - /* - * TODO write a Behavior that waits for all child actors to stop and then - * runs some cleanup before stopping. The factory for this behavior should - * stop and watch all children to get the process started. - */ + def Unhandled[T]: Behavior[T] = Behavior.unhandled /** * Return this behavior from message processing to signal that this actor @@ -310,17 +304,17 @@ object Actor { * signal that results from stopping this actor will NOT be passed to the * current behavior, it will be effectively ignored. */ - def Stopped[T]: Behavior[T] = StoppedBehavior.asInstanceOf[Behavior[T]] + def Stopped[T]: Behavior[T] = Behavior.stopped /** * A behavior that treats every incoming message as unhandled. */ - def Empty[T]: Behavior[T] = EmptyBehavior.asInstanceOf[Behavior[T]] + def Empty[T]: Behavior[T] = Behavior.empty /** * A behavior that ignores every incoming message and returns “same”. */ - def Ignore[T]: Behavior[T] = IgnoreBehavior.asInstanceOf[Behavior[T]] + def Ignore[T]: Behavior[T] = Behavior.ignore /** * Construct an actor behavior that can react to both incoming messages and