2017-01-15 49 views
0

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

我在我的游戏应用程序中更名为演员系统,所以我现在如何获得演员系统。

play.akka.actor-system = "myAkka" 

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

回答

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

} 

定义辛格尔顿吉斯模块急于如下:

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

+0

也检查https://www.playframework.com/documentation/2.5.x/ScalaAkka#dependency-injecting-actors – LuisKarlos

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

演员生命周期处理程序创建和演员,并取消其在应用程序停止:

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

你可以阅读更多here

相关问题