消息

时间:2015-09-14 13:50:56

标签: java akka

我正在使用java和akka。我有这种情况:

演员A向演员B发送消息。

B等待超时,如果在此超时中没有相同的消息(相同类型的消息)发生,则向actor C发送消息。

我该如何实施?我必须用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
        }
} 

在我的项目中,我有一个文件系统观察程序,在文件被修改,创建或删除时接收事件(这是演员A),首先我只看一个文件。

当此文件上的事件发生时,我的演员A向另一个演员(演员B)发送消息,该演员首先等待超时;其次,如果该文件上没有其他事件发生(因此如果演员B上没有其他消息到达) ),向演员C发送消息,计算该文件的散列。

@Snickers3192我实现了所有这些我只想知道是否真的可以使用akka的东西。

1 个答案:

答案 0 :(得分:1)

也许ReceiveTimeout可以帮到你。这是一种消息驱动的非阻塞方式来接收超时。来自Akka docs:

  

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);
        }
    }
}

另见:

  

如果你想等待任何消息,你只需设置一个receiveTimeout - Viktor Klang   https://stackoverflow.com/a/12754034/1956540