Search code examples
scalaakkaspawn

Akka spawn actor issue


I'm trying to realise the "actors fabric". An application include fabric-actor, which must spawn child actors on demand.

I tried to realise this as follows:

 object BranchCreator {
     final case class CreateBranche(name: String)
            
     def apply(): Behavior[CreateBranche] = {
                Behaviors.receiveMessage { (context, message) =>
                 context.spawn(new Branch(), message.name)
               }
           }
    }

But method spawn is not available for context. Only "name" and "copy" are available for context.


Solution

  • Use Behaviors.receive instead of Behaviors.receiveMessage

    As per documentation of Behaviors.receiveMessage:

    Simplified version of Behaviors.Receive with only a single argument - the message to be handled. Useful for when the context is already accessible by other means, like being wrapped in an setup or similar. [cut]