【问题标题】:When we use AsyncContext mentioned in servlet3 specification, how long the http connection remains open?当我们使用 servlet3 规范中提到的 AsyncContext 时,http 连接保持打开多长时间?
【发布时间】:2016-05-26 05:47:08
【问题描述】:

当我们使用 servlet3 规范中提到的 AsyncContext 时,http 连接保持打开多长时间? 我的一段代码是

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();

它正在工作!并且随着缓冲区被刷新,内容在客户端可用。

我的问题是,此连接保持打开多长时间?以及即使标题中没有提到keep-alive,服务器如何管理它?

【问题讨论】:

    标签: java servlets long-polling servlet-3.0


    【解决方案1】:

    我终于得到了答案。 即使不存在 keep-alive 标头,服务器也会保持默认的 keep-alive。连接在该时间段内处于打开状态。 AsyncContext 继续扩展连接,直到它被手动关闭或者它没有向客户端发送数据超过保持活动(提及或默认)时间。因此,基本上它在客户端表现为非常慢的网络连接。

    【讨论】:

      猜你喜欢
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 2019-05-17
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      相关资源
      最近更新 更多