# Creating Guardian Actor with SpawnProtocol

**URL:** <https://discuss.akka.io/t/creating-guardian-actor-with-spawnprotocol/9211>\
**Category:** Actors\
**Tags:** akka-typed, java\
**Created:** [November 30, 2021, 4:53pm UTC](https://discuss.akka.io/t/creating-guardian-actor-with-spawnprotocol/9211 "2021-11-30T16:53:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AND2797](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/and2797/32/2616_2.png) [@AND2797](https://discuss.akka.io/u/AND2797)\
**Post date:** [November 30, 2021, 4:53pm UTC](https://discuss.akka.io/t/creating-guardian-actor-with-spawnprotocol/9211/1 "2021-11-30T16:53:14Z")

</div>

I want to create a GuardianActor with the capability of spawning actors. I’m confused how this actor should be created. Currently I’m trying to do something like this →

```auto
public class GuardianActor extends AbstractBehavior<SpawnProtocol.Command>{
   
    private GuardianActor(ActorContext<SpawnProtocol.Command> context){
         super(context);
    }
    
    public static Behavior<SpawnProtocol.Command> createRootBehavior() { 
         return Behaviors.setup(context -> { 
                   // some initial tasks
                   return new GuardianActor(context); 
        });

   @Override 
   public Receive<SpawnProtocol.Command> createReceive() {
        return newReceiveBuilder().onMessage(SpawnProtocol.Command.class, this::onSpawnProtocolCommand).build();
   }

   private Behavior<SpawnProtocol.Command> onSpawnProtocolCommand(SpawnProtocol.Command msg) {

   // what do i do here?

   return this;
  }
}

```

Here antyime a `SpawnProtocol.Spawn<>` message is sent to the GuardianActor it is intercepted by the method specified in `createReceive`. However if I simply return `SpawnProtocol.create()` from the `createRootBehavior` then the creation of the actor just takes place on its own. What is the correct pattern to follow ?

---

<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:** [December 1, 2021, 2:24pm UTC](https://discuss.akka.io/t/creating-guardian-actor-with-spawnprotocol/9211/2 "2021-12-01T14:24:04Z")

</div>

The SpawnProtocol already is an actor that you can use as guardian without needing to create a class implementing the message handling yourself:

```java
ActorSystem<SpawnProtocol.Command> system = 
  ActorSystem.create(SpawnProtocol.create(), "MySystem");

```

If you are curious about how that is implemented you can find the [sources here](https://github.com/akka/akka/blob/d703a2afe0e0c6dc8f1c0321b0f4652600e2ae99/akka-actor-typed/src/main/scala/akka/actor/typed/SpawnProtocol.scala#L55), they are written in Scala but you can probably get the gist of it even if you only know Java.

---

<div class="post-metadata">

**Author:** ![AND2797](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/and2797/32/2616_2.png) [@AND2797](https://discuss.akka.io/u/AND2797)\
**Post date:** [December 1, 2021, 2:28pm UTC](https://discuss.akka.io/t/creating-guardian-actor-with-spawnprotocol/9211/3 "2021-12-01T14:28:32Z")

</div>

Hi Johan,  
Thanks for your reply. This makes it quite clear. Since you mention that `SpawnProtocol` is already an actor that can be used as a guardian, how would I “send” a message to this actor? Is it safe to pass around `ActorSystem<SpawnProtocol.Command> system` to constructors?

---

<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:** [December 1, 2021, 2:48pm UTC](https://discuss.akka.io/t/creating-guardian-actor-with-spawnprotocol/9211/4 "2021-12-01T14:48:31Z")

</div>

`ActorSystem<T>` is also the `ActorRef<T>` of the guardian actor, so you can pass to methods/constructors that does not know that it is the actor system.

---

<div class="post-metadata">

**Author:** ![dmmax](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/dmmax/32/3018_2.png) [@dmmax](https://discuss.akka.io/u/dmmax)\
**Post date:** [May 1, 2023, 7:07am UTC](https://discuss.akka.io/t/creating-guardian-actor-with-spawnprotocol/9211/5 "2023-05-01T07:07:10Z")

</div>

Hi @AND2797  
Maybe you will be also interested to have a look into the library that I created recently: [GitHub - dmmax/akka-spawn-protocol: Uses Spawn Protocol to create Akka Actors from outside. It can be applied at any level of actors' hierarchy](https://github.com/dmmax/akka-spawn-protocol), it is a kinda extended version of Akka’s spawn protocol that also supports Guice as an extension
