Java。大气框架和CXF。无法暂停AtmosphereResource

时间:2019-06-02 17:32:12

标签: java asynchronous jax-rs cxf atmosphere

我在应用程序中使用cxf框架来实现rest服务。我正在尝试与气氛框架集成以进行任何异步操作。我正在研究下一种情况:

  1. 用户调用了某种休息方法(在我们的示例中为api/atmosphere/fetch)。通常的http请求,无需使用套接字。
  2. 其他服务暂停大气资源(在我们的示例中为r.suspend(30000L);
  3. Rest服务会启动任何异步操作(在我们的示例中为new Thread(new AsyncOperation(r.uuid())).start();。它可以通过某些MessageListener中的等待响应将消息发送到jms)
  4. 执行异步操作(resource.getResponse().write("Returning response after executing some asynchronous operation"); resource.resume(); resource.close();之后,向用户发送响应

但是这不适用于cxf(使用spring可以正常工作。下面的源代码)。期望用户释放资源后会收到响应,但实际上用户在fetch方法完成后会立即收到空响应。其余服务作为void cxf方法的标准结果返回204响应。是否可以在cxf-application中使用suspend对象的resume-AtmosphereResource方法?

我的源代码:

  1. AtmosphereCxfController.java
package controller;

import org.atmosphere.cpr.AtmosphereResource;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

@Path("api/atmosphere")
public class AtmosphereCxfController {

    @GET
    @Path("/fetch")
    public void fetch(@Context HttpServletRequest httpServletRequest) {
        AtmosphereResource r = (AtmosphereResource) httpServletRequest.getAttribute("org.atmosphere.cpr.AtmosphereResource");
        r.suspend(30000L);
        new Thread(new AsyncOperation(r.uuid())).start();
    }
}

  1. AsyncOperation.java
package controller;

import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceFactory;

import java.io.IOException;

//Imitation of long operation
public class AsyncOperation implements Runnable {
    private String uuid;

    public AsyncOperation(String uuid) {
        this.uuid = uuid;
    }

    public void run() {
        try {
            Thread.sleep(5000L); //imitation of executing some operation
        } catch (Exception e) {
            e.printStackTrace();
        }
        AtmosphereResource resource = AtmosphereResourceFactory.getDefault().find(uuid);
        resource.getResponse().write("Returning response after executing some asynchronous operation");
        resource.resume();
        try {
            resource.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  1. atmosphere.xml
<atmosphere-handlers>
    <!-- CXF -->
    <atmosphere-handler support-session="false"
                        context-root="/*"
                        class-name="org.atmosphere.handler.ReflectorServletProcessor">
        <property name="servletClassName"
                  value="org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet"/>
    </atmosphere-handler>
</atmosphere-handlers>
  1. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/all.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
        <init-param>
            <param-name>jaxrs.serviceClasses</param-name>
            <param-value>
                controller.AtmosphereCxfController
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>
  1. pom.xml的依存关系和属性:
   <properties>
        <springframework.version>3.2.18.RELEASE</springframework.version>
        <atmosphere.version>2.1.4</atmosphere.version>
        <cxf.version>2.6.0</cxf.version>
        <servlet.api.version>2.5</servlet.api.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.api.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.atmosphere</groupId>
            <artifactId>atmosphere-runtime</artifactId>
            <version>${atmosphere.version}</version>
        </dependency>

    </dependencies>
  1. all.xml春天情境
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                http://cxf.apache.org/jaxrs
                http://cxf.apache.org/schemas/jaxrs.xsd">

    <jaxrs:server id="apiEndpoint" address="/">
        <jaxrs:serviceBeans >
            <bean class="controller.AtmosphereCxfController" />
        </jaxrs:serviceBeans>
    </jaxrs:server>

</beans>

但是当我使用spring时,同样效果很好。我收到了来自异步操作的预期Returning response after executing some asynchronous operation响应:

  1. 弹簧支架控制器AtmosphereSpringController.java
package controller;

import org.atmosphere.cpr.AtmosphereResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/api/atmosphere")
public class AtmosphereSpringController {

    @RequestMapping(value = "/fetch", method = RequestMethod.GET)
    @ResponseBody
    public void fetch(HttpServletRequest httpServletRequest) {
        AtmosphereResource r = (AtmosphereResource) httpServletRequest.getAttribute("org.atmosphere.cpr.AtmosphereResource");
        r.suspend(30000L);
        new Thread(new AsyncOperation(r.uuid())).start();
    }
}
  1. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/all.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.atmosphere.cpr.MeteorServlet</servlet-class>
        <init-param>
            <param-name>org.atmosphere.servlet</param-name>
            <param-value>org.springframework.web.servlet.DispatcherServlet</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>
  1. spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
e
    <context:component-scan base-package="controller"/>
</beans>
  1. all-xml空上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
</beans>
  1. pom.xml的依存关系和属性
<properties>
        <springframework.version>4.3.8.RELEASE</springframework.version>
        <atmosphere.version>2.1.4</atmosphere.version>
        <servlet.api.version>2.5</servlet.api.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.atmosphere</groupId>
            <artifactId>atmosphere-runtime</artifactId>
            <version>${atmosphere.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.api.version}</version>
        </dependency>
    </dependencies>

</project>

0 个答案:

没有答案