java - Jersey asynchronous multi threaded response? -


i trying create asynchronous jersey restful webap.

the following code used test implementation not working.

@path("/demox") public class restclient {      public static boolean ran = false;      @get     @path("/test")     @managedasync     public void test(@suspended final asyncresponse asyncresponse) throws interruptedexception {         asyncresponse.settimeout(10000, timeunit.milliseconds);         asyncresponse.settimeouthandler(ar -> ar.resume(                 response.status(response.status.service_unavailable)                         .entity("operation timed out")                         .build()));         if(!ran) {             while(true) {                 ran = true;             }         }         asyncresponse.resume("rest okay! ");     } } 

the way test open url twice. second tab waits first 1 timeout. not working.

servlet file:

<servlet>     <servlet-name>jersey rest service</servlet-name>     <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class>     <init-param>         <param-name>jersey.config.server.provider.packages</param-name>         <param-value>services,org.codehaus.jackson.jaxrs</param-value>     </init-param>     <load-on-startup>1</load-on-startup>     <async-supported>true</async-supported> </servlet> <servlet-mapping>     <servlet-name>jersey rest service</servlet-name>     <url-pattern>/rest/*</url-pattern> </servlet-mapping> 

i think working. accordingly here:

by asyncresponse parameter resource method tell jersey runtime method supposed invoked using asynchronous processing mode, client connection should not automatically closed underlying i/o container when method returns. instead, injected asyncresponse instance (that represents suspended client request connection) used explicitly send response client using other thread. in other words, jersey runtime knows when asyncget method completes, response client may not ready yet , processing must suspended , wait explictly resumed response once becomes available.


Comments