note: I am new to play framework
For my Play! project, I require some form of asynchronous programming. Simply, I need to display a view, whilst doing processing in the background, followed by a redirect or a new form being rendered.
This question has been asked with no response. I have had a look on the Play Documentation pages, I didn't find any solution there.
What I have tried:
I attempted to modify the AsynchController that is given with the play starter example. However when navigating to http://localhost/message , the function seemed to act more as a sleep instead of a scheduled task which is set and "forgotten" about, i.e. one can continue on with further coding.
AsynchController snippet: with own modification
public CompletionStage<Result> message() {
return getFutureMessage(5, TimeUnit.SECONDS).thenApplyAsync(s -> ok(views.html.User.Account.verified.render()), exec);
}
private CompletionStage<String> getFutureMessage(long time, TimeUnit timeUnit) {
CompletableFuture<String> future = new CompletableFuture<>();
actorSystem.scheduler().scheduleOnce(
Duration.create(time, timeUnit),
() -> future.complete("Waiting 5 seconds..."),
exec
);
return future;
}
route entry
GET /message controllers.AsyncController.message
Purpose:
My aim here was for the message Waiting 5 seconds... to be displayed, followed by a +/- 5s delay. Thereafter it would reach the "future" (is this correct?) whereby it would render a view (or redirect to a controller), in this case the verified page ( for account verified).
Am I on the right track with my original goal, where can I get a good example of something similar?