From dba36729a0a622d1495ad1e2774b63d4bbcede86 Mon Sep 17 00:00:00 2001 From: "Andy.Chen" Date: Fri, 23 Aug 2024 15:54:59 +0800 Subject: [PATCH] feat: streamlined approach for Behaviors #1444 --- .../pekko/actor/typed/javadsl/Behaviors.scala | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/actor-typed/src/main/scala/org/apache/pekko/actor/typed/javadsl/Behaviors.scala b/actor-typed/src/main/scala/org/apache/pekko/actor/typed/javadsl/Behaviors.scala index 57a57d89796..c88df73cc46 100644 --- a/actor-typed/src/main/scala/org/apache/pekko/actor/typed/javadsl/Behaviors.scala +++ b/actor-typed/src/main/scala/org/apache/pekko/actor/typed/javadsl/Behaviors.scala @@ -143,6 +143,27 @@ object Behaviors { def receiveMessage[T](onMessage: pekko.japi.Function[T, Behavior[T]]): Behavior[T] = new BehaviorImpl.ReceiveBehavior((_, msg) => onMessage.apply(msg)) + /** + * Simplified version of [[receiveMessage]] with only a single argument - the message + * to be handled, but it doesn't produce a return value of next behavior. + * Useful for when the behavior doesn't want to change in runtime. + * + * Construct an actor behavior that can react to incoming messages but not to + * lifecycle signals. After spawning this actor from another actor (or as the + * guardian of an [[pekko.actor.typed.ActorSystem]]) it will be executed within an + * [[ActorContext]] that allows access to the system, spawning and watching + * other actors, etc. + * + * Compared to using [[AbstractBehavior]] this factory is a more functional style + * of defining the `Behavior`. Processing the next message will not result in + * different behavior than this one + */ + def receiveMessage[T](onMessage: pekko.japi.Procedure[T]): Behavior[T] = + new BehaviorImpl.ReceiveBehavior((_, msg) => { + onMessage.apply(msg) + same[T] + }) + /** * Construct an actor behavior that can react to both incoming messages and * lifecycle signals. After spawning this actor from another actor (or as the