Created
October 11, 2015 16:56
-
-
Save bobbychopra/a81b48d6e9d384d93d63 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module Program | |
open System | |
open Akka | |
open Akka.Actor | |
open Akka.FSharp | |
open Akka.Configuration | |
type Message = | |
| Subscribe | |
| Unsubscribe | |
| Msg of IActorRef * string | |
[<EntryPoint>] | |
let main argv = | |
let subscriber (mailbox:Actor<_>) msg = | |
let eventStream = mailbox.Context.System.EventStream | |
match msg with | |
| Msg (sender, content) -> printfn "%A says %s - %A" (sender.Path) content (mailbox.Self.Path) | |
| Subscribe -> subscribe typeof<Message> mailbox.Self eventStream |> ignore | |
| Unsubscribe -> unsubscribe typeof<Message> mailbox.Self eventStream |> ignore | |
let actorSystem = System.create "myActorSystem" (Configuration.load ()) | |
let subscriber1 = spawn actorSystem "subscriber1" (actorOf2 subscriber) | |
let subscriber2 = spawn actorSystem "subscriber2" (actorOf2 subscriber) | |
let subscriber3 = spawn actorSystem "subscriber3" (actorOf2 subscriber) | |
let publisher = spawn actorSystem "publisher" (actorOf2 | |
(fun mailbox msg -> | |
publish msg mailbox.Context.System.EventStream)) | |
subscriber1 <! Subscribe | |
publisher <! Msg (publisher, "hello") | |
subscriber1 <! Unsubscribe | |
subscriber1 <! Subscribe | |
subscriber2 <! Subscribe | |
subscriber3 <! Subscribe | |
publisher <! Msg (publisher, "hello again") | |
actorSystem.AwaitTermination () | |
0 // return an integer exit code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment