Play framework - visualize a view when processing / redirecting after X seconds

Note: I am new to creating the framework.

For my game! project, I need some form of asynchronous programming. I just need to display the view, doing the processing in the background, and then redirect or a new rendered form.

This question was asked unanswered. I looked at the Play Documentation pages, I did not find any solution there.

What I tried:

I tried to change the AsynchController that is specified using the play starter example . However, when navigating to http://localhost/message function seems to act more like hibernation than a scheduled task that has been set and "forgotten", i.e. You can continue with further coding.

Fragment AsynchController: with its 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 

Goal:

My goal was for the Waiting 5 seconds... message to be displayed, followed by a delay of +/- 5 seconds. After that, it will reach the β€œfuture” (is that right?), As a result of which it will display the view (or redirect to the controller), in this case the verified page ( verified for the account).

I'm on the right track with my original goal, where can I get a good example of something like this?

+5
source share
1 answer

I have a simple solution, but you need to configure 2 entries in the route.

The first entry displays the message "Waiting 5 seconds ...", at the same time it gives an AJAX call to the second. The controller for the second record performs calculation / sleep mode for 5 seconds and returns the desired content, which will be displayed on the first page using Javascript.

On the first web page, you would add something like:

  axios.get(second-url) .then(function (response) { document.getElementById(someplaceholder).innerHTML=response.data. }) 
+1
source

Source: https://habr.com/ru/post/1269911/


All Articles