Pregunta

I'm writing HTTP webservice that may take quite long to produce results. I use embedded Jetty 8.1.5 and JAX-RS (Apache CXF)

I decided to go with some kind of control protocol: when new request comes, I start a long-running job in a separate thread and periodically write to HttpOutputStream lines with the current status ("CONTROL_MESSAGE: 42% done")

The problem is that Jetty uses chunk encoding here, so my status messages are buffered and are useless as they all can be buffered in a single chunk, providing no progress for a client.

I can't use Content-Length property as I don't know end result length. HttpOutputStream.flush() doesn't work as Jetty uses internal buffers.

As I see it, I need a way to tell Jetty "please finish current chunk and flush it", but don't know how.

¿Fue útil?

Solución

Actually I'm not sure your problem is chunking. If I get you right you are writing the progress to the same stream where the result will finally go?

this can be done with or without chunking simply by calling flushBuffers on the response.

However, it then depends on your connection and your browser if this makes it into the client. A transparent proxy may aggregate the content and not flush until the response is complete or its own buffer is full. The browser might not act on any content until the response is complete or its buffer is full.

In some implementations of comet that use this technique, you have to send 512 byes of white space after the pushed message.

You'd be better off using something like server sent events, websocket or long polling to send progress to the client..... better yet - just use cometd.org, which will select the best transport for the client available.

Otros consejos

You should be able to disable chunking by disabling the persistent connection itself by adding a Connection: close header to the response, it is that or knowing the Content-Length ahead of time. Or I suppose you could just use HTTP/1.0 as well. I'll open a bug with jetty documentation to get this better documented.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top