使用公开为 @WebService 的基于 SOAP 的 @Stateless EJB 实现长轮询
我有一个预先存在的基于 SOAP 的 Web 服务,我想用它提供一个基于长轮询的通知系统。我怎样才能实现这个?该客户端当前是 Java 桌面富客户端,必须从其他连接的客户端接收更新。服务器是GlassFish 3.01。我有一个基本的、阻塞的@WebMethod,但我遇到了由于使用它而导致的问题。下面是一些伪代码,显示了 Web 方法的想法:
@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 "";
}
}
在客户端,我使用 Future 对象异步调用它:
public Future<?> requestLongPollAsync(Date lastUpdate,
AsyncHandler<LongPollResponse> handler) {
try {
return mywebservice.longPollAsync(getXMLGregorianCalendar(lastUpdate),
handler);
}
// ...
}
客户端似乎工作正常。然而,我有两个由此产生的问题,似乎是由于 Web 服务调用的持久性所致:
- 每个请求者都使用一个活动的 http 侦听器,因此这是不可扩展的,
- 当客户端断开连接时,GlassFish 会抛出异常(SSL 异常) ,因为所有调用都必须通过安全 SSL 侦听器(默认情况下为 http-listener-2))。
我需要使用 com.sun.grizzly.comet.CometEngine 吗? EJB 3.1 @Asynchronous 注释在这里有什么作用吗?我发现的所有示例都依赖于 Servlet API、AJAX 和其他不适用的技术。谢谢。
I have a pre-existing SOAP-based web service with which I'd like to provide a long-polling based notification system. How can I implement this? The client is currently a Java desktop rich client which must receive updates from other connected clients. The server is GlassFish 3.01. I had a basic, blocking @WebMethod but I had problems resulting from its use. Here's some pseudo-code showing the idea of the web method:
@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 "";
}
}
And on the client-side, I am calling this asynchronously, using the Future object:
public Future<?> requestLongPollAsync(Date lastUpdate,
AsyncHandler<LongPollResponse> handler) {
try {
return mywebservice.longPollAsync(getXMLGregorianCalendar(lastUpdate),
handler);
}
// ...
}
The client side seems to work okay. However, I have 2 problems that arose from this, seemingly due to the long lasting nature of the web service call:
- Each requester uses an active http listener, so this is not scalable,
- When the client disconnects, GlassFish throws an exception (SSL exception, as all calls must go through the secure SSL listener (by default, http-listener-2)).
Do I need to use com.sun.grizzly.comet.CometEngine? Does the EJB 3.1 @Asynchronous annotation do anything here? All examples I've found rely on the Servlet API, AJAX, and other technologies which are not applicable. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑使用 JAX-WS 2.0 异步编程模型(在您的情况下很可能是回调客户端)。以下是一些资源:
Consider using JAX-WS 2.0 asynchronous programming model (and most likely a callback client in your case). Here are some resources: