按Akka.NET integration docs中所述将actor与Akka.NET流集成时,我遇到了异常处理问题。如果演员只是抛出异常,那么Akka会吞下它,我必须等待超时触发它才能得到报告。
我推断这是因为演员没有将消息发送回流。采取在线程边界使用大型try-catch的老式方法,我可以将异常编组到发给发件人的消息中,这使它可以突破超时并继续。
下面的示例代码。据我所知,我无法创建一种监督策略,该策略将允许流处理参与者的异常。最好的处理方式是什么?
// Run app with argument "straightThrow" or "catchAndHandle"
class Bleurgher : ReceiveActor
{
public Bleurgher()
{
Receive<string>(s =>
{
switch (s) {
case "straightThrow":
throw new Exception("Bleurgh");
break;
case "catchAndHandle":
try
{
throw new Exception("Bleurgh");
}
catch (Exception x)
{
Console.WriteLine($"Exception : {x.Message}");
Sender.Tell(new Akka.Actor.Status.Failure(x));
};
break;
}
});
}
public static Akka.Actor.Props Props()
{
return Akka.Actor.Props.Create<Bleurgher>();
}
}
class Program
{
static void Main(string[] args)
{
using (var system = ActorSystem.Create("cast"))
using (var materializer = system.Materializer())
{
var data = new List<string>(args);
var bleurgher = system.ActorOf(Bleurgher.Props());
var source = Source.From(data);
var sink = Sink.ForEach<string>(s => Console.WriteLine(s));
var runnable = source
.SelectAsync(1, o => bleurgher.Ask(o, TimeSpan.FromSeconds(5)))
.Select(o => (o.ToString()))
.ToMaterialized(sink, Keep.Right);
runnable.Run(materializer).Wait();
}
}
}