气氛/泽西岛双向对话
我见过许多 Atmosphere 示例,包括 pub-sub。我想做类似 pub-sub 的事情(客户端订阅该客户端唯一的通道;服务器定期发布到该通道),只不过客户端也会将数据发送到服务器。客户端将发送数据以响应服务器发送的数据,以及在其他情况下,当客户端发生服务器需要了解的重要事件(服务器不需要确认)时。
甚至可以用 Atmosphere 来做到这一点吗?
它可能看起来像这样:
@Stateless
@Path("/id/{clientId}/key/{clientKey}")
public class MyService {
@POST
@Produces("application/xml")
@Consumes("application/xml")
@Suspend
public StreamingOutput subscribe(@PathParam("clientId") String clientId,
@PathParam("clientKey") String clientKey,
@Context Broadcaster broadcaster,
InputStream body) {
if (!authenticate(clientId, clientKey) {
throw new WebApplicationException(401);
}
broadcaster.setID(clientId);
// Do something here... Not sure what
}
}
但这里有几个问题:
- 传入连接将挂起,因此除非通过广播恢复,否则它将无法向服务器发送任何内容;
- 任何
InputStream
的使用都会导致 I/O 阻塞,这违背了使用 Atmosphere 的目的。
这两个问题都可以通过删除 @Suspend 来解决,但随后我就处于每个连接一个线程的情况。
我感觉 Atmosphere 在这里不适合使用,也许我可能需要做一些级别较低的事情。但我不知道该怎么做。有想法吗?
编辑:
无论如何,我找不到异步解析 XML 的直接方法,因此整个事情看起来不太像可以异步完成的事情。
I've seen a number of Atmosphere examples including pub-sub. I want to do something like pub-sub (client subscribes to a channel that is unique to that client; server periodically publishes to that channel), except that the client will send data to the server as well. The client will send data in response to data sent by the server and in other cases when something important happens on the client that the server needs to know about (which the server doesn't need to acknowledge).
Is it even possible to do this with Atmosphere?
It might look something like this:
@Stateless
@Path("/id/{clientId}/key/{clientKey}")
public class MyService {
@POST
@Produces("application/xml")
@Consumes("application/xml")
@Suspend
public StreamingOutput subscribe(@PathParam("clientId") String clientId,
@PathParam("clientKey") String clientKey,
@Context Broadcaster broadcaster,
InputStream body) {
if (!authenticate(clientId, clientKey) {
throw new WebApplicationException(401);
}
broadcaster.setID(clientId);
// Do something here... Not sure what
}
}
But there are a couple of problems here:
- The incoming connection will suspend, so it won't be able to send anything to the server except when resumed via broadcast;
- Any usage of the
InputStream
will result in blocking I/O, which kind of defeats the purpose of using Atmosphere.
Both of these problems could be solved simply by removing @Suspend
, but then I'm in the thread-per-connection situation.
I get the feeling that Atmosphere isn't going to be the appropriate technology here and perhaps I might have to do something a bit lower level. But I'm not sure how to go about it. Ideas?
Edit:
I can't find a straightforward way of parsing XML asynchronously anyway, so this whole thing is looking less like something that can be done asynchronously.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需广播 Callable 即可执行异步 XML 解析。看看这个示例:
TwitterFeed
Just broadcast a Callable to execute your asynchronous XML parsing. Take a look at this sample:
TwitterFeed