bumblebee
bumblebee

Reputation: 1841

Akka Actor Model implementation in Java

Whats the difference between AbstractorActor and UntypedActor classes in Akka library. Both the classes are used to create actors. But what separates them ?!

Upvotes: 1

Views: 375

Answers (1)

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.

The old (deprecated, since 2.5.0) way:

// 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):

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. It will become even nicer in 2018 with the dawn of Akka Typed becoming production ready in early 2018 -- keep an eye on that.

Overall, simply ignore the existence of UntypedActor if you're building new things using Akka with Java. 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.

Upvotes: 5

Related Questions