Atmosphere/Jersey bidirectional conversation

风格不统一 提交于 2019-12-03 22:24:58

问题


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:

  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.

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.


回答1:


Salut,

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

TwitterFeed

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

Thanks

-- Jeanfrancois



来源:https://stackoverflow.com/questions/8230682/atmosphere-jersey-bidirectional-conversation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!