2017-09-28 3 views
0

私は再生フレームワークを使い始めています。私は特定の数のws呼び出しを行うジョブを書きたいと思っています。再生フレームワークの呼び出しスケジュールされた非同期タスク内のWS

私は次のように2つのクラスを書いた:

@Singleton 
public class AutoGateJob { 

@Inject 
private ActorSystem actorSystem; 
@Inject 
private ExecutionContext executionContext; 
@Inject 
private RgsDataServiceServices rgsDataServiceServices; 

@Inject 
public AutoGateJob(ApplicationLifecycle lifecycle, ActorSystem system, ExecutionContext 
    context) { 
    Logger.info("### create autojob"); 
    this.actorSystem = system; 
    this.executionContext = context; 

    this.initialize(); 

    lifecycle.addStopHook(() -> { 
     Logger.info("### c'est l'heure de rentrer!!!"); 
     this.actorSystem.shutdown(); 
     return CompletableFuture.completedFuture(null); 
    }); 
} 

private void initialize() { 
    this.actorSystem.scheduler().schedule(
     Duration.create(0, TimeUnit.SECONDS), // initialDelay 
     Duration.create(5, TimeUnit.SECONDS), // interval 
     () -> this.runAutoGateJobTasks(), 
     this.executionContext 
    ); 
} 

private void runAutoGateJobTasks() { 
    Logger.info("## Run job every 5 seconds"); 
    rgsDataServiceServices = new RgsDataServiceServices(); 

    rgsDataServiceServices.findAllPaymentToSendHandler() 
    .handle((wsResponse, throwable) -> { 
     Logger.info("## find all payment to send response: ", wsResponse.asJson()); 
     List<String> paymentsList = new ArrayList<>(); 
     paymentsList.forEach(s -> { 
      Logger.info("## Processing payment: ", s); 
     }); 
     return CompletableFuture.completedFuture(null); 
    }); 
} 

}

public class AutoGateJobInitializer extends AbstractModule { 

@Override 
protected void configure() { 
    Logger.info("## Setup job on app startup!"); 
    bind(AutoGateJob.class).asEagerSingleton(); 
} 

}

を、問題がある: はMys rgsDataServiceServicesが働く作業WSClient注射を持っていますコントローラと一緒に使用するとうまくいくが、呼び出されたときAutoGateJobに私は、nullポインタ例外 ( [エラー] a.d.TaskInvocation - nullの のjava.lang.NullPointerException:ヌル )持って を私は本当に何が起こっているのか理解していないが、私はこのように動作するために私の仕事を必要としています。

ありがとうございました!

答えて

0

すべての依存関係をコンストラクタに挿入する必要があります。あなたがやっているのは、コンストラクタにいくつかのものを注入するだけです。それから、すべての依存関係を使用しようとするタスクをすぐにスケジュールしますが、Guiceがすべての依存関係を注入する前にそのタスクが実行されます。あなたはすなわち、唯一のコンストラクタ・インジェクションを使用して、すべての依存関係が利用できるようにしたい場合:

private final ActorSystem actorSystem; 
private final ExecutionContext executionContext; 
private final RgsDataServiceServices rgsDataServiceServices; 

@Inject 
public AutoGateJob(ActorSystem system, ExecutionContext context, RgsDataServiceServices rgsDataServiceServices) { 
    Logger.info("### create autojob"); 
    this.actorSystem = system; 
    this.executionContext = context; 
    this.rgsDataServiceServices = rgsDataServiceServices 

    this.initialize(); 
} 

また、あなたがプレイ、俳優のシステムをシャットダウンするライフサイクルフックを追加すべきではない、既にへのライフサイクルフックを登録しますそれを行う。必要に応じて、スケジュールされたタスクをキャンセルするライフサイクルフックを登録できますが、アクターシステムがシャットダウンしたときに自動的に取り消されるため、本当に必要とは思われません。

関連する問題