Skip to content

Getting BroadcasterFactory and AtmosphereResourceFactory with 2.2 and newer

Jeanfrancois Arcand edited this page Sep 1, 2014 · 20 revisions

With the release of Atmosphere 2.2.0, the static getters BroadcasterFactory.getDefault() and AtmosphereResourceFactory.getDefault() have been deprecated. From now on you should get BroadcasterFactory and AtmosphereResourceFactory instances from an AtmosphereFramework instance instead, or from an AtmosphereResource and AtmosphereConfig directly:

  BroadcasterFactory f = atmosphereResource.getAtmosphereConfig()
                           .getBroadcasterFactory();

How to retrieve AtmosphereFramework depends on the underlying server or framework.

Servlet

If you've installed AtmosphereServlet by dynamic registration supported in Servlet 3, you can get AtmosphereFramework easily from AtmosphereServlet.

AtmosphereServlet servlet = servletContext.createServlet(AtmosphereServlet.class);
AtmosphereFramework framework = servlet.framework();

ServletRegistration.Dynamic reg = servletContext.addServlet("AtmosphereServlet", servlet);
reg.setAsyncSupported(true);

BroadcasterFactory broadcasterFactory = framework().getBroadcasterFactory();
AtmosphereResourceFactory atmosphereResourceFactory = framework().atmosphereFactory();

AtmosphereFramework is also stored as an application scoped attribute under the name of AtmosphereServlet's servlet name. This way is useful when using web.xml.

<servlet>
    <servlet-name>AtmosphereServlet</servlet-name>
    <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
    <async-supported>true</async-supported>
</servlet>
AtmosphereFramework framework = (AtmosphereFramework) servletContext.getAttribute("AtmosphereServlet");
BroadcasterFactory broadcasterFactory = framework().getBroadcasterFactory();
AtmosphereResourceFactory atmosphereResourceFactory = framework().atmosphereFactory();

Jersey

If you're using the atmosphere-jersey extension, you can have jersey inject BroadcasterFactory with a context parameter:

@Path("/endpoint")
public class AtmosphereEndpoint {

    @Suspend
    @GET
    public String suspend(@Context BroadcasterFactory broadcasterFactory) {
        return "SUSPEND";
    }
}

Guice

If you're using the atmosphere-guice extension, you can add the following Provider to a Guice Module:

@Provides
BroadcasterFactory provideBroadcasterFactory(AtmosphereGuiceServlet atmosphereGuiceServlet) {
    return atmosphereGuiceServlet.framework().getBroadcasterFactory();
}

@Provides
AtmosphereResourceFactory provideAtmosphereResourceFactory(AtmosphereGuiceServlet atmosphereGuiceServlet) {
    return atmosphereGuiceServlet.framework().atmosphereFactory();
}

After that it's possible to inject BroadcasterFactory and AtmosphereResourceFactory in any class created by Guice:

public class Sample {
    @Inject
    private BroadcasterFactory broadcasterFactory;
    @Inject
    private AtmosphereResourceFactory atmosphereResourceFactory;

    public void broadcast(String destination, String text) {
        broadcasterFactory.lookup(destination).broadcast(text);
    }

    public AtmosphereResource getAtmosphereResourceById(String id) {
        return atmosphereResourceFactory.find(id);
    }
}

Vert.x

In atmosphere-vertx, VertxAtmosphere's framework method returns AtmosphereFramework.

public class VertxChatServer extends Verticle {
    @Override
    public void start() throws Exception {
        VertxAtmosphere.Builder b = new VertxAtmosphere.Builder();
        // Some configurations for builder
        VertxAtmosphere vertxAtmosphere = b.build();
        AtmosphereFramework framework = vertxAtmosphere.framework();
        BroadcasterFactory broadcasterFactory = framework().getBroadcasterFactory();
        AtmosphereResourceFactory atmosphereResourceFactory = framework().atmosphereFactory();
    }
}

Play

In atmosphere-play, AtmosphereCoordinator's framework method returns AtmosphereFramework.

AtmosphereFramework framework = AtmosphereCoordinator.instance.framework();
BroadcasterFactory broadcasterFactory = framework().getBroadcasterFactory();
AtmosphereResourceFactory atmosphereResourceFactory = framework().atmosphereFactory();

Step by Step Tutorials

Concepts & Architecture

15 Minutes Tutorial

Advanced Topics

API

Known WebServer Issues

References

External Documentations

githalytics.com alpha

Clone this wiki locally