Nidhin Joy
Nidhin Joy

Reputation: 51

Long polling in spring mvc(async)

My operation takes 30 mins to process which is invoked by a rest call request. i want to give the client an immediate response telling operation in progress,and processing should happen in another thread, what is the best way to crack this out,Is deferred result the only way.

Upvotes: 1

Views: 1056

Answers (3)

Coder
Coder

Reputation: 7076

Use a message queue (ActiveMQ, Redis).

  1. Send request from client.
  2. Controller gets request, post process/message in message queue.
  3. Send response back to client saying it's processing.
  4. Another thread to look for changes/new process in message queue.
  5. Execute the process - Update the status in message queue each step is completed. - (started/running/completed/failed).
  6. You can show the status of process everytime with the id of process in queue.

Upvotes: 1

francesco foresti
francesco foresti

Reputation: 2043

Since you are providing rest services, another approach could be to immediately return 'Accepted' (202) or 'Created' (201) to the client and provide a link to another service that would provide updates about the progress status of the processing. This way the client is free to decide whether to poll the server for updates, or just provide the user an 'update status' button.

Upvotes: 1

Amila
Amila

Reputation: 5213

30 minutes is a long time. I'd suggest you using websockets to push progress updates and operation status.

Upvotes: 1

Related Questions