Java Spring:通过REST API实时更新客户端状态 [英] Java Spring: Real-time status update to the client over REST API

查看:128
本文介绍了Java Spring:通过REST API实时更新客户端状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Java Spring中开发一个Web应用程序,我希望用户能够从前端上传CSV文件,然后查看导入过程的实时进度,并且在导入之后,他应该能够从导入的数据中搜索单个条目.

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.

导入过程将包括实际上传文件(通过REST API POST请求发送),然后读取文件并将其内容保存到数据库,以便用户能够从该数据中进行搜索.

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.

如何显示此过程的实时进度?我找到了一个有关jQuery的教程,该教程显示了上载/传输的数据量的进度,但是由于大多数工作是在处理上载的文件时完成的,所以我想我想要一个解决方案,在行处理之前我先找出量文件中的行数,然后用户可以看到实时消息,例如:

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:

已处理的行:10000条中的1条

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.

无论哪种方式,问题是,将这些消息从Spring REST API发送到客户端的最简单方法是什么?

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

推荐答案

我自己找到了一个解决方案,并为此使用了Web套接字.

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

我从Spring文档中使用了这种方法:

I used this approach from the Spring documentation:

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

这可能有助于将每条处理过的行的消息发送到前端侦听器(在启动Web套接字主题/连接之后),但是我使用了另一种方法来进行数据导入,因此我使用了批量插入,因此无法用于我,但是网络套接字能够做到这一点.

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.

这篇关于Java Spring:通过REST API实时更新客户端状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆