Atmosphere框架,broadasterfactory . getdefault()替代



我正在使用Atmosphere框架,它看起来在最新版本的库方法:BroadcasterFactory.getDefault()被弃用了。(这种方法基本上到处都在使用,我找不到一个如何使用新"方式"的例子)

javadoc声明:

 @deprecated Use {@link org.atmosphere.cpr.AtmosphereConfig#resourcesFactory()}

然而,我找不到一个单一的文档如何获得AtmosphereConfig能够获得resourceFactory(这是一个实例方法)。

谁能告诉我如何得到配置…或AtmosphereFramework对象本身,我可以从中获得配置或任何例子,这是up2date ?

不确定它是否有效,但尝试获得ServletContext并使用getAttribute(AtmosphereFramework.class.getName())获得AtmosphereFramework。如果你正在使用Spring,尝试直接自动装配AtmosphereFramework。

您也可以从AtmosphereResource中获得BroadcasterFactory,然后查找Broadcaster,如:

private String path;
private BroadcasterFactory broadcasterFactory;
@Ready(value = Ready.DELIVER_TO.ALL)
public void onReady(final AtmosphereResource r) {
    System.out.println("onConnect");
    r.addEventListener(new AtmosphereConnectionController());
    if(broadcasterFactory == null){
        path = r.getBroadcaster().getID();
        broadcasterFactory = r.getAtmosphereConfig().getBroadcasterFactory();
    }
}
//later in code
broadcasterFactory.lookup(path).broadcast("message");

使用依赖注入。在我的项目中,它是这样的:

@MeteorService(path = "/recursos/fila-de-atendimento", interceptors = {AtmosphereResourceLifecycleInterceptor.class})
public class FilaDeAtendimentoResource extends HttpServlet {
    @Inject
    private BroadcasterFactory broadcasterFactory;
    ...
    /** Used for registering for a message */
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        ...
        Broadcaster broadcaster = broadcasterFactory.lookup(broadcasterId, true);
        meteor.setBroadcaster(broadcaster);
        ...
    }
}

相关内容

  • 没有找到相关文章

最新更新