本文介绍了Atmosphere / Jersey双向对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多大气的例子,包括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
    }
}

但是这里有一些问题:


  1. 传入连接将暂停,因此它不会能够向服务器发送任何东西,除非通过广播恢复;

  2. 任何使用 InputStream 都会导致阻塞I / O哪种方式违背了使用Atmosphere的目的。

  1. The incoming connection will suspend, so it won't be able to send anything to the server except when resumed via broadcast;
  2. 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.

推荐答案

Salut,

只是广播一个Callable来执行异步XML解析。看一下这个样本:

just broadcast a Callable to execute your asynchronous XML parsing. Take a look at this sample:

让我知道它是怎么回事(无论是在这里还是在atmosphere-framework@googlegroups.com上)

Let me know how it goes (either here or on atmosphere-framework@googlegroups.com)

谢谢

- Jeanfrancois

-- Jeanfrancois

这篇关于Atmosphere / Jersey双向对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:46
查看更多