Scala 在play应用程序中在何处设置我的演员?
我想在play应用程序中设置我的演员,例如,我有一个演员将轮询消息队列或每x分钟运行一次 我在我的play应用程序中重命名了演员系统,因此我现在知道如何获得演员系统Scala 在play应用程序中在何处设置我的演员?,scala,playframework,akka,Scala,Playframework,Akka,我想在play应用程序中设置我的演员,例如,我有一个演员将轮询消息队列或每x分钟运行一次 我在我的play应用程序中重命名了演员系统,因此我现在知道如何获得演员系统 play.akka.actor-system = "myAkka" 我知道我可以在控制器内部使用依赖项注入来获得actor系统,但我不需要在控制器级别使用它,我需要在应用程序在请求/响应级别之外启动时执行此操作。其中一种方法是将您的actor打包在一个急切的单例中 创建如下单例: package com.app; import
play.akka.actor-system = "myAkka"
我知道我可以在控制器内部使用依赖项注入来获得actor系统,但我不需要在控制器级别使用它,我需要在应用程序在请求/响应级别之外启动时执行此操作。其中一种方法是将您的actor打包在一个急切的单例中 创建如下单例:
package com.app;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class ActorBootstrap {
private ActorRef somaActor;
@Inject
public ActorBootstrap(ActorSystem system) {
// Craete actors here: somaActor = system.actorOf()
}
public ActorRef getSomaActor() {
return somaActor;
}
}
将singleton定义为guice模块中的eager,如下所示:
package com.app;
import com.google.inject.AbstractModule;
public class AppModule extends AbstractModule {
@Override
protected void configure() {
bind(ActorBootstrap.class).asEagerSingleton();
}
}
有关详细信息,请参阅
向Play注册您的模块(将以下行添加到application.conf):
有关详细信息,请参阅。以下是计划参与者实现的基本示例 参与者安排一些定期工作:
public class ScheduledActor extends AbstractActor {
// Protocol
private static final String CANCEL = "cancel";
private static final String TICK = "tick";
// The first polling in 30 sec after the start
private static final int TICK_INTERVAL_SEC = 90;
private static final int TICK_INTERVAL_SEC = 90;
private Cancellable scheduler;
public static Props props() {
return Props.create(ScheduledActor.class, ()->new ScheduledActor());
}
public ScheduledActor() {
receive(ReceiveBuilder
.matchEquals(TICK, m -> onTick())
.matchEquals(CANCEL, this::cancelTick)
.matchAny(this::unhandled)
.build());
}
@Override
public void preStart() throws Exception {
getContext().system().scheduler().scheduleOnce(
Duration.create(ON_START_POLL_INTERVAL, TimeUnit.SECONDS),
self(),
TICK,
getContext().dispatcher(),
null);
}
@Override
public void postRestart(Throwable reason) throws Exception {
// No call to preStart
}
private void onTick() {
// do here the periodic stuff
...
getContext().system().scheduler().scheduleOnce(
Duration.create(TICK_INTERVAL_SEC, TimeUnit.SECONDS),
self(),
TICK,
getContext().dispatcher(),
null);
}
public void cancelTick(String string) {
if (scheduler != null) {
scheduler.cancel();
}
}
}
actor生命周期处理程序在应用程序停止时创建和取消actor:
public class ScheduledActorMonitor {
private ActorRef scheduler;
private ActorSystem system;
@Inject
public ScheduledActorMonitor(ActorSystem system, ApplicationLifecycle lifeCycle) {
this.system = system;
initStopHook(lifeCycle);
}
public void startPolling() {
scheduler = system.actorOf(ScheduledActor.props();
}
public void cancelTick() {
if (scheduler != null) {
scheduler.tell(HelloScheduler.CANCEL, null);
}
}
private void initStopHook(ApplicationLifecycle lifeCycle) {
lifeCycle.addStopHook(() -> {
cancelTick();
return CompletableFuture.completedFuture(null);
});
}
}
StartupHandler作为单例注入;它在构造函数中接收参与者监视器并开始轮询:
@Singleton
public class StartupHandler {
@Inject
public StartupHandler(final ScheduledActorMonitor schedularMonitor) {
schedularMonitor.startPolling();
}
}
StartupHandler由默认的模块播放模块注册为注入:
public class Module extends AbstractModule {
@Override
public void configure() {
bind(StartupHandler.class).asEagerSingleton();
}
}
你可以读更多 也检查
public class Module extends AbstractModule {
@Override
public void configure() {
bind(StartupHandler.class).asEagerSingleton();
}
}