Java Spring: Real-time status update to the client

2020-06-12 05:53发布

I am developing a web application in Java Spring where I want the user to be able to upload a CSV file from the front-end and then see the real-time progress of the importing process and after importing he should be able to search individual entries from the imported data.

The importing process would consist of actually uploading the file (sending it via REST API POST request) and then reading it and saving its contents to a database so the user would be able to search from this data.

How could I show the real-time progress of this process? I found a tutorial for jQuery, which shows the progress of amount of data uploaded/transferred, but as the most the work is done while processing the uploaded file, I thought I would like a solution where before the line processing I find out the amount of lines in the file and then the user could see a live message like:

Lines processed: 1 out of 10000

It could update/change incrementally, but as one line is processed pretty quickly, showing each number of lines processed is not that important.

Either way, the question is, what's the easiest way to send these messages from Spring REST API to the client?

1条回答
We Are One
2楼-- · 2020-06-12 06:47

I found a solution myself and used Web Sockets for that.

I used this approach from the Spring documentation:

https://spring.io/guides/gs/messaging-stomp-websocket/

It could help on sending the messages for each processed line to the front end listener (after the web socket topic/connection is started) but I used a different approach for the data import, I used batch insert so that was unavailable for me, but web sockets are capable of doing that.

查看更多
登录 后发表回答