在Play应用程序中设置演员的位置?

时间:2017-01-15 18:30:07

标签: scala playframework akka

我想在播放应用中设置我的演员,例如,我有一个演员,它将轮询一个消息队列或每隔x分钟运行一次。

我在我的播放应用程序中重命名了演员系统,所以我现在如何获得演员系统。

play.akka.actor-system = "myAkka"

我知道我可以在控制器内部使用依赖注入来获取actor系统,但是我不需要在控制器级别使用它,当我的应用程序在请求/响应级别之外启动时,我需要这样做。 / p>

2 个答案:

答案 0 :(得分:0)

其中一种方法是以热切的单身人士为你的演员创作。

像这样创建单身:

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;
    }

}

在guice模块中将单例定义为eager,如下所示:

package com.app;

import com.google.inject.AbstractModule;

public class AppModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(ActorBootstrap.class).asEagerSingleton();
    }

}

有关详细信息,请参阅https://www.playframework.com/documentation/2.5.x/JavaDependencyInjection#Eager-bindings

使用Play注册模块(将以下行添加到application.conf):

play.modules.enabled += "com.app.AppModule"

有关详细信息,请参阅https://www.playframework.com/documentation/2.5.x/JavaDependencyInjection#Programmatic-bindings

答案 1 :(得分:0)

以下是计划的actor实现的基本示例。

演员安排一些定期工作:

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作为单例注入;它在构造函数中接收一个actor监视器并开始轮询:

@Singleton  
public class StartupHandler {  
    @Inject  
    public StartupHandler(final ScheduledActorMonitor schedularMonitor) {  
        schedularMonitor.startPolling();  
    }  
}

StartupHandler由默认模块Play Module注册为注入:

public class Module extends AbstractModule {  
    @Override  
    public void configure() {  
        bind(StartupHandler.class).asEagerSingleton();  
    }  
} 

您可以阅读更多here