Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我们使用servlet3规范中提到的AsyncContext时,http连接保持打开的时间有多长?_Java_Servlets_Long Polling_Servlet 3.0 - Fatal编程技术网

Java 当我们使用servlet3规范中提到的AsyncContext时,http连接保持打开的时间有多长?

Java 当我们使用servlet3规范中提到的AsyncContext时,http连接保持打开的时间有多长?,java,servlets,long-polling,servlet-3.0,Java,Servlets,Long Polling,Servlet 3.0,当我们使用servlet3规范中提到的AsyncContext时,http连接保持打开多久? 我的代码是 final AsyncContext asyncContext = httpServletRequest.startAsync(); asyncContext.setTimeout(0); asyncContexts.offer(asyncContext); .... .... new Thread(new Runnable() { @Override publ

当我们使用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标头,服务器也会维护默认的keep-alive。连接在该段时间内处于打开状态。AsyncContext将继续扩展连接,直到手动关闭连接或向客户端发送数据的时间超过保持活动(提及或默认)时间。因此,基本上它在客户端表现为一个非常慢的网络连接