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
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
这篇关于气氛/球衣双向对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:气氛/球衣双向对话
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01