Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java streaming rest API

I'm always confused about how to stream a response back to the client, for example a large csv file, and I'd like to ask some questions:

  1. I'm reading the file line by line and write directly to the output stream little by little with flush(), is this enough or it still depends on the flush() implementation on the specific OS?
  2. Suppose I have a very powerful machine and only flush at the end, can the client still process the answer in a stream way (without loading all the content into the memory)?
  3. The client mentioned the response may not be chunked, and it's about Chunked_transfer_encoding, so how does this impact the response? BTW I already know the response size when I send back the file.
  4. The client also talked about StreamingResponseBody. What I understand is this is only for asynchronous processing, we can stream data back without using it.
  5. Last question, does Reactive programming has anything to do with this? My user case is really simple, no concurrent request involved. I know there are a lot of modern frameworks in all the languages, but I'd prefer understand the basic stuff first.

Sorry about all the questions.

like image 827
grape_mao Avatar asked Aug 14 '26 12:08

grape_mao


1 Answers

  1. you use Java flush(), and it behaves identically on each O/S, even if O/S flush can differ.
  2. client sees only a stream of data which does not depend on how it was generated.

...

  1. Reactive programming has notthing in common with this. It is used to minimize memory consumption for large number of connections. If you have less than 1000 clients, use plain synchronous multithreading. It is easier to code and debug.
like image 71
Alexei Kaigorodov Avatar answered Aug 18 '26 05:08

Alexei Kaigorodov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!