def apply()

in pekko-sample-cluster-scala/src/main/scala/sample/cluster/stats/App.scala [17:42]


    def apply(): Behavior[Nothing] = Behaviors.setup[Nothing] { ctx =>
      val cluster = Cluster(ctx.system)
      if (cluster.selfMember.hasRole("compute")) {
        // on every compute node there is one service instance that delegates to N local workers
        val numberOfWorkers =
          ctx.system.settings.config.getInt("stats-service.workers-per-node")
        val workers = ctx
          .spawn(
            Routers
              .pool(numberOfWorkers)(StatsWorker().narrow[StatsWorker.Process])
              // the worker has a per word cache, so send the same word to the same local worker child
              .withConsistentHashingRouting(1, _.word),
            "WorkerRouter")
        val service = ctx.spawn(StatsService(workers), "StatsService")

        // published through the receptionist to the other nodes in the cluster
        ctx.system.receptionist ! Receptionist
          .Register(StatsServiceKey, service)
      }
      if (cluster.selfMember.hasRole("client")) {
        val serviceRouter =
          ctx.spawn(Routers.group(App.StatsServiceKey), "ServiceRouter")
        ctx.spawn(StatsClient(serviceRouter), "Client")
      }
      Behaviors.empty[Nothing]
    }