我在和akka.net开玩笑,试着理解监督。我以为我明白了,但它不像我期望的那样起作用。
我试图获得一个带有监视器和子角色的小示例,在该示例中,监视器应该在子进程中的异常情况下重新启动子程序。子程序似乎正在重新启动,但我不明白为什么,因为代码似乎没有执行我的SupervisorStrategy。我改变了我的策略返回Directive.Stop,以检查我是否可以阻止演员,但这也不起作用。所以现在看来,我有一个不可阻挡的演员,这是一件好事,只要我不想杀死它:)。运行示例的代码如下:
open Akka
open Akka.Actor
open Akka.Tools
open Akka.FSharp
open System
type MonitorMessage =
| Create
type ChildMessage =
| Ping
| Kill
let test() =
let systemName = "my-system"
let system = System.create systemName (Configuration.load())
let handleChildMessage = function
| Ping ->
printfn "Received %A" Ping
printfn "Pong: %A" (DateTime.Now.Ticks)
| Kill ->
1/0 |> ignore
let createChild parent id =
spawnOpt parent (id.ToString()) (actorOf handleChildMessage)
[ SpawnOption.SupervisorStrategy (Strategy.OneForOne (fun error ->
match error with
| _ ->
printfn "%A" error
Directive.Stop
)) ]
let handleMonitorMessage (actor:Actor<MonitorMessage>) message =
match message with
| Create ->
let sender = actor.Sender()
sender <! createChild actor (Guid.NewGuid())
let monitor = spawn system "monitor" (actorOf2 handleMonitorMessage)
let child = monitor <? Create |> Async.RunSynchronously
child <! Ping
child <! Kill
child <! Ping
test()
Console.ReadLine() |> ignore发布于 2016-02-29 06:32:50
createChild函数不会将参与者创建为监视器的子级。这是因为您已经将参与者系统传递给了spawnOpt函数--这意味着生成的参与者将是顶级角色(直接生活在参与者系统内核之下)。您需要将其更改为spawnOpt parent,以便将其创建为父级的子级。https://stackoverflow.com/questions/35688839
复制相似问题