简体   繁体   English

Untyped Actors(Java + Akka):重新排列未处理的消息

[英]Untyped Actors (Java+Akka): requeuing unhandled messages

I'm creating a system of actors using Java+Akka. 我正在使用Java + Akka创建一个演员系统。 In particular, I define untyped actors by providing an implementation of the onReceive() method. 特别是,我通过提供onReceive()方法的实现来定义非类型化的actor。

In that method, I implement the behavior of the actor by defining the logic to be executed upon reception of a message. 在该方法中,我通过定义在接收消息时要执行的逻辑来实现actor的行为。 It may be something as: 它可能是这样的:

public void onReceive(Object msg) throws Exception {
  if(msg instanceof MsgType1){ ... }
  else if(msg instanceof MsgType2){ ... }
  else unhandled(msg);
}

However, what if the actor is interested in only a single type of msg? 但是,如果演员只对单一类型的消息感兴趣呢? Is it possible to implement a selective receive, so that the actor waits for a certain msg (and the system automatically re-queues all the other types of messages)??? 是否可以实现选择性接收,以便actor等待某个msg(并且系统自动重新排队所有其他类型的消息)???

This "a la Erlang" message processing mode is not available in Akka AFAIK. 这种“a la Erlang”消息处理模式在Akka AFAIK中不可用。 However, you can use Stash to obtain the effect you want. 但是,您可以使用Stash来获得所需的效果。

So the code would look like this 所以代码看起来像这样

public void onReceive(Object msg) throws Exception {
    if(msg instanceof MsgType1){ ... }
    else if(msg instanceof MsgType2){ ... }
    else stash();
}

At some point in the message handling you would switch to another state (presumably by calling getContext().become ). 在消息处理的某个时刻,您将切换到另一个状态(可能是通过调用getContext().become )。 You would also do a unstashAll() call in order to re-append the messages you ignored until that point back to the mailbox. 您还可以执行unstashAll()调用,以便重新附加您忽略的邮件,直到该点返回邮箱。

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

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