Getting BroadcasterFactory and AtmosphereResourceFactory with 2.2 and newer - Atmosphere/atmosphere Wiki

With the release of Atmosphere 2.2.0, the static getters BroadcasterFactory.getDefault() and AtmosphereResourceFactory.getDefault() were deprecated, and from 2.3+ they are completely removed.

Using 2.3+

Starting with 2.3.0 you can inject the factories using the javax.inject.Inject annotation:

@Inject
private BroadcasterFactory factory;

The list of injectable objects can be found here.

You can also use any of the techinques described below, and if you are crazy and know what you are doing, you can use the static getters in Universe.

Using 2.2+

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.

Using AtmosphereHandler

If you are using AtmosphereHandler, you can also implement the AtmosphereServletProcesor interface and retrieve the BroadcasterFactory from the AtmosphereConfig

void init(AtmosphereConfig config) throws ServletException;

method, which will be invoked when the handler is getting created.

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>
ServletContext servletContext = ServletContextFactory.getDefault().getServletContext();
AtmosphereFramework framework = (AtmosphereFramework) servletContext.getAttribute("AtmosphereServlet");
BroadcasterFactory broadcasterFactory = framework().getBroadcasterFactory();
AtmosphereResourceFactory atmosphereResourceFactory = framework().atmosphereFactory();

UPDATE (2017-03-02): The AtmosphereFramework does not appear to be stored as a servlet context attribute as described above. However, I was able to find an attribute containing the BroadcasterFactory:

ServletContext servletContext = ServletContextFactory.getDefault().getServletContext();
BroadcasterFactory broadcasterFactory = (BroadcasterFactory) servletContext.getAttribute(BroadcasterFactory.class.getName());

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();