Created
May 25, 2016 11:38
-
-
Save M0ns1gn0r/45ca2e0e22069c1ce38b394b235b0566 to your computer and use it in GitHub Desktop.
Sample code for the question on StackOverflow: http://stackoverflow.com/questions/37386801/deathwatchnotification-when-stopping-parent-actor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Download NuGet packages "Akka" and "Akka.FSharp" to get these dlls. | |
#r @"System.Collections.Immutable.dll" | |
#r @"Newtonsoft.Json.dll" | |
#r @"Akka.dll" | |
#r @"Akka.FSharp.dll" | |
open Akka.Actor | |
open Akka.FSharp | |
let system = System.create "FSharpSystem" <| Configuration.defaultConfig () | |
/// Creates an actor with a dynamic name. | |
let spawnChild (af : IActorRefFactory) (f : Actor<'Message> -> Cont<'Message, 'Returned>) = | |
let e = Linq.Expression.ToExpression(fun () -> new FunActor<'Message, 'Returned>(f)) | |
let props = Props.Create e | |
af.ActorOf(props) | |
let coordinator (mailbox: Actor<string>) = | |
let rec loop () = | |
actor { | |
let! msg = mailbox.Receive() | |
logInfof mailbox "Coordinator received message %s." msg | |
match msg with | |
| "start" -> | |
spawn mailbox "child1" (actorOf2 (fun box _ -> box.Sender() <! "continue1")) <! "do" | |
| "continue1" -> | |
spawn mailbox "child2" (actorOf2 (fun box _ -> box.Sender() <! "continue2")) <! "do" | |
| "continue2" -> | |
spawn mailbox "child3" (actorOf2 (fun box _ -> box.Sender() <! "finish")) <! "do" | |
| "finish" -> | |
mailbox.Context.Stop mailbox.Self | |
| _ -> mailbox.Unhandled msg | |
return! loop () | |
} | |
loop () | |
let receiver = | |
let body mailbox msg = | |
logInfo mailbox "Receiver got a request." | |
let sender = mailbox.Sender() | |
spawnChild mailbox coordinator <! "start" | |
spawn system "receiver" (actorOf2 body) | |
receiver <! "incoming_request" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment