Atmosphere/Jersey bidirectional conversation(气氛/球衣双向对话)
问题描述
我见过许多 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
InputStreamwill 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
这篇关于气氛/球衣双向对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:气氛/球衣双向对话
基础教程推荐
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 多个组件的复杂布局 2022-01-01
