gpt4 book ai didi

Java Akka 调度 API 抛出 MissingMethodException

转载 作者:行者123 更新时间:2023-12-02 12:15:04 24 4
gpt4 key购买 nike

Java 8(好吧,Groovy)在这里使用 Akka com.typesafe.akka:akka-actor_2.11:2.5.4。我正在尝试创建一个参与者 (AppMaster),它创建/拥有/管理另一个参与者 (StateChecker),并计划发送此 StateChecker每 4 秒发送一条 CheckState 消息:

class CheckState {}

class StateChecker extends AbstractActor {
// For this question, it really doesn't matter what this actor does
}

class AppMaster extends AbstractActor {
ActorRef stateChecker

@Override
Receive createReceive() {
receiveBuilder()
.match(Init, { message ->
stateChecker = context.actorOf(Props.create(StateChecker, "yes", 20), "state-checker")

FiniteDuration startDur = Duration.Zero()

FiniteDuration freqDur = Duration.create(4000, TimeUnit.MILLISECONDS)
CheckState checkState = new CheckState()
ExecutionContextExecutor executor = context.system().dispatcher()

context.system().scheduler().schedule(startDur, freqDur, stateChecker, checkState,
executor)
})
.build()
}
}

当我运行此程序时,出现以下异常:

groovy.lang.MissingMethodException: No signature of method: akka.actor.LightArrayRevolverScheduler.schedule() is applicable for argument types: (scala.concurrent.duration.FiniteDuration, scala.concurrent.duration.FiniteDuration, akka.actor.LocalActorRef, com.me.myapp.messages.CheckState, akka.dispatch.Dispatcher) values: [0 days, 4000 milliseconds, Actor[akka://myapp/user/myapp-master/o1-keeper#-1837899613], ...]
Possible solutions: schedule(scala.concurrent.duration.FiniteDuration, scala.concurrent.duration.FiniteDuration, java.lang.Runnable, scala.concurrent.ExecutionContext), schedule(scala.concurrent.duration.FiniteDuration, scala.concurrent.duration.FiniteDuration, scala.Function0, scala.concurrent.ExecutionContext), schedule(scala.concurrent.duration.FiniteDuration, scala.concurrent.duration.FiniteDuration, akka.actor.ActorRef, java.lang.Object, scala.concurrent.ExecutionContext, akka.actor.ActorRef)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)

我添加了日志语句,并且在调度期间抛出异常:

context.system().scheduler().schedule(startDur, freqDur, stateChecker, checkState,
executor)

知道我需要做什么来解决这个问题吗?由于这是我在使用 Akka 的 Java API 时遇到的问题,因此我确实需要通过 Java API 获得答案。

最佳答案

问题在于您传递给 schedule() 方法的参数与该方法的任何重载版本都不匹配。您可以在 JavaDoc 中看到 schedule() 的重载版本的签名。 .

您尝试使用的版本如下 method :

Cancellable schedule(scala.concurrent.duration.FiniteDuration initialDelay,
scala.concurrent.duration.FiniteDuration interval,
ActorRef receiver,
java.lang.Object message,
scala.concurrent.ExecutionContext executor,
ActorRef sender)

您缺少最后一个参数,它是一个 ActorRef,被指定为计划消息的发件人。如果 StateChecker 不关心谁向其发送 CheckState 消息,那么最简单的解决方法是传入 null 作为最后一个参数:

context.system()
.scheduler()
.schedule(startDur, freqDur, stateChecker, checkState, executor, null)
// ^

关于Java Akka 调度 API 抛出 MissingMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46253298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com