尝试暂停异步请求的连接时出现Jersey错误 [英] Jersey error when trying to suspend a connection of an asynchronous request

查看:90
本文介绍了尝试暂停异步请求的连接时出现Jersey错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个小测试,以暂停使用jersey发出的请求,但出现错误.让我向您展示我正在尝试做什么,以及如何使服务器启动并运行,只是此请求失败了:

Im doing a small test on suspending a asynch request using jersey but im getting an error. Let me show you what im trying to do and by the way the server is up and running just this request is failing:

Path("/json/metallica")
public class JSONService {

    @GET
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    public void getAsynchHealthyTracks(@Suspended AsyncResponse asyncResponse){

        HealthService.getInstance().getHealththyTracks(asyncResponse);
    }

}

当我从本地主机在tomcat上调用此命令时,出现以下错误:

when i call this from my local host on tomcat i get the following error:

 HTTP Status 500 - javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.

type Exception report

message javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:391)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
root cause

javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:312)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:103)
    org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

我的web.xml文件中已经有<async-supported>true</async-supported>,这是web.xml文件的内容:

I have already the <async-supported>true</async-supported> in my web.xml file and here is the contents of the web.xml file:

 <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>


    <servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.example.ema,com.fasterxml.jackson.jaxrs.json</param-value>
            <async-supported>true</async-supported>
            <load-on-startup>1</load-on-startup>
        </init-param>

        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>

    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>



</web-app>

健康服务本身只是构建一个pojo对象,并应将其作为json发送回去.无论如何,这是服务:

the health service itself just builds a pojo object and should send it back as a json. Anyway, here is the service:

public class HealthService {
private static HealthService instance = null;
protected HealthService() {
    // Exists only to defeat instantiation.
}
public static HealthService getInstance() {
    if(instance == null) {
        instance = new HealthService();
    }
    return instance;
}

public void getHealthyTracks(AsyncResponse asyncResponse){


    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }


    Track track = new Track();
    track.setSinger("micheal jackson");
    track.setTitle("thriller");

    asyncResponse.resume(track);
}

}

我设置了一个断点,甚至没有达到最初的球衣调用.服务器只是停止.我的最终目标是睡觉几秒钟,然后发送回响应.

i put a break point and it does not even reach the initial jersey call. the server just stops. My end goal is just to sleep for a few seconds and then send back a response.

更新:这是现在的web.xml的照片:

UPDATE: Here is a photo of the web.xml as it is now:

推荐答案

我发现了问题所在.标签"true"表示不应放在init params部分中.应该将其放在web.xml文件中的servlet标记内.我的web.xml现在看起来像这样:

I figured out the issue. the tag "true" should NOT be put in the init params section. It should be put just inside the servlet tag in the web.xml file. My web.xml now looks like this:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <display-name>Archetype Created Web Application</display-name>


    <servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.example.ema,com.fasterxml.jackson.jaxrs.json</param-value>

            <load-on-startup>1</load-on-startup>
        </init-param>

        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>

    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>



</web-app>

这篇关于尝试暂停异步请求的连接时出现Jersey错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆