使用 Java 和 JBoss 进行长轮询
我正在寻找一个例子,如何在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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:
您尝试实现的东西称为服务器推送。
每个网络服务器/应用程序服务器都有一个线程池,例如 10 个用于处理 Web 请求的线程,如果所有这些线程都进入“睡眠”状态,则不会为其他 Web 请求提供服务,直到其中一个“睡眠”存在。一些解决方案是增加这些线程的数量,但随后您将消耗更多的内存和更多的操作系统资源(每个线程成本)。所以是的,您的“服务器推送”实现是不可扩展的。
解决方案:
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: