smeeb
smeeb

Reputation: 29497

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?

Upvotes: 1

Views: 689

Answers (1)

Jeffrey Chung
Jeffrey Chung

Reputation: 19507

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).

Upvotes: 2

Related Questions