[英]AKKA timeout on message
I'm using java with akka. 我将Java与Akka一起使用。 I have this situation: 我有这种情况:
Actor A sends a message to actor B. 演员A向演员B发送一条消息。
B waits a timeout and, if in this timeout no identical message(same type of message) occours, sends a message to actor C. B等待一个超时,如果在这个超时中没有相同的消息(相同消息类型)发生,则向参与者C发送消息。
How can I implement it? 我该如何实施? What I have to write in B onReceive method? 我必须用B onReceive方法编写什么?
public void onReceive(Object message){
if(message instanceof EventOccurred){
//wait a timeout to see if other EventOccurred message are coming
//and then send a messago to actor C
}
}
In my project I have a file system watcher who receives events when a file is modified,created or deleted (This is actor A), on first I'm watching only a file. 在我的项目中,我有一个文件系统观察者,当文件被修改,创建或删除时(这是参与者A),他会接收事件,首先我只观察一个文件。
When an event on this file occours my actor A sends a message to another actor(actor B) that first wait a timeout and secondly,if no other events occurred on that file(so if no other message are arrived on actor B), sends a message to actor C that calculate an hash of that file. 当此文件上的事件发生时,我的演员A向另一个演员(演员B)发送了一条消息,该消息首先等待超时,其次,如果该文件上没有其他事件发生(因此如果演员B上没有到达其他消息),则发送给执行者C的消息,它计算该文件的哈希值。
@Snickers3192 I implemented all this things I wanted only to know if it's really possible with akka. @ Snickers3192我实现了所有这些功能,我只想知道akka是否真的可能。
Maybe ReceiveTimeout can help you. 也许ReceiveTimeout可以为您提供帮助。 It's a message driven non-blocking way to receive a timeout. 这是一种消息驱动的非阻塞方式来接收超时。 From Akka docs: 来自Akka文档:
setReceiveTimeout defines the inactivity timeout after which the sending of a ReceiveTimeout message is triggered setReceiveTimeout定义不活动超时,之后将触发ReceiveTimeout消息的发送
public class MyReceiveTimeoutUntypedActor extends UntypedActor {
public MyReceiveTimeoutUntypedActor() {
// To set an initial delay
getContext().setReceiveTimeout(Duration.create("30 seconds"));
}
public void onReceive(Object message) {
if (message.equals("Hello")) {
// To set in a response to a message
getContext().setReceiveTimeout(Duration.create("1 second"));
} else if (message instanceof ReceiveTimeout) {
// To turn it off
getContext().setReceiveTimeout(Duration.Undefined());
} else {
unhandled(message);
}
}
}
See also: 也可以看看:
if you want to wait for any message, you simply set a receiveTimeout - Viktor Klang https://stackoverflow.com/a/12754034/1956540 如果您想等待任何消息,只需设置一个receiveTimeout-Viktor Klang https://stackoverflow.com/a/12754034/1956540
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.