使用Java和JBoss进行长轮询 [英] Long Polling with Java and JBoss

查看:147
本文介绍了使用Java和JBoss进行长轮询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 "";
    }
}

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

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

推荐答案

你想要实现的是称为服务器推送
每个网络服务器/应用服务器都有一个线程池,比如用于处理网络请求的10个线程,如果所有这些线程都进入休眠状态,那么在其中一个休眠存在之前,其他任何网络请求都不会得到服务。一些解决方案是增加这些线程的数量,但随后您将占用更多内存和更多操作系统资源(每个线程成本)。所以是的,你的'服务器推送'的实现是不可扩展的。

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.

解决方案:


  • 您的网络应用程序可以每隔5秒发送一次http请求,检查您的'someCondition'是否已更改,然后获取数据

  • AFAIK,Tomcat(所以JBoss已经有一些连接器用于支持这样的请求,因此不需要Thread.sleep()或信号量

  • 使用最新的Web服务器实现Servlet API 3,它也有支持对于如此长时间运行的HTTP请求

  • 了解更多信息:实施彗星的在线教程(服务器推送)

  • 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)

这篇关于使用Java和JBoss进行长轮询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆