简体   繁体   中英

How to create a child actor in akka.net using F#?

I have two actors - childActor and parentActor

open System
open Akka
open Akka.FSharp

let systemActor = System.create "systemActor" <| Configuration.defaultConfig()

let childActor (mailbox: Actor<_>) = 
    let rec loop() = actor {
        let! message = mailbox.Receive()
        printfn "Message received"
        return! loop()
    }
    loop()

let parentActor (mailbox: Actor<_>) = 
    let rec loop() = actor {
        let! message = mailbox.Receive()
        printfn "Message received"
        return! loop()
    }
    loop()

I can create the parent actor reference using spawn function.

let parentRef = spawn systemActor "parentActor" parentActor

Now what I want to do here is to create the child actor reference under parent actor. Something like below.

let childRef = spawn parentRef "childActor" childActor

But this is not working. spawn function requires a parameter of type IActorRefFactory so it is not accepting IActorRef. Is there any way to create a child actor under parent actor in akka.net using F#?

IActorRefFactory is an interface responsible for determining a parent and in case of Akka.FSharp it's implemented by ActorSystem and Actor<_> as well. So in your case just use:

let childRef = spawn mailbox "childActor" childActor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM