-
I haven't found the answer in official docs (https://www.playframework.com/documentation/3.0.x) so I'm asking here: When my app starts I do some requests for some data to external service: Simplified extract from code: public class StartModule extends AbstractModule {
@Override
protected void configure() {
bind(ApplicationStart.class).asEagerSingleton();
// other code
}
} @Singleton
public class ApplicationStart {
// private final .... (DI fields)
@Inject
public ApplicationStart(ApplicationLifecycle lifecycle, Environment environment, MyWsService wsService, MyUtils utils, (...) ) {
wsService.someRequest().thenAccept(jsonNode -> utils.putDataInCache());
(...)
}
} So it gets data and put it into cache for later user for clients. The problem: I suppose the solution is somewhere near ApplicationLoader (GuiceApplicationBuilder?) but I can't do fully working compliant/working code. So - is there any recommended solution to the problem? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
How are you deploying your service? If you are deploying on Kubernetes you can set up a readiness endpoint that returns a "not ready" status until the cache is populated. K8s will not route traffic to your instance until the readiness probe succeeds. |
Beta Was this translation helpful? Give feedback.
-
No, it is simply standalone - by running the script in the bin directory (https://www.playframework.com/documentation/3.0.x/Deploying#Using-the-dist-task) |
Beta Was this translation helpful? Give feedback.
-
Can you try to block initialization of your wsService.someRequest().thenAccept(jsonNode -> utils.putDataInCache())
.toCompletableFuture().get(); It's not a good solution, but can help in your case, because application will wait initialization your component before listen port and handle requests. PS: Also as an example of this approach you can see Play Flyway plugin. This plugin does database migration before start an application. It also use eager component with blocked initialization to make all migrations before handle user request. |
Beta Was this translation helpful? Give feedback.
-
Yes, it works. Thanks for replay and suggestions. |
Beta Was this translation helpful? Give feedback.
Can you try to block initialization of your
ApplicationStart
component?It's not a good solution, but can help in your case, because application will wait initialization your component before listen port and handle requests.
PS: Also as an example of this approach you can see Play Flyway plugin. This plugin does database migration before start an application. It also use eager component with blocked initialization to make all migrations before handle user request.