-
-
Notifications
You must be signed in to change notification settings - Fork 751
Getting BroadcasterFactory and AtmosphereResourceFactory with 2.2 and newer
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.
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.
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.
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.
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());
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";
}
}
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);
}
}
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();
}
}
In atmosphere-play, AtmosphereCoordinator
's framework
method returns AtmosphereFramework
.
AtmosphereFramework framework = AtmosphereCoordinator.instance.framework();
BroadcasterFactory broadcasterFactory = framework().getBroadcasterFactory();
AtmosphereResourceFactory atmosphereResourceFactory = framework().atmosphereFactory();
- Understanding Atmosphere
- Understanding @ManagedService
- Using javax.inject.Inject and javax.inject.PostConstruct annotation
- Understanding Atmosphere's Annotation
- Understanding AtmosphereResource
- Understanding AtmosphereHandler
- Understanding WebSocketHandler
- Understanding Broadcaster
- Understanding BroadcasterCache
- Understanding Meteor
- Understanding BroadcastFilter
- Understanding Atmosphere's Events Listeners
- Understanding AtmosphereInterceptor
- Configuring Atmosphere for Performance
- Understanding JavaScript functions
- Understanding AtmosphereResourceSession
- Improving Performance by using the PoolableBroadcasterFactory
- Using Atmosphere Jersey API
- Using Meteor API
- Using AtmosphereHandler API
- Using Socket.IO
- Using GWT
- Writing HTML5 Server-Sent Events
- Using STOMP protocol
- Streaming WebSocket messages
- Configuring Atmosphere's Classes Creation and Injection
- Using AtmosphereInterceptor to customize Atmosphere Framework
- Writing WebSocket sub protocol
- Configuring Atmosphere for the Cloud
- Injecting Atmosphere's Components in Jersey
- Sharing connection between Browser's windows and tabs
- Understanding AtmosphereResourceSession
- Manage installed services
- Server Side: javadoc API
- Server Side: atmosphere.xml and web.xml configuration
- Client Side: atmosphere.js API