1

我怎样才能在现有的休息资源中获得 AtmosphereResource ......
我试过这个:

大气.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>

我的休息资源

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

调用其余资源时,我将上述代码中的大气资源变量设为空。
我想我可能在 web.xml 或大气.xml 中做错了什么。
我想在我的应用程序的现有休息资源中使用 Atmosphere-runtime。
任何帮助是极大的赞赏。提前致谢。

4

1 回答 1

1

根据这个文档

有时需要在 Jersey 资源中注入 Atmosphere 组件。可以使用@Context、@PathParam、@QueryParam、@MatrixParam、@HeaderParam、@CookieParam、@FormParam 等注解注入以下组件

您可以注入:AtmosphereResource、Broadcaster、BroadcasterFactory

@Context AtmosphereResource resource
@PathParam
public String inject(@PathParam("test") Broadcaster broadcaster) {
  ...
}
于 2013-06-27T15:06:46.317 回答