如何在现有的rest资源中获得atmosphererresource…
我试了一下:
atmosphere.xml
<atmosphere-handlers>
<atmosphere-handler context-root="/*"
class-name="org.atmosphere.handler.ReflectorServletProcessor">
<property name="servletClass"
value="com.sun.jersey.spi.container.servlet.ServletContainer" />
</atmosphere-handler>
</atmosphere-handlers>
web . xml
<servlet>
<servlet-name>jaxrs</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.atmos.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jaxrs</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<description>AtmosphereServlet</description>
<servlet-name>AtmosphereServlet</servlet-name>
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
<init-param>
<param-name>org.atmosphere.cpr.Broadcaster.supportOutOfOrderBroadcast</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.broadcasterCacheClass</param-name>
<param-value>org.atmosphere.cache.UUIDBroadcasterCache</param-value>
</init-param>
<!-- Reduce memory usage by sharing ExecutorServices -->
<init-param>
<param-name>org.atmosphere.cpr.broadcaster.shareableThreadPool</param-name>
<param-value>true</param-value>
</init-param>
<!-- Automatically free Broadcaster objects when the last client on the
Broadcaster's channel disconnects -->
<init-param>
<param-name>org.atmosphere.cpr.broadcasterLifeCyclePolicy</param-name>
<param-value>EMPTY_DESTROY</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.broadcaster.maxProcessingThreads</param-name>
<param-value>50</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.broadcaster.maxAsyncWriteThreads</param-name>
<param-value>50</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>AtmosphereServlet</servlet-name>
<!-- Any mapping -->
<url-pattern>/*</url-pattern>
</servlet-mapping>
My Rest Resource
@GET
@Path("/receive")
public Response receive() {
AtmosphereResource atmosphereResource = (AtmosphereResource) req.getAttribute("org.atmosphere.cpr.AtmosphereResource"); //Here the atmosphereResource is null
// suspend the response
atmosphereResource.suspend(timeout, TimeUnit.MINUTES);
return Response.ok().build();
}
在调用其余资源时,我将上述代码中的atmosphereResource变量作为null。
我想我可能在web.xml或atmosphere.xml中做错了什么。
我想在我的应用程序中现有的休息资源中使用Atmosphere-runtime。
非常感谢任何帮助。
根据这个文档:
有时需要在Jersey资源中注入Atmosphere组件。下面的组件可以通过使用@Context、@PathParam、@QueryParam、@MatrixParam、@HeaderParam、@CookieParam、@FormParam等注释来注入
你可以注入:atmosphererresource, broadcast, broadasterfactory
@Context AtmosphereResource resource
@PathParam
public String inject(@PathParam("test") Broadcaster broadcaster) {
...
}