简体   繁体   English

在未来的Akka Java中无限期地等待演员的回应

[英]Wait for an actor response indefinitely in a future akka java

I have a non-actor-based piece of code which delegates some operations to an akka actor and I would like to wait this actor response indefinitely, I mean, until this actor returns a response whatever time it takes. 我有一段非基于角色的代码,它将一些操作委派给akka角色,我想无限期地等待该角色响应,直到该角色返回所花的时间。 The problem is I have not idea how to wait indefinitely in a Future with Pattern.ask and Await.result methods. 问题是我不知道如何在将来使用Pattern.ask和Await.result方法无限期地等待。

I would something like this: 我会这样:

Timeout timeout = new Timeout(Duration.inf());
Future<Object> future = Patterns.ask(actor, msg, timeout);
String result = (String) Await.result(future, timeout.duration());

but this does not work because Timeout does not accept a Duration object as constructor parameter, it only accepts FiniteDuration objetcs... 但这不起作用,因为超时不接受Duration对象作为构造函数参数,它仅接受FiniteDuration objetcs ...

Any idea? 任何想法?

Cheers 干杯

You may never receive an answer, since message delivery is not 100% guaranteed. 您可能永远不会收到答案,因为不能100%保证消息传递。 As such, waiting indefinitely isn't a good approach -- you could well end up waiting forever. 因此,无限期地等待并不是一个好方法-您最终可能会永远等待。

You probably want some level of timeout (perhaps a long one, if it suits), and then a fallback case where you re-send your request as necessary. 您可能需要某种程度的超时(如果合适的话,可能需要较长的超时时间),然后是一个后备情况,在这种情况下您可以根据需要重新发送请求。 This would be the more robust way of handling this situation. 这将是处理这种情况的更可靠的方法。

Disclaimer : I'm not very experienced with Akka. 免责声明:我对Akka不太了解。

Suggestion : can't you forego the Timeout object and simply write 建议:您不能放弃Timeout对象,只是写

Future<Object> future = Patterns.ask(actor, msg);
Await.result(f, Duration.Inf);

Either that, or use Timeout timeout = Timeout.never 要么使用Timeout timeout = Timeout.never ,要么使用Timeout timeout = Timeout.never

Beware, however : 但是要当心:

A Timeout with infinite duration. 具有无限持续时间的超时。 Will never timeout. 永远不会超时。 Use extreme caution with this as it may cause memory leaks, blocked threads, or may not even be supported by the receiver, which would result in an exception. 请格外小心,因为这可能会导致内存泄漏,线程阻塞,甚至接收器甚至不支持它,这将导致异常。 (from the Akka API doc) (来自Akka API文档)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM