Akka同一类中的两个receiveBuilder

huangapple go评论72阅读模式
英文:

Akka two receiveBuilder in same class

问题

在处理遗留代码时,位于扩展到BaseActor的类内部,有两个处理消息的入口点,如下所示:

private Receive active = receiveBuilder().matchAny((message) -> { 
    
    if(message instanceof String){
        // 一些代码
    } else if(...) {
        // 一些代码
    } else {
        // 一些代码
    }
});

以及这个接收器:

@Override
public Receive createReceive() {
        
    return receiveBuilder().matchAny((message) -> {
        if(message instanceof String){
            // 一些代码
        } else if(...) {
            // 一些代码
        } else {
            // 一些代码
        }
    });
}

我知道有类型化和非类型化的Actor,但在进入activecreateReceive时感到困惑;在本地测试时总是进入createReceive,在服务器上进入active。如果只声明了createReceive而没有active,则使用createReceive

在这里有什么逻辑,或者我在哪里可以找到相关文档。我已经查阅了doc.akka.io,但仍不太清楚。

英文:

Working with legacy code, inside class which is extended to BaseActor, there two entry points for process messages like that:

 private Receive active = receiveBuilder().matchAny((message) -> { 
    
 if(message instanceof String){
    some code
   } else if(...) {
    //some code} else {
    //some code}
   }

and this receiver:

 @Override
    public Receive createReceive() {
        
        return receiveBuilder().matchAny((message) -> {
          if(message instanceof String){
    some code
   } else if(...) {
    //some code} else {
    //some code}
   }
         }

i know that there are typed and untyped actors, but its confusing when enters into active and when into createReceive;
When testing locally always enters into createReceive, on server it enters into active.
if there is only createReceive declared and not active it uses createReceive.
what logic works here, or where i can find docs for it. checked doc.akka.io but still not clear

答案1

得分: 1

Receive本身不执行任何操作:它只是围绕类模式匹配的Java粘合剂。您可以从createReceive()方法(演员初始模式匹配消息循环)中返回Receive,或者在getContext.become()调用中返回,这将使用新的消息循环替换当前活动的模式匹配消息循环。

上述内容在https://doc.akka.io/docs/akka/current/actors.html中有描述。

英文:

Receive by itself doesn't do anything : Its just Java glue around class pattern matching. You either return Receive from the createReceive() method (the actors initial pattern matching message loop), or in a getContext.become() call, which then replaces the current active pattern matching message loop with a new one.

The above is described https://doc.akka.io/docs/akka/current/actors.html

huangapple
  • 本文由 发表于 2020年9月22日 20:13:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64009505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定