气氛/球衣双向对话
我见过许多 Atmosphere 示例,包括 pub-sub.我想做一些类似 pub-sub 的事情(客户端订阅该客户端唯一的频道;服务器定期发布到该频道),除了客户端也会向服务器发送数据.客户端将发送数据以响应服务器发送的数据,并且在其他情况下,当客户端发生服务器需要知道的重要事件(服务器不需要确认)时.
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).
甚至可以用 Atmosphere 做到这一点吗?
Is it even possible to do this with 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 的目的.
- 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.
这两个问题都可以通过简单地删除 @Suspend
来解决,但是我处于每个连接线程的情况.
Both of these problems could be solved simply by removing @Suspend
, but then I'm in the thread-per-connection situation.
我觉得 Atmosphere 在这里不是合适的技术,也许我可能需要做一些更低级别的事情.但我不知道该怎么做.想法?
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?
无论如何我都找不到一种直接的异步解析 XML 的方法,所以整个事情看起来不像可以异步完成的事情.
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.
推荐答案
只需广播一个 Callable 即可执行异步 XML 解析.看看这个样本:
Just broadcast a Callable to execute your asynchronous XML parsing. Take a look at this sample:
TwitterFeed
相关文章