Atmosphere/Jersey bidirectional conversation

2019-02-15 20:51发布

问题:

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