0
我正在嘗試創建異步的Jersey RESTful webap。Jersey異步多線程響應?
以下代碼用於測試實現,但它不起作用。
@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 IS OKAY! ");
}
}
我測試它的方式是我打開兩次URL。但第二個標籤總是等待第一個超時。所以這顯然不起作用。
Servlet的文件:
<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>
是的。但是,您如何解釋第二個瀏覽器選項卡加載所需的等待時間?所有在第一個標籤之後打開的標籤等待它完成加載,然後立即加載。 –
提到的鏈接現在被破壞,可以查看:https://jersey.github.io/documentation/latest/async.html – anand