Continuous polling of output using spring ,rest an

2019-09-05 00:14发布

问题:

I am working on a web application the frontend of which is developed in angular js and spring mvc and which consumes the data from a RESTful webservice. There is a scenario wherein the REST webservice executes a tail command on a log file. Now this output should be streamed on the UI.Any pointers on this would be helpful.

回答1:

Solution 1

Maybe you want to take a look into WebSockets. The idea is, that you open a contineous connection between server and client for information exchange. This could be used for receiving log file updates from server.

The scenario could be something like this:

  1. User enters the log view page and thereby subscribes for getting log file updates
  2. In the server side code, where tail command is executed, an update is sent to all subscribers
  3. User receives new log content

-> Spring Websockets <-

Solution 2

Another solution of plain polling would be to use a javascript timer function for repeating requests to your log file. Something like this:

setTimeout(function(){ queryLogFile() }, 1000);

However, this will result in a high amount of requests, so maybe you should use some kind of caching mechanism for your log file.