简体   繁体   中英

Akka Java API - how to extend AbstractActor with logging AND with stash?

In Scala I see that you can write something like this:

class MyActor extends Actor with ActorLogging with Stash

In the Java API I see the following classes to extend

AbstractActor
AbstractLoggingActor
AbstractActorWithStash

Is there a way to create the Java equivalent of the Scala line above? Thanks!

class MyActor extends Actor with ActorLogging with Stash

Actor , ActorLogging , and Stash here are traits. In Scala, a class can extend a trait and mix in additional traits.

AbstractActor
AbstractLoggingActor
AbstractActorWithStash

The above classes in Akka's Java API are abstract classes. In Java, a class cannot extend more than one abstract class. Therefore, one cannot define in Java the syntactic equivalent of the MyActor class.

However, one can easily define the effective equivalent of MyActor in Java. AbstractLoggingActor is a convenient means to obtain a LoggingAdapter in an actor; the alternative to using AbstractLoggingActor is simply to manually create a LoggingAdapter . The following defines an actor that has both logging and stashing with the Java API:

import akka.actor.*;
import akka.event.Logging;
import akka.event.LoggingAdapter;

class MyActorInJava extends AbstractActorWithStash {
  LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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