我尝试在我的游戏项目中使用此akka-quartz-scheduler。 我创建了两个akka演员,并为演员添加了配置:
akka {
quartz {
schedules {
HELLO_ACTOR {
description = "A cron job that fires off every 10 seconds"
expression = "0/10 0 0 ? * * *"
}
CLEANUP_ACTOR {
description = "A cron job that fires off every 10 seconds"
expression = "0/10 0 0 ? * * *"
}
}
}
}
我创建了模块并添加了调度器,以便在其中绑定actor和调度器:
protected void configure() {
bindActor(HelloActor.class, HelloActor.NAME);
bindActor(CleanupRunner.class, CleanupRunner.NAME);
bind(QuartzSchedulerHelper.class).asEagerSingleton();
bind(QuartzSchedulerExtension.class).toProvider(SchedulerJobInitializer.class);
}
private static class SchedulerJobInitializer implements Provider<QuartzSchedulerExtension> {
private final QuartzSchedulerExtension quartzSchedulerExtension;
@Inject
public SchedulerJobInitializer(ActorSystem actorSystem) {
this.quartzSchedulerExtension = new QuartzSchedulerExtension((ExtendedActorSystem) actorSystem);
}
@Override
public QuartzSchedulerExtension get() {
return quartzSchedulerExtension;
}
}
应用程序启动时,我在日志中看到参与者已添加到调度程序中:
2018-07-06 11:51:44,947 [INFO] from org.quartz.impl.DirectSchedulerFactory in play-dev-mode-akka.actor.default-dispatcher-2 - Quartz scheduler 'QuartzScheduler~application
2018-07-06 11:51:44,947 [INFO] from org.quartz.impl.DirectSchedulerFactory in play-dev-mode-akka.actor.default-dispatcher-2 - Quartz scheduler version: 2.2.3
2018-07-06 11:51:44,950 [INFO] from org.quartz.core.QuartzScheduler in play-dev-mode-akka.actor.default-dispatcher-2 - Scheduler QuartzScheduler~application_$_application started.
2018-07-06 11:51:44,974 [INFO] from com.typesafe.akka.extension.quartz.QuartzSchedulerExtension in application-akka.actor.default-dispatcher-2 - Setting up scheduled job 'HELLO_ACTOR', with 'com.typesafe.akka.extension.quartz.QuartzCronSchedule@6a6c7b3b'
2018-07-06 11:51:45,005 [INFO] from com.typesafe.akka.extension.quartz.QuartzSchedulerExtension in application-akka.actor.default-dispatcher-2 - Setting up scheduled job 'CLEANUP_ACTOR', with 'com.typesafe.akka.extension.quartz.QuartzCronSchedule@2c32247d'
应该每10秒触发一次幻觉。有什么原因不起作用,或者我应该在哪里添加?
答案 0 :(得分:1)
您仍然需要在代码中进行计划,以声明需要发送哪些消息以及向哪个参与者发送消息。在Scala
中,您可以这样做:
QuartzSchedulerExtension(context.system).schedule("HELLO_ACTOR", someActor, "Hello from the Quartz scheduler")
QuartzSchedulerExtension(context.system).schedule("CLEANUP_ACTOR", someOtherActor, "Clean up from the Quartz scheduler")
如果要停止其中之一,可以使用方法cancelJob
:
QuartzSchedulerExtension(context.system).cancelJob("HELLO_ACTOR")
答案 1 :(得分:1)
您的日志片段并不表示您的演员已在调度程序中注册。它确实表明Quartz作业已成功调度:在您的配置中,akka.quartz.schedules.HELLO_ACTOR
和akka.quartz.schedules.CLEANUP_ACTOR
是作业,而不是参与者,尽管您已将其命名。 (我建议重命名这些作业以避免混淆。)
考虑到您的QuartzSchedulerHelper
,看来演员没有在调度程序中注册:
List<String> actors = Arrays.asList(HelloActor.NAME, CleanupRunner.NAME);
for (String name : actors) {
ActorRef actor = registry.actorFor(name);
schedule(quartzSchedulerExtension, actor, name, RandomStringUtils.randomAlphabetic(10));
}
actorFor
以actor path作为参数。您要传递给该方法的是演员的名字。使用演员路径:
String actorPath = "/user/" + name
ActorRef actor = registry.actorFor(actorPath)
您可能还想尝试以下演员路径:
String actorPath = "akka://application/user/" + name