异步进程在javax.ws.rs.*(REST Client)中不起作用。

huangapple go评论147阅读模式
英文:

Asynchronous Process not Working in javax.ws.rs.*(REST Client)

问题

I demonstrate asynchronous process in REST API using javax(2.26 version). When I tried I got "A filter or servlet of the current chain does not support asynchronous operations." I don't know what to do with that. I tried many ways but doesn't work out.

The below is the resource file
***Resource.java***
```import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.MediaType;

import com.jersey.rest.User;

@Path("/async")
public class AsynDemo {
	
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public void asyncMethod(@Suspended final AsyncResponse response, User user) {
		new Thread() {
			public void run() {
				response.resume(true);
			}
		}.start();
	}

}

Pom.xml

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jersey</groupId>
    <artifactId>rest</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>rest</name>

    <build>
        <finalName>rest</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>2.26</version>
        </dependency>
        
         <dependency>
             <groupId>javax.activation</groupId>
             <artifactId>activation</artifactId>
             <version>1.1.1</version>
         </dependency>
        <!-- uncomment this to get JSON support-->
        <dependency>
		    <groupId>org.glassfish.jersey.media</groupId>
		    <artifactId>jersey-media-json-jackson</artifactId>
		</dependency>
        
        <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
		<dependency>
		    <groupId>javax.xml.bind</groupId>
		    <artifactId>jaxb-api</artifactId>
		    <version>2.3.0</version>
		</dependency>
		
		<dependency> 
	       <groupId>com.google.code.gson</groupId> 
	       <artifactId>gson</artifactId> 
	       <version>2.6.2</version> 
	   </dependency>
	   
		<dependency>
		    <groupId>org.glassfish.jersey.security</groupId>
		    <artifactId>oauth2-client</artifactId>
		</dependency>
		
		<dependency>
		    <groupId>com.auth0</groupId>
		    <artifactId>java-jwt</artifactId>
		    <version>3.10.3</version>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
		<dependency>
		    <groupId>com.fasterxml.jackson.core</groupId>
		    <artifactId>jackson-annotations</artifactId>
		    <version>2.11.2</version>
		</dependency>
				
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
		    <groupId>com.fasterxml.jackson.core</groupId>
		    <artifactId>jackson-databind</artifactId>
		    <version>2.11.0</version>
		</dependency>
				
    </dependencies>
    <properties>
        <jersey.version>2.26-b03</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

Client checking java file

import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import com.jersey.rest.User;

public class MainTesting {
	public static void main(String[] args) {
	
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target("http://localhost:9090/rest/jwt/async"); 
		AsyncInvoker invoker = target.request().async();
		
		Future<Boolean> response = invoker.post(Entity.entity(new User(), MediaType.APPLICATION_JSON), Boolean.class);
		try {
			System.out.println(response.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
	}

}

APPLICATION ERROR

SEVERE: Servlet.service() for servlet [Jersey jwt Application] in context with path [/rest] threw exception [java.lang.UnsupportedOperationException: Asynchronous processing not supported on Servlet 2.x container.] with root cause
java.lang.UnsupportedOperationException: Asynchronous processing not supported on Servlet 2.x container.
	at org.glassfish.jersey.servlet.WebComponent$3.suspend(WebComponent.java:137)
	at org.glassfish.jersey.servlet.internal.ResponseWriter.suspend(ResponseWriter.java:125)
	at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.suspend(ServerRuntime.java:921)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:326)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
	at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:337)
	at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
	at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
	at org.glassfish.jer

<details>
<summary>英文:</summary>

I demonstrate asynchronous process in REST API using javax(2.26 version). When I tried I got&quot;A filter or servlet of the current chain does not support asynchronous operations.&quot; I don&#39;t know what to do with that. I tried many ways but doesn&#39;t work out.

The below is the resource file
***Resource.java***
```import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.MediaType;

import com.jersey.rest.User;

@Path(&quot;/async&quot;)
public class AsynDemo {
	
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public void asyncMethod(@Suspended final AsyncResponse response, User user) {
		new Thread() {
			public void run() {
				response.resume(true);
			}
		}.start();
	}

}

Pom.xml

         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;

    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;com.jersey&lt;/groupId&gt;
    &lt;artifactId&gt;rest&lt;/artifactId&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;rest&lt;/name&gt;

    &lt;build&gt;
        &lt;finalName&gt;rest&lt;/finalName&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;2.5.1&lt;/version&gt;
                &lt;inherited&gt;true&lt;/inherited&gt;
                &lt;configuration&gt;
                    &lt;source&gt;1.7&lt;/source&gt;
                    &lt;target&gt;1.7&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

    &lt;dependencyManagement&gt;
        &lt;dependencies&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.glassfish.jersey&lt;/groupId&gt;
                &lt;artifactId&gt;jersey-bom&lt;/artifactId&gt;
                &lt;version&gt;${jersey.version}&lt;/version&gt;
                &lt;type&gt;pom&lt;/type&gt;
                &lt;scope&gt;import&lt;/scope&gt;
            &lt;/dependency&gt;
        &lt;/dependencies&gt;
    &lt;/dependencyManagement&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
            &lt;!-- use the following artifactId if you don&#39;t need servlet 2.x compatibility --&gt;
            &lt;!-- artifactId&gt;jersey-container-servlet&lt;/artifactId --&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.glassfish.jersey.inject&lt;/groupId&gt;
            &lt;artifactId&gt;jersey-hk2&lt;/artifactId&gt;
            &lt;version&gt;2.26&lt;/version&gt;
        &lt;/dependency&gt;
        
         &lt;dependency&gt;
             &lt;groupId&gt;javax.activation&lt;/groupId&gt;
             &lt;artifactId&gt;activation&lt;/artifactId&gt;
             &lt;version&gt;1.1.1&lt;/version&gt;
         &lt;/dependency&gt;
        &lt;!-- uncomment this to get JSON support--&gt;
        &lt;dependency&gt;
		    &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
		    &lt;artifactId&gt;jersey-media-json-jackson&lt;/artifactId&gt;
		    &lt;/dependency&gt;
        
        &lt;!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api --&gt;
		&lt;dependency&gt;
		    &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
		    &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
		    &lt;version&gt;2.3.0&lt;/version&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt; 
	       &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt; 
	       &lt;artifactId&gt;gson&lt;/artifactId&gt; 
	       &lt;version&gt;2.6.2&lt;/version&gt; 
	   &lt;/dependency&gt;
	   
		&lt;dependency&gt;
		    &lt;groupId&gt;org.glassfish.jersey.security&lt;/groupId&gt;
		    &lt;artifactId&gt;oauth2-client&lt;/artifactId&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
		    &lt;groupId&gt;com.auth0&lt;/groupId&gt;
		    &lt;artifactId&gt;java-jwt&lt;/artifactId&gt;
		    &lt;version&gt;3.10.3&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --&gt;
		&lt;dependency&gt;
		    &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
		    &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
		    &lt;version&gt;2.11.2&lt;/version&gt;
		&lt;/dependency&gt;
				
		&lt;!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --&gt;
		&lt;dependency&gt;
		    &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
		    &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
		    &lt;version&gt;2.11.0&lt;/version&gt;
		&lt;/dependency&gt;
				
    &lt;/dependencies&gt;
    &lt;properties&gt;
        &lt;jersey.version&gt;2.26-b03&lt;/jersey.version&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;
&lt;/project&gt;

Client checking java file

import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import com.jersey.rest.User;

public class MainTesting {
	public static void main(String[] args) {
	
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(&quot;http://localhost:9090/rest/jwt/async&quot;); 
		AsyncInvoker invoker = target.request().async();
		
		Future&lt;Boolean&gt; response = invoker.post(Entity.entity(new User(), MediaType.APPLICATION_JSON), Boolean.class);
		try {
			System.out.println(response.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
	}

}

APPLICATION ERROR

SEVERE: Servlet.service() for servlet [Jersey jwt Application] in context with path [/rest] threw exception [java.lang.UnsupportedOperationException: Asynchronous processing not supported on Servlet 2.x container.] with root cause
java.lang.UnsupportedOperationException: Asynchronous processing not supported on Servlet 2.x container.
	at org.glassfish.jersey.servlet.WebComponent$3.suspend(WebComponent.java:137)
	at org.glassfish.jersey.servlet.internal.ResponseWriter.suspend(ResponseWriter.java:125)
	at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.suspend(ServerRuntime.java:921)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:326)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
	at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:337)
	at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
	at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
	at..................```

Tomcat exception
```WARNING: Unable to start async because the following classes in the processing chain do not support async [org.glassfish.jersey.servlet.ServletContainer]
java.lang.IllegalStateException: A filter or servlet of the current chain does not support asynchronous operations.
	at org.apache.catalina.connector.Request.startAsync(Request.java:1692)
	at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1050)
	at org.glassfish.jersey.servlet.async.AsyncContextDelegateProviderImpl$ExtensionImpl.getAsyncContext(AsyncContextDelegateProviderImpl.java:112)
	at org.glassfish.jersey.servlet.async.AsyncContextDelegateProviderImpl$ExtensionImpl.suspend(AsyncContextDelegateProviderImpl.java:96)
	at org.glassfish.jersey.servlet.internal.ResponseWriter.suspend(ResponseWriter.java:125)
	at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.suspend(ServerRuntime.java:921)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:326)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
	at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:337)
	at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
	at org.glassfish.jersey.internal.Errors$1.call(Err....

Aug 21, 2020 5:07:24 PM org.glassfish.jersey.servlet.internal.ResponseWriter suspend
WARNING: Attempt to put servlet request into asynchronous mode has failed. Please check your servlet configuration - all Servlet instances and Servlet filters involved in the request processing must explicitly declare support for asynchronous request processing.
java.lang.IllegalStateException: A filter or servlet of the current chain does not support asynchronous operations.
	at org.apache.catalina.connector.Request.startAsync(Request.java:1692)
	at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1050)
	at org.glassfish.jersey.servlet.async.AsyncContextDelegateProviderImpl$ExtensionImpl.getAsyncContext(AsyncContextDelegateProviderImpl.java:112)
	at org.glassfish.jersey.servlet.async.AsyncContextDelegateProviderImpl$ExtensionImpl.suspend(AsyncContextDelegateProviderImpl.java:96)
	at org.glassfish.jersey.servlet.internal.ResponseWriter.suspend(ResponseWriter.java:125)
	at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.suspend(ServerRuntime.java:921)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:326)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
	at org.glassfish.jersey.server.Serv.....

Aug 21, 2020 5:07:24 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Jersey jwt Application] in context with path [/rest] threw exception [javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.] with root cause
javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:327)
	at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
	at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:337)
	at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
	at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
	....```

Could you help me to solve this? :)


</details>


# 答案1
**得分**: 1

在 web.xml 的 servlet 定义中添加 `<async-supported>true</async-supported>`。

```xml
<servlet>
    <servlet-name>Jersey jwt Application</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.jersey.jwt</param-value>
    </init-param>
    <async-supported>true</async-supported>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey jwt Application</servlet-name>
    <url-pattern>/jwt/*</url-pattern>
</servlet-mapping>

这解决了该问题。

英文:

Add &lt;async-supported&gt;true&lt;/async-supported&gt; in web.xml servlet definition.

&lt;servlet&gt;
    &lt;servlet-name&gt;Jersey jwt Application&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
    &lt;init-param&gt;
        &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
        &lt;param-value&gt;com.jersey.jwt&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;async-supported&gt;true&lt;/async-supported&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
    &lt;servlet-name&gt;Jersey jwt Application&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/jwt/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;

it solved the issue.

huangapple
  • 本文由 发表于 2020年8月21日 19:50:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63522413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定