[undertow-dev] blocking IO

Stuart Douglas sdouglas at redhat.com
Fri Jun 20 09:04:23 EDT 2014



Machiel Groeneveld wrote:
> I have a question about the blocking IO. In the documentation there is
> mention of the startBlocking call but not of spinning threads. I'm
> mentioning this because in the FileResource.java (a supplied handler in
> undertow) the file seems to be served from a new thread.

The file is served from a new thread as reading the file from disk can 
block.

>
> I currently have this:
>
> public void handleRequest(HttpServerExchange exchange) throws Exception {
>
> if (exchange.isInIoThread()) {
>
> exchange.dispatch(this);
>
> return;
>
> }
>
> exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
>
> exchange.startBlocking();
>
> OutputStream out = exchange.getOutputStream();
>
> writeToStream(out); //query database and write rows to out
>
> exchange.endExchange();
>
> }

This looks fine, but there is no guarantee that the response will be 
chunked. The OutputStream has a buffer, so if the response is smaller 
than the buffer then it will set a content-length header, otherwise it 
will use chunked encoding. If you want to force chunked encoded you can 
just call flush() on the stream or set the Transfer-Encoding header, 
although in general fixed length will perform better.

Stuart

>
>
> is this the way to correctly write a chunked http response?
>
> _______________________________________________
> undertow-dev mailing list
> undertow-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/undertow-dev


More information about the undertow-dev mailing list