简体   繁体   English

在HTTP Streaming servlet中为每个请求实现线程

[英]Implementing thread per request in HTTP Streaming servlet

I'm implementing HTTP Streaming servlet to deliver push notifications to client. 我正在实现HTTP流servlet,以将推送通知传递给客户端。 In that container there is also Jersey JAX-RS REST service. 在该容器中,还提供Jersey JAX-RS REST服务。

The problem is when I open the stream in two browser windows I have to wait until the other request is completed. 问题是,当我在两个浏览器窗口中打开流时,必须等待另一个请求完成。

I read the similar topic Servlet seems to handle multiple concurrent browser requests synchronously and opened the stream in two different browsers the stream and got it work. 我读过类似的主题Servlet似乎可以同步处理多个并发的浏览器请求,并在两个不同的浏览器中打开该流并使其正常工作。

And I tried to use AsyncContext, but it not stream, it allow to open browser tabs concurrently and at the end of execution thread delivers all the content to browser. 我尝试使用AsyncContext,但它不是流式传输,它允许同时打开浏览器选项卡,并且在执行线程结束时将所有内容传递给浏览器。

Also I tried to open concurrently the sample JSP page in two browser tabs and got similar result. 另外,我尝试同时在两个浏览器选项卡中打开示例JSP页面,并得到了相似的结果。

    <HTML>
    <HEAD>
       <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
       <meta http-equiv="Pragma" content="no-cache">
    </HEAD>
    <BODY BGCOLOR="blue" TEXT="white">
    <% 
      try {
        for (int i=1; i < 100; i++) {
           out.print("<h1>"+i+"</h1>");
           out.flush();

           try {
                Thread.sleep(1000);
           } catch (InterruptedException e) {
                out.print("<h1>"+e+"</h1>");
           }
         }
       } catch (Exception e) {
           out.print("<h1>"+e+"</h1>");
       }
       out.print("<h1>DONE</h1>");
    %>
    </BODY>
    </HTML>

As server I'm using Apache Tomcat 7.0. 作为服务器,我使用的是Apache Tomcat 7.0。 But use another servlet container wouldn't be a problem. 但是使用另一个servlet容器不会有问题。

Any pointers? 有指针吗?

Thanks 谢谢

Doing your own thread management from a servlet container is almost never a good idea. 从Servlet容器进行自己的线程管理几乎从来都不是一个好主意。

For server push in Tomcat see here: http://tomcat.apache.org/tomcat-7.0-doc/aio.html 有关在Tomcat中推送服务器的信息,请参见此处: http : //tomcat.apache.org/tomcat-7.0-doc/aio.html

Are you sure client-side polling wouldn't be easier, though? 您确定客户端轮询不会更容易吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM