简体   繁体   English

Java中的Akka Actor模型实现

[英]Akka Actor Model implementation in Java

Whats the difference between AbstractorActor and UntypedActor classes in Akka library. Akka库中的AbstractorActor和UntypedActor类之间有什么区别? Both the classes are used to create actors. 这两个类都用于创建演员。 But what separates them ?! 但是什么使它们分开呢?

They're separate APIs to create Actors in Java, UntypedActor is deprecated and has been superseded by the AbstractActor with the onset on Java 8 thanks to which's lambda expressions the receive function is much nicer to implement than in the old way. 它们是在Java中创建Actor的独立API,不赞成使用UntypedActor并已在Java 8上被AbstractActor取代,这要归功于lambda表达式,与以前的方法相比,receive函数的实现要好得多。

The old ( deprecated , since 2.5.0) way: 旧的(从2.5.0版本开始不推荐使用 ):

// this is DEPRECATED -----------vvvvvvvvvvvv
class My_OLD_STYLE_Actor extends UntypedActor {
  @Override
  public void onReceive(Object msg) {
    if (msg instanceof RequestMonies) {
      RequestMonies req = (RequestMonies) msg;
      // ...
    } else if (msg instanceof CheckStatus) {
      CheckStatus check = (CheckStatus) msg;
      // ...
    } else unhandled();
  }
}

And here's the same snippet using the proper current API: AbstractActor which is the current API (since 2.5.0): 这是使用正确的当前API的相同代码段: AbstractActor ,它是当前API(自2.5.0起):

public class JavaStyleActor extends AbstractActor {

  @Override
  public Receive createReceive() {
    return receiveBuilder()
      .match(RequestMonies.class, req -> {
        // handle request monies...
      })
      .match(CheckStatus.class, check -> {
        // ...
      })
      .build();
  }

}

You'll notice the need for casting is gone, and also gone is the need of making sure to call unhandled manually. 您会注意到铸造的需求已经消失了,确保手动调用未处理的需求也消失了。 Overall the new API is much easier to do the right thing. 总体而言,新API更容易执行正确的操作。 It will become even nicer in 2018 with the dawn of Akka Typed becoming production ready in early 2018 -- keep an eye on that. 随着Akka Typed在2018年初投入生产,它将在2018年变得更好-请密切注意这一点。

Overall, simply ignore the existence of UntypedActor if you're building new things using Akka with Java. 总体而言,如果您要使用Java的Akka来构建新事物,只需忽略UntypedActor的存在。 It's only still there for people who have built things using it in the past so that they can smoothly change to AbstractActor during 2.5's lifecycle. 过去只有使用过它的人才能使用它,以便他们可以在2.5的生命周期内平稳地转换为AbstractActor。

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

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