本文介绍了Atmosphere + Jersey:我如何拥有多个广播公司?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作的Jersey / Atmosphere / Guice应用程序,它有两个Atmosphere Resources。第一个是示例聊天应用程序的克隆:

I have a working Jersey/Atmosphere/Guice application which has two Atmosphere Resources. The first is pretty much a clone of the example chat application:

@Path("/chat")
@AtmosphereService(broadcaster = JerseyBroadcaster.class, path = "/chat")
public class ChatResource {

    @Suspend(contentType = "application/json")
    @GET
    public String suspend() {
       return "";
    }

    @Broadcast(writeEntity = false)
    @POST
    @Produces("application/json")
    public Response broadcast(Message message) {
        return new Response(message.author, message.message);
    }
}

第二个是将发送的测试通知资源服务器端事件:

The second is a test notification resource which will be sent server-side events:

@Path("/notifications")
@AtmosphereService(broadcaster = JerseyBroadcaster.class, path = "/notifications")
public class NotificationsResource {

    @Suspend(contentType = "application/json")
    @GET
    public String suspend() {
       return "";
    }
}

所有内容都正确连线并且工作正常。但是为了让我发送服务器端事件,我发出:

Everything is wired up correctly and works fine. However in order for me to send a server side event I issue:

MetaBroadcaster.getDefault().broadcastTo("/*", new Response(...));

显然,这会将广播消息发送到两个资源。我想要做的是仅将服务器端事件发送到通知资源:

Clearly, this will send the broadcast message to both resources. What I want to do is send the server side events only to the notifications resource:

MetaBroadcaster.getDefault().broadcastTo("/notifications", new NotificationResponse(...));

但是,这不起作用。我总是收到以下错误:

However, that doesn't work. I always receive the following error:

org.atmosphere.cpr.MetaBroadcaster - No Broadcaster matches /notifications.

这是因为只有一家广播公司注册; JerseyBroadcaster上的/ *。

That's because there is only one broadcaster registered; the JerseyBroadcaster on /*.

问题是:如何使这两个资源拥有不同ID /名称的不同广播公司?

The question is: how do I make it so that these two resources have different broadcasters with different IDs/Names?

推荐答案

在资源中,使用你想要的频道暂停(查询的'true'参数()强制频道为如果它不存在则创建):

In the resource, suspend using the channel you want (the 'true' parameter to lookup() forces the channel to be created if it doesn't exist):

@Suspend( contentType = MediaType.APPLICATION_JSON, period = MAX_SUSPEND_MSEC )
@GET
public Broadcastable suspend( @Context final BroadcasterFactory factory )
{
    return new Broadcastable( factory.lookup( MY_CHANNEL, true ) );
}

在其他代码中,可以在任何地方播放,向该频道广播:

In the other code, which can be pretty much anywhere, broadcast to that channel:

Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup( MY_CHANNEL );
if( broadcaster != null ) {
    broadcaster.broadcast( message );
}

如果你要从资源方法广播,你可以注释它改为(如ChatResource的broadcast()方法所示)。

If you're going to be broadcasting from a resource method, you can annotate it instead (as shown in ChatResource's broadcast() method).

这篇关于Atmosphere + Jersey:我如何拥有多个广播公司?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:46
查看更多