加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 当我们使用servlet3规范中提到的AsyncContext时,http连

发布时间:2020-12-15 02:13:06 所属栏目:Java 来源:网络整理
导读:当我们使用servlet3规范中提到的AsyncContext时,http连接保持打开多长时间? 我的代码是 final AsyncContext asyncContext = httpServletRequest.startAsync();asyncContext.setTimeout(0);asyncContexts.offer(asyncContext);........ new Thread(new Runna
当我们使用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,服务器如何管理它?

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读