# Initializing typed actor behavior from an async (bootstrap) call?

**URL:** <https://discuss.akka.io/t/initializing-typed-actor-behavior-from-an-async-bootstrap-call/9186>\
**Category:** Akka Libraries\
**Tags:** akka-typed\
**Created:** [November 25, 2021, 1:04pm UTC](https://discuss.akka.io/t/initializing-typed-actor-behavior-from-an-async-bootstrap-call/9186 "2021-11-25T13:04:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![agemooij](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/agemooij/32/1687_2.png) [@agemooij](https://discuss.akka.io/u/agemooij)\
**Post date:** [November 25, 2021, 1:04pm UTC](https://discuss.akka.io/t/initializing-typed-actor-behavior-from-an-async-bootstrap-call/9186/1 "2021-11-25T13:04:23Z")

</div>

Hi

I’m finally getting started with Akka Typed and I ran into this issue:

- My main/root actor needs to bootstrap the system from persistent state
- So from `Behaviors.setup` I would like to perform some async call to initialize everything and use that to produce the final Behavior

But there is no async version of `Behaviors.setup`.  
Are there other ways of initializing a `Behavior` from a `Future`?

The only way I can see this work is to pipe the result of the future to self, wrapped in a message, and return a temporary `Behavior` that only handles that specific message. And then that temporary `Behavior` switches to the final full `Behavior` by handling that message.  
Is that the right pattern for initializing an actor from an async call?

Thanks,  
Age

---

<div class="post-metadata">

**Author:** ![johanandren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/johanandren/32/85_2.png) [@johanandren](https://discuss.akka.io/u/johanandren)\
**Post date:** [November 29, 2021, 2:29pm UTC](https://discuss.akka.io/t/initializing-typed-actor-behavior-from-an-async-bootstrap-call/9186/2 "2021-11-29T14:29:24Z")

</div>

We do not have any public API for that, but I think it is relatively easy to achieve, and it will likely be something like what you describe. I think it is hard to abstract over in some generic form since the completion message needs to be part of the actor protocol.

We have a (somewhat special) deferred start used in the guardian actor in the Akka internals, stashing messages until some async operation completes and then switches to the “actual” behavior that you can see here: [akka/GuardianStartupBehavior.scala at main · akka/akka · GitHub](https://github.com/akka/akka/blob/main/akka-actor-typed/src/main/scala/akka/actor/typed/internal/adapter/GuardianStartupBehavior.scala#L30)

---

<div class="post-metadata">

**Author:** ![agemooij](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/agemooij/32/1687_2.png) [@agemooij](https://discuss.akka.io/u/agemooij)\
**Post date:** [November 29, 2021, 9:12pm UTC](https://discuss.akka.io/t/initializing-typed-actor-behavior-from-an-async-bootstrap-call/9186/3 "2021-11-29T21:12:36Z")

</div>

Thank Johan!

I was hoping for an abstraction like this:

```scala
sealed trait Command

def bootstrap[Command](context: ActorContext[Command]): Future[Behavior[Command]] = ???

val behavior = Behaviors.setupAsync(bootstrap(_))

```

But I agree that there are some difficult choices to be made about stashing behavior and what to do with failed bootstrap Futures that make it hard to fully abstract away.

So I ended up with this kind of pattern instead:

```scala
sealed trait Command
private case class Bootstrapped(...)

private def bootstrapped(...): Behavior[Command] =
  Behaviors.receive { (context, command) =>
    ...
  }

...

val behavior = Behaviors.setup[Command] { context =>
  context.pipeToSelf(bootstrap(context)) {
    case Success(bootstrapped: Bootstrapped) => bootstrapped
    case Failure(exception) => ...
  }

  Behaviors.receiveMessage[Command] {
    case bootstrapped: Bootstrapped => bootstrapped(...)
    case _ => Behaviors.unhandled // or stash
  }
}

```

Age

---

<div class="post-metadata">

**Author:** ![patriknw](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/patriknw/32/89_2.png) [@patriknw](https://discuss.akka.io/u/patriknw)\
**Post date:** [December 5, 2021, 3:37pm UTC](https://discuss.akka.io/t/initializing-typed-actor-behavior-from-an-async-bootstrap-call/9186/4 "2021-12-05T15:37:40Z")

</div>

Using pipeToSelf looks good to me for this.

Here is another example using async interactions. [Stash • Akka Documentation](https://doc.akka.io/docs/akka/current/typed/stash.html)

---

<div class="post-metadata">

**Author:** ![agemooij](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/agemooij/32/1687_2.png) [@agemooij](https://discuss.akka.io/u/agemooij)\
**Post date:** [December 7, 2021, 9:24am UTC](https://discuss.akka.io/t/initializing-typed-actor-behavior-from-an-async-bootstrap-call/9186/5 "2021-12-07T09:24:57Z")

</div>

Thanks @patriknw, that is indeed a pretty good example.
