使用 Java 和 JBoss 进行长轮询

发布于 2024-09-28 14:23:03 字数 537 浏览 0 评论 0原文

我正在寻找一个例子,如何在java中实现长轮询机制。我很想使用无状态 EJB。

我知道类似的东西会起作用:

@WebService(serviceName="mywebservice")
@Stateless
public class MyWebService {
    @WebMethod
    public String longPoll() {
         short ct = 0;
         while(someCondition == false && ct < 60) {
             sleep(1000);  // 1 sec
             ct++;
         }
         if (someCondition)
             return "got value";
         else
             return "";
    }
}

不幸的是我知道这无法扩展。我可以在不完成响应的情况下返回 webmethod 并在其他地方完成它吗?

I'm looking for an example, how to implement a longpoling mechanism in java. I would love to use a stateless EJB.

I know that something like that would work:

@WebService(serviceName="mywebservice")
@Stateless
public class MyWebService {
    @WebMethod
    public String longPoll() {
         short ct = 0;
         while(someCondition == false && ct < 60) {
             sleep(1000);  // 1 sec
             ct++;
         }
         if (someCondition)
             return "got value";
         else
             return "";
    }
}

Unfortunately i know that this does'nt scale. Can i return in the webmethod without finishing the response and finish it somewhere else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独守阴晴ぅ圆缺 2024-10-05 14:23:03

JAX-WS 支持使用异步客户端调用来调用 Web 服务,并支持回调和轮询模型。看一下:

JAX-WS provides support for invoking Web services using an asynchronous client invocation and supports both a callback and polling model. Have a look at:

所有深爱都是秘密 2024-10-05 14:23:03

您尝试实现的东西称为服务器推送
每个网络服务器/应用程序服务器都有一个线程池,例如 10 个用于处理 Web 请求的线程,如果所有这些线程都进入“睡眠”状态,则不会为其他 Web 请求提供服务,直到其中一个“睡眠”存在。一些解决方案是增加这些线程的数量,但随后您将消耗更多的内存和更多的操作系统资源(每个线程成本)。所以是的,您的“服务器推送”实现是不可扩展的。

解决方案:

  • 你的Web应用程序可以每(比如说)5秒发送一个http请求,检查你的“someCondition”是否改变,然后获取数据据
  • 我所知,Tomcat(所以JBoss也是如此)已经有一些“连接器”来支持此类请求,因此,使用实现 Servlet API 3 的最新 Web 服务器不需要 Thread.sleep() 或信号量
  • ,它还支持此类长时间运行的 HTTP 请求,
  • 了解更多信息:实现 Comet 的在线教程(服务器推送)

The thing you're trying to implement is called server push.
Each webserver/appserver has a pool of threads, say 10 threads for processing web requests, if all those threads will go into 'sleep' no other web request will be serviced until one of those 'sleeps' exists. Some solution is to increase number of those threads but then you'll eat more memory and more operating system resources (each thread costs). So yes, your implementation of 'server push' isn't scalable.

Solutions:

  • your web application can send a http request every (say) 5 secs, to check if your 'someCondition' changed, and then get the data
  • AFAIK, Tomcat (so JBoss too) already has some 'connector' for supporting such requests, so Thread.sleep() or semaphores won't be needed
  • use latest web server implementing Servlet API 3, it also has support for such long-running HTTP requests
  • read more: Online tutorials for implementing comets (server push)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文