我开始使用播放框架,我想编写一个进行一定数量 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 中调用时我有空指针异常( [error] adTaskInvocation - null java.lang.NullPointerException: null )我不太明白发生了什么事,但我需要我的工作来表现这种方式。
谢谢你的帮忙!