How to inform user of updates on a batch progress in a Spring Boot + Angular web app?

34 Views Asked by At

My app has a Spring Boot back-end and an Angular front-end that calls the back-end via REST endpoints.

I want to allow the user to launch a batch process on the server that involves making numerous API calls to external systems, processing data, and loading a database. This will take probably several minutes to run. So, I want the user to see a screen that shows progressive messages, eg.:

Requesting index from external web service ... received. 100 items to query.

Making 100 API calls for Widget data ... 100 requests made ... 95 successful, 5 aborted.

Processing results ... complete.

Writing to database ... completed.

How can I accomplish this with Spring Boot and Angular? I believe that with Spring Boot everything is triggered by a HTTP request to a URL, and it all has to run (and be completed) before data is sent back to the caller (the Angular front-end in this case). So, should I design an Angular front-end that orchestrates everything and makes all these dozens or hundreds of requests to the back-end?

Or: Is there a way in Spring Boot to create an endpoint that sends back data multiple times, during the batch process rather than at the end of it, so I can simply program the front-end to update what the user sees? That seems to me the cleanest way to code it, and the proper separation of responsibilities between back-end and front-end.

1

There are 1 best solutions below

0
Gijs Den Hollander On

I would not build als this in the frond end, the heavy lifting should be done in the backend. You could run in all kind of issuis, browser slowness, bandwith issui for your user, data security..

I see that Spring boot has some polling abillities, but this does not seem to include incremental polling results (as far as I saw, maybe i missed something). See sping polling

I would do something along the following lines.

Backend

  • You create an endpoint which can start the batch process. The endpoint returns some unique id, which you can use to later inquire of the state of the batch process.
  • When the batch process start, you start another thread in the you API to handle all the work the batch process needs to do. The state of that batch job you update to a cache / DB depending on your requirements.
  • You create an endpoint where you can retrieve the state of the batch job via its Id. The endpoints checks cache / db for the state of the batch job.

Front end

Create a page which can start the batch job. And query its state in some interval, your requirements (every second or by reuqest of your user?). And create some progress depending on your requirement and state of the batch. This call could be polling call, that only receives answer if the state hase changed (but this really depends on your requirements).