title: Jetty chunked transfer encoding without keep-alive
body: <p>I'm streaming data over HTTP using Jetty as a server. The data comes from a message queue, and I'm handling the processing asynchronously, printing messages to the connection when it is ready and there are messages available. When the client disconnects I stop consuming messages from the queue and clean up all resources dedicated to the stream.</p>
  
  <p>Jetty chooses send the response chunked and sets <code>Transfer-Encoding: chunked</code> by default – this is what I want, since this is an endless stream and I obviously can't set a <code>Content-Length</code> header.</p>
  
  <p>However, I also need to set <code>Connection: close</code> on the response. The server will run behind a load balancer that will try to keep a persistent connection to the backend servers, unless they explicitly send <code>Connection: close</code>. I have no way to configure the load balancer, it is completely out of my hands. If the load balancer keeps the connection open I have no way to know when to stop consuming from the message queue, because the connection will remain open.</p>
  
  <p>The problem is that when I do <code>response.setHeader("Connection", "close")</code> Jetty stops rendering the response as chunked. It also does not set a <code>Content-Length</code> header, it just streams the output to the connection. As far as I understand this is not OK in HTTP, even though many clients probably will handle it. I would really like to use chunked transfer encoding, and also disable keep-alive. How can I convince Jetty of this?</p>
  
  <p>Here is a minimal example that shows what I do. If I remove the line that sets the <code>Connection</code> header Jetty chunks the response, but with it it does not.</p>
  
  <pre><code>public class StreamingServer {
    public static void main(String[] args) throws Exception {
      Server server = new Server(2000);
      server.setHandler(new AbstractHandler() {
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
          response.setBufferSize(1024);
          // if I remove this line I get Transfer-Encoding: chunked
          response.setHeader("Connection", "close");
          response.flushBuffer();
          AsyncContext asyncContext = request.startAsync();
          asyncContext.setTimeout(0);
          final ServletOutputStream out = response.getOutputStream();
  
          // start consuming messages from the message queue here
  
          out.setWriteListener(new WriteListener() {
            public void onError(Throwable t) {
              // stop consuming messages and clean up resources
            }
  
            public void onWritePossible() throws IOException {
              while (out.isReady()) {
                // send the next available message from the queue
                out.print(...);
              }
            }
          });
        }
      });
      server.start();
      server.join();
    }
  }
  </code></pre>