简体   繁体   中英

Working with Akka AbstractActorWithTimers from Java API

I'm trying to use the new Akka Timers API inside a Java (well, Groovy, really) app, of course using the Akka/Java API (Gradle coordinates I'm using are 'com.typesafe.akka:akka-actor_2.11:2.5.4' ). When I try to create my new timer-laced actor, I get compiler errors:

class MyTimeAwareActor extends AbstractActorWithTimers {
    @Override
    void onReceive(Object message) {
        if(message in StartNewTimer) {
            timers.startPeriodicTimer(/* blah whatever */)
        }
    }
}

This code yields a compiler error on my use of the @Override annotation:

" Groovy:Method 'onReceive' from class com.example.myapp.MyTimeAwareActor does not override method from its superclass or interfaces but is annotated with @Override. "

This tells me I'm using this actor incorrectly. All I want to do is start a new timer (that does something) every time the actor receives a StartNewTimer message.

Any ideas how I can do that and fix this compiler error?

Replace onReceive with createReceive (the following is in Java; you can convert it to Groovy):

@Override
public Receive createReceive() {
    return receiveBuilder()
        .match(StartNewTimer.class, message -> {
            getTimers().startPeriodicTimer(/* blah whatever */);
        })
        .build();
}

onReceive is not defined in AbstractActorWithTimers or its parent, AbstractActor ( onReceive is defined in UntypedAbstractActor ).

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