Typed actor context capture

akka-typed

(Maxim Valyanskiy) #1

I wonder is this a correct way to use context in typed actor:

  def behaviour: Behavior[Protocol] = Behaviors.setup[Protocol] { ctx ⇒
    // ....
        def initState(state: State): Behavior[Protocol] = Behaviors.receiveMessage {
          case Initialized =>
            // context from Behavior.setup
            ctx.log.debug(s"MESSAGE: Initialized")

          // ...
        }
        initState(State.empty)
  }

Do I need to use Behaviours.receive to access context or it is ok to use context from upper Behaviours.setup?


(Konrad `ktoso` Malawski) #2

Absolutely :slight_smile:
This is how you’d use the functional style APIs.


(Johan Andrén) #3

In that specific example, not using the context in the setup block, you could also use Behaviors.receive where you will get the context together with the message as parameters each time there is a message to process.