如何在tomcat 7中使用Jersey 2中的异步回调

时间:2014-01-31 11:10:35

标签: java rest tomcat asynchronous jersey-2.0

如何在tomcat服务器中使用jersey 2的异步回调。 我在泽西手册中找到了一些示例:https://jersey.java.net/documentation/latest/user-guide.html#d0e8496

但问题出现在我想用以下代码测试以下代码时: https://jersey.java.net/documentation/latest/user-guide.html#d0e8615 代码,如Jersey 2手册中所述。

-------------------------------------------- -------------------------------------------------- ---------------------------------

由于手动版本已更改,我在此处发布代码:

这是我的服务:

            import javax.ws.rs.GET; 
            import javax.ws.rs.Path; 
            import javax.ws.rs.Produces; 
            import javax.ws.rs.container.AsyncResponse; 
            import javax.ws.rs.container.CompletionCallback; 
            import javax.ws.rs.container.Suspended; 
            import javax.ws.rs.core.MediaType; 
            import javax.ws.rs.core.Response; 

            @Path("/resource") 
            public class AsyncResource { 
                private static int numberOfSuccessResponses = 0; 
                private static int numberOfFailures = 0; 
                private static Throwable lastException = null; 

                @GET 
                public void asyncGetWithTimeout(@Suspended final AsyncResponse asyncResponse) { 
                System.out.println("AsyncResource.asyncGetWithTimeout()"); 
                    asyncResponse.register(new CompletionCallback() { 
                        @Override 
                        public void onComplete(Throwable throwable) { 
                            if (throwable == null) { 
                                // no throwable - the processing ended successfully 
                                // (response already written to the client) 
                                numberOfSuccessResponses++; 
                            } else { 
                                numberOfFailures++; 
                                lastException = throwable; 
                            } 
                        } 
                    }); 

                    new Thread(new Runnable() { 
                        @Override 
                        public void run() { 
                            String result = veryExpensiveOperation(); 
                            asyncResponse.resume(result); 
                        } 

                        private String veryExpensiveOperation() { 
                            // ... very expensive operation 
                        return "Hi"; 
                        } 
                    }).start(); 
                } 
            }

这是客户:

    ClientConfig clientConfig = new ClientConfig(); 
    Client client = ClientBuilder.newClient(clientConfig); 

    WebTarget webTarget = client.target("http://localhost:8080/Jersey2.5Service/rest"); 
    WebTarget target = webTarget.path("resource"); 

    final AsyncInvoker asyncInvoker = target 
                    .request().async(); 
    final Future<Response> responseFuture = asyncInvoker.get(); 
    System.out.println("Request is being processed asynchronously."); 
    final Response response = responseFuture.get(); 
            // get() waits for the response to be ready 
    System.out.println("Response received." +response.readEntity(String.class));

的web.xml

<servlet>  
      <servlet-name>HelloServlet</servlet-name>  
        <servlet-class>  
         org.glassfish.jersey.servlet.ServletContainer  
       </servlet-class>  
       <init-param>  
         <param-name>jersey.config.server.provider.packages</param-name>  
         <param-value>main.java</param-value>  
       </init-param>  
       <load-on-startup>1</load-on-startup>  
     </servlet>  
     <servlet-mapping>  
       <servlet-name>HelloServlet</servlet-name>  
       <url-pattern>/rest/*</url-pattern>  
     </servlet-mapping>

这些是我正在使用的罐子:

    asm-all-repackaged-2.2.0-b21.jar 
    cglib-2.2.0-b21.jar 
    guava-14.0.1.jar 
    hk2-api-2.2.0-b21.jar 
    hk2-locator-2.2.0-b21.jar 
    hk2-utils-2.2.0-b21.jar 
    javax.annotation-api-1.2.jar 
    javax.inject-2.2.0-b21.jar 
    javax.servlet-api-3.0.1.jar 
    javax.ws.rs-api-2.0.jar 
    jaxb-api-2.2.7.jar 
    jersey-client.jar 
    jersey-common.jar 
    jersey-container-servlet-core.jar 
    jersey-container-servlet.jar 
    jersey-server.jar 
    org.osgi.core-4.2.0.jar 
    osgi-resource-locator-1.0.1.jar 
    persistence-api-1.0.jar 
    servlet-api-3.0.jar 
    validation-api-1.1.0.Final.jar 

这些错误即将来临:

Jan 31, 2014 4:06:53 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: Not supported.
    at org.apache.catalina.connector.Request.startAsync(Request.java:1676)
    at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1031)
    at org.glassfish.jersey.servlet.async.AsyncContextDelegateProviderImpl$ExtensionImpl.suspend(AsyncContextDelegateProviderImpl.java:87)
    at org.glassfish.jersey.servlet.internal.ResponseWriter.suspend(ResponseWriter.java:120)
    at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.suspend(ServerRuntime.java:758)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:330)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259)
    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 org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Jan 31, 2014 4:06:53 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [HelloServlet] in context with path [/Jersey2.5Service] 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:331)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259)
    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 org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

4 个答案:

答案 0 :(得分:8)

对我来说,解决方案是在Servlet过滤器中添加对Async的支持。错误消息给了我一个提示:

尝试将servlet请求置于异步模式失败。请检查您的servlet配置 - 请求处理中的所有Servlet实例和涉及的Servlet过滤器必须显式声明对异步请求处理的支持

我使用Spring for Dependency Injection并使用默认的Jersey servlet。对于Spring,我使用委托过滤器代理。我需要声明Filter asycn支持为true。

对于servlet:

 <servlet>
    <servlet-name>myserlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>        
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>

并为Spring过滤器添加async支持true。

<filter>
        <description>Spring filter</description>
        <display-name>spring-filter</display-name>
        <filter-name>springFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <async-supported>true</async-supported>
    </filter>

我希望这对你和其他人有所帮助。

答案 1 :(得分:2)

我知道这个问题很老,但对于遇到此问题的人请将Tomcat服务器更新到最新版本8。**。从版本8开始的Tomcat支持异步处理。

答案 2 :(得分:1)

对于异步回调,您可以使用Guava lib。例如:

@GET
@Produces(MediaType.APPLICATION_JSON)
@ManagedAsync
public void getBooks(@Suspended final AsyncResponse response){

ListenableFuture<Collection<Book>> bookFuture = dao.getBooksAsync();
    Futures.addCallback(bookFuture, new FutureCallback<Collection<Book>>() {

        public void onSuccess(Collection<Book> books){
            response.resume(books);
        }
        public void onFailure(Throwable thrown){
            response.resume(thrown);
        }
    });
}

在我的书DAO中,我有:

BookDao(){
    books = new HashMap<String,Book>();

    Book b1 = new Book();

    service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));               
}

Collection<Book> getBooks(){

    return(books.values());
}

ListenableFuture<Collection<Book>> getBooksAsync(){

    ListenableFuture<Collection<Book>> future = 
            service.submit(new Callable<Collection<Book>>() {
                public Collection<Book> call() throws Exception{
                    return getBooks();
                }
            });
    return(future);
}

答案 3 :(得分:0)

这可能为时已晚,但是升级到tomcat 9可以解决此问题,而无需修改服务器中的任何内容