Amit Hanoch Amit Hanoch - 1 year ago 97
Java Question

Java play two responses from one request

I have a request to update data in my server.
When i update i need to run a very heavy calculation that takes some time.

I want to do this:
1. Send request to update from client
2. Get the request in server and start a new thread to do the calculation, then update the object in my database and return a response to the client that the update is complete.
3. When the calculation ends - send another response to the client informing him that the calculation ended.

That way we have 2 appovals to the client.

Answer Source

In your routes you should have 2 entries :

  • one route that does the update to the database (Lets call it Controller.updateTheDatabase())
  • one route that does the heavy calculation (Lets call it Controller.doHeavyCalculation())

(without context it's hard to give good names)

Then you define each one of those functions in your Controller,

  1. doHeavyCalculation: calls your calculation Service asynchronously
  2. updateTheDatabase: sends a database update asynchronously

Once you have this, you can call your API in your template, and you use the 2 responses separately(I do with with AJAX JavaScript) to update the information messages.

I use PlayFramework with Scala so sorry if I cannot help with chunks of code but I ran into problems similar to yours.

Here is the documentation for Asynchronous requests

EDIT : After reading the question again I changed my approach