2017-01-15 5 views
0

Ich möchte meine Schauspieler innerhalb einer Play-App einrichten, zum Beispiel habe ich einen Schauspieler, der eine Nachrichtenwarteschlange abfragt oder alle x Minuten abläuft.Wo finde ich meine Schauspieler in einer Play-App?

Ich benannte das Actor-System in meiner Play-App um, so wie ich jetzt das Actor-System bekomme.

play.akka.actor-system = "myAkka" 

Ich weiß, dass ich den Schauspieler System bekommen kann innerhalb eines Controllers Dependency Injection verwenden, aber ich brauche es nicht auf Controller-Ebene, muss ich dies tun, wenn meine Anwendung außerhalb des Request/Response-Ebene beginnt .

Antwort

0

Einer der Weg ist es, Ihre Schauspieler in einem eifrigen Singleton zu kisten.

erstellen Singletons wie folgt aus:

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

} 

definieren die Singleton als eifrig in guice Modul wie folgt:

package com.app; 

import com.google.inject.AbstractModule; 

public class AppModule extends AbstractModule { 

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

} 

Siehe https://www.playframework.com/documentation/2.5.x/JavaDependencyInjection#Eager-bindings für weitere Einzelheiten.

Registrieren Sie Ihr Modul mit Play (fügen Sie die folgende Zeile in application.conf):

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

Siehe https://www.playframework.com/documentation/2.5.x/JavaDependencyInjection#Programmatic-bindings für weitere Einzelheiten.

+0

überprüfen Sie auch https://www.playframework.com/documentation/2.5.x/ScalaAkka#dependency-injecting-actors – LuisKarlos

0

Hier ist ein grundlegendes Beispiel für eine geplante Actor-Implementierung.

Die Schauspieler Pläne einige regelmäßige Arbeit:

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

Der Schauspieler Life-Cycle-Handler erzeugt und Schauspieler und hebt es auf Antrag stop:

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

Die StartupHandler als Singleton eingespritzt wird; er empfängt im Konstruktor einen Schauspieler Monitor und startet Polling:

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

Die StartupHandler zur Injektion von Standardmodul Play-Modul registriert ist:

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

Sie können mehr here lesen.