Search code examples
javaplayframeworkjobs

Play: How to render before and after a job


I have a Play app that compiles some code behind the scenes and I was wondering what the best strategy was to first render a "please wait..." message before the job starts and then a "job complete" message once it is finished.

It's not something that can be chunked, it's either done or not, so I'm guessing without using websockets the only way is to poll with ajax?

thanks!


Solution

  • When executing a Job (using .now()), it returns a Promise object. Using this, you can use the play await function and ajax (long polling) or web sockets.

    So, you would do the following

    1. display the please wait page
    2. make your ajax call that executes the job, and awaits for the job to complete
    3. when your ajax returns you simply update the page, whether that is a javascript update to a div, or a redirect to a new page.