Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to stream a response over HTTP with netty
    primarykey
    data
    text
    <p>I'm using Netty 3.6.6 and I'd like to send a large response back to the caller. I can't copy the response body into a ChannelBuffer as in some cases it will be very large.</p> <p>I'm migrating a server from CXF to Netty, previously, I could just use the OutputStream provided by CXF to write the data. </p> <p>I originally tried to just send the response without content, and then continued to write data to the Channel in a series of 8k buffers. This failed as the client seemed to get the original response and see no data and complain. I tried setting the response as chunked, but this didnt seem to make a difference, nor did setting the chunked header, the client always saw an empty stream.</p> <p>I saw the file server example for 3.6.6, and that's similar to what I want to do, except the data will not be a file. I saw the ChunkedStream &amp; NioStream, which seemed close to what I need, except they take InputStream/ReadableByteChannel whereas I have an OutputStream; I could try using the PipedInput &amp; OutputStreams, but that seems like it would introduce an unfortunate bottleneck.</p> <p>I'm sure there's a way to stream a lot of data back to the client in response to a request, but I'm just not seeing how to do it unless I have a file.</p> <p>I am also curious how you let the client know the response is complete if the connection is keep-alive, and you're streaming content, but don't know the content length. Seems like the client would wait forever for the connection to close in those cases.</p> <p>Modifying the static file server example from 3.6.6 to remove the content-length header (just comment it out), specify that its a chunked response</p> <pre><code> response.setChunked(true); response.setHeader(Names.TRANSFER_ENCODING, Values.CHUNKED); </code></pre> <p>and then using <code>ChunkedNioStream</code> to send the file after writing the response:</p> <pre><code> // Write the initial line and the header. ch.write(response); final ReadableByteChannel aIn = java.nio.channels.Channels.newChannel(new FileInputStream(file)); ChannelFuture writeFuture = ch.write(new ChunkedNioStream(aIn)); </code></pre> <p>Yields the undesired behavior, the client gets a couple hundred bytes and then stop receiving, basically what I'm seeing in my application. The correct thing only seems to happen with a content-length, which is not feasible in my use case.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload