我努力理解为什么基于下面的语句是在我的发电机演员我的记者的演员是没有收到消息:
reporter <! Message input
我的记者的演员如下:
let reporterActor (mailbox:Actor<_>) =
let rec loop() = actor { let! msg = mailbox.Receive()
match msg |> box :?> Command with
| Start ->()
| Message v -> printf "%s" v
| Exit -> mailbox.Context.System.Terminate() |> ignore }
loop() |> ignore
基本上,一个控制台启动,接受来自用户的输入。我的生成器actor将该输入转发给我的Reporter actor。但是,上面的代码永远不会被执行。
的代码如下:
module Main
open System
open Akka.FSharp
open Akka.Actor
open Actors
type Command =
| Message of string
| Start | Exit
let reporterActor (mailbox:Actor<_>) =
let rec loop() = actor { let! msg = mailbox.Receive()
match msg |> box :?> Command with
| Start ->()
| Message v -> printf "%s" v
| Exit -> mailbox.Context.System.Terminate() |> ignore }
loop() |> ignore
let generatorActor (reporter:IActorRef) (mailbox:Actor<_>) message =
let handle input = match input with
| "exit" -> mailbox.Context.System.Terminate |> ignore
| _ -> reporter <! Message input
handle (Console.ReadLine().ToLower())
[<EntryPoint>]
let main argv =
let system = System.create "system" (Configuration.load())
let reporterActor = spawn system "reporterActor" (actorOf(reporterActor))
let generatorActor = spawn system "generatorActor" (actorOf2(generatorActor reporterActor))
generatorActor <! Start
system.AwaitTermination()
0
更新:
我才知道,我可以用任意的消息参数更换邮箱参数触发我的记者的演员:
let reporterActor message =
match message |> box :?> Command with
| Start ->()
| Message v -> printf "Reporting: %s" v
| Exit -> failwith "Kill this!"
我还是不明白我什么时候应该用邮箱吧rameter与何时我应该依赖消息参数。
谢谢Adam。你有测试演员的经验吗? –
我没有,但是我发现这个链接https://petabridge.com/blog/how-to-unit-test-akkadotnet-actors-akka-testkit/它可能对测试有帮助。我不确定是否有F#化的库,但我相信你可以使用F#来处理它。 – awright18