Java – how long does the HTTP connection remain open when we use the asynccontext mentioned in the servlet3 specification?
•
Java
How long does the HTTP connection remain open when we use the asynccontext mentioned in the servlet3 specification?
final AsyncContext asyncContext = httpServletRequest.startAsync(); asyncContext.setTimeout(0); asyncContexts.offer(asyncContext); .... .... new Thread(new Runnable() { @Override public void run() { try { final BufferedReader read = facade.getStreamData(); while (read.ready()) { httpServletResponse.setContentType("text/html"); if(i 100) { asyncContext.complete(); } if(Strings.isNullOrEmpty(read.readLine())) { continue; } asyncContext.getResponse().getWriter().print(read.readLine()); asyncContext.getResponse().flushBuffer(); i = i + 10; Thread.sleep(2000); } asyncContext.getResponse().getWriter().print("#" + 100); asyncContext.getResponse().flushBuffer(); asyncContext.complete(); } catch (IOException e) { throw new RuntimeException( "Error when writing the event.",e); } catch (InterruptedException e) { throw new RuntimeException( "Error when writing the event.",e); } } }).start();
This is work! And when the buffer is refreshed, the content is available on the client
My question is, how long will this connection remain open? And even if the title does not mention keep alive, how does the server manage it?
Solution
I finally got the answer Even if there is no keep alive header, the server will remain active by default The connection was open during that time Asynccontext continues to extend the connection until it is manually closed, or it will not send data to the client for more than the time it remains active (mentioned or default) Therefore, it basically behaves as a very slow network connection on the client
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码