Akka 接收信息超时的处理_Receive Timeout
The UntypedActorContext setReceiveTimeout defines the inactivity timeout after which the sending of a ReceiveTimeout message is triggered.
When specified, the receive function should be able to handle an akka.actor.ReceiveTimeout message. 1 millisecond is the minimum supported timeout.
Please note that the receive timeout might fire and enqueue the ReceiveTimeout message right after another message was enqueued; hence it is not guaranteed that upon reception of the receive timeout there must have been an idle(空闲的,懒惰的) period beforehand(事先提前) as configured via this method. Once set, the receive timeout stays in effect (i.e. continues firing repeatedly after inactivity periods). Pass in Duration.Undefined to switch off this feature.
如下是对接收消息超时的处理,Actor,
package com.usoft4;import akka.actor.ReceiveTimeout;import akka.actor.UntypedActor;import scala.concurrent.duration.Duration;/** * Created by liyanxin on 2015/1/12. */public class MyActor extends UntypedActor { private int x; private int y; public MyActor(int x, int y) { this.x = x; this.y = y; // To set an initial delay this.getContext().setReceiveTimeout(Duration.create("10 seconds")); } @Override public void onReceive(Object message) throws Exception { if (message.equals("Hello")) { // To set in a response to a message getContext().setReceiveTimeout(Duration.create("1 second")); } else if (message instanceof ReceiveTimeout) { System.out.println("接收消息超时"); // To turn it off getContext().setReceiveTimeout(Duration.Undefined()); } else { unhandled(message); } }}
====================END====================