休息资源内的大气资源

时间:2013-06-18 15:34:17

标签: websocket jersey jax-rs long-polling atmosphere

如何在现有的休息资源中获取AtmosphereResource ...
我试过这个:

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>

我的休息资源

@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中做错了。
我想在我的应用程序中的现有rest资源中使用Atmosphere-runtime。
任何帮助是极大的赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:1)

根据doc

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

您可以注入:AtmosphereResource,Broadcaster,BroadcasterFactory

@Context AtmosphereResource resource
@PathParam
public String inject(@PathParam("test") Broadcaster broadcaster) {
  ...
}