gpt4 book ai didi

java - 使用 JAX-RS 处理异步 http 请求?

转载 作者:可可西里 更新时间:2023-11-01 16:32:34 27 4
gpt4 key购买 nike

我正在使用 JAX-RS 为资源实现 CRUD 操作。在我的第一个用例中,它是一个阻塞(同步) 请求,即在单线程执行中,客户端获得响应。

用例 1:

@Path("/resourceService")
public class CRUDService {

@GET
@Path("/{param}")
public Response getResource(@PathParam("param") String id) {

Resource res = SomeBean().getResource(id);
return Response.status(200).entity(res).build();

}

}

用例 2:

现在我必须实现相同的请求,但它是一个非阻塞(异步) 请求,因此一旦请求到达服务器,我就必须向服务器返回响应一个 ACKNOWLEDGEMENT 即一个带有 ACCEPTED (202) 状态码的 Response 对象,然后恢复初始请求处理(通过 id 获取资源这个案例)。

此请求(检索到的资源)的响应通过发送一个新的 POST 请求发送给客户端,该请求的内容是检索到的资源,然后客户端将返回此 POST 的响应 带有成功状态代码的请求。(注意:这里的客户端不是浏览器,客户端和服务器都是被调用的 REST Api 中的机器)。

@Path("/resourceService")
public class CRUDService {

@GET
@Path("/{param}")
public Response getResource(@PathParam("param") String id) {

// ACKNOWLEDGEMENT response built and sent back to clinet
return Response.status(202).build();

// Do the request handling i.e resource retrieval
Resource res = SomeBean().getResource(id);
Resonse response = Response.status(200).entity(res).build();

// Create a HTTP POST Request and set the body as response object

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
post.setEntity(response);
HttpResponse response = client.execute(post);


}

}

我阅读了有关 Asynchronous JAX-RSjava.util.concurrent 包的信息。

如果我异步执行它,比如启动一个新线程来处理请求,如何发送第一次返回 ACK 响应。例如:

@POST
@Consumes("application/json")
@Produces("application/json")
public void getResource(@PathParam("param") String id,
final @Suspended AsyncResponse response) {
new Thread() {
public void run() {
Resource res = SomeBean().getResource(id);
response.resume(resource);
}
}.start();
}
}

如何使用 JAX-RS 实现此调用流程?

编辑:我按照您的回答中提到的进行了尝试,但是出现异常:

服务器日志:

2016 - 07 - 25 12: 22: 01, 521 ERROR[io.undertow.request](
default task - 1) UT005023: Exception handling request to / resourceService: org.jboss.resteasy.spi.UnhandledException: java.lang.NoSuchMethodError: my.ow.dummy.package.CRUDService$1. < init > (Lmy / own / dummy / package / CRUDService;) V
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java: 76)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java: 212)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java: 149)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java: 372)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java: 179)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java: 220)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java: 56)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java: 51)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at javax.servlet.http.HttpServlet.service(HttpServlet.java: 790)[jboss - servlet - api_3 .1 _spec - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java: 85)[undertow - servlet - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java: 61)[undertow - servlet - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java: 36)[undertow - servlet - 1.0 .0.Final.jar: 1.0 .0.Final]
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java: 78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java: 25)[undertow - core - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java: 113)[undertow - servlet - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java: 52)[undertow - core - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java: 45)[undertow - core - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java: 61)[undertow - servlet - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java: 70)[undertow - servlet - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java: 76)[undertow - core - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java: 25)[undertow - core - 1.0 .0.Final.jar: 1.0 .0.Final]
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java: 61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java: 25)[undertow - core - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java: 25)[undertow - core - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java: 240)[undertow - servlet - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java: 227)[undertow - servlet - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java: 73)[undertow - servlet - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java: 146)[undertow - servlet - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.server.Connectors.executeRootHandler(Connectors.java: 168)[undertow - core - 1.0 .0.Final.jar: 1.0 .0.Final]
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java: 687)[undertow - core - 1.0 .0.Final.jar: 1.0 .0.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1145)[rt.jar: 1.7 .0 _40]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java: 615)[rt.jar: 1.7 .0 _40]
at java.lang.Thread.run(Thread.java: 724)[rt.jar: 1.7 .0 _40]
Caused by: java.lang.NoSuchMethodError: my.ow.dummy.package.CRUDService$1. < init > (Lmy / own / dummy / package / CRUDService;) V at my.ow.dummy.package.CRUDService.handlePutRequest(CRUDService.java: 78)[classes: ]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[rt.jar: 1.7 .0 _40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 57)[rt.jar: 1.7 .0 _40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43)[rt.jar: 1.7 .0 _40]
at java.lang.reflect.Method.invoke(Method.java: 606)[rt.jar: 1.7 .0 _40]
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java: 137)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java: 280)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java: 234)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java: 221)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java: 356)[resteasy - jaxrs - 3.0 .6.Final.jar: ]
...29 more

这个异常并没有早点出现,它是在我添加了新的 Thread() 部分之后开始的。有什么想法吗?

最佳答案

在客户端和服务器实际上都是服务器的情况下,您可以创建一个带有一些参数的资源方法,该方法将获取 uri 以将 Resource 发送到,例如 @HeaderParam,如果初始请求需要是 GET :

@GET
@Path("/{param}")
public Response getResource(@PathParam("param") String id, @HeaderParam(
"SendBackUri") String uri) {
new Thread() {
public void run() {
Resource res = SomeBean().getResource(id);
ClientBuilder.newClient().target(uri).request().buildPost(Entity.entity(res, MediaType.WILDCARD_TYPE)).invoke();
}
}.start();
return Response.status(202).build();
}

编辑:您可以使用 ExecutorService:

@GET
@Path("/{param}")
public Response getResource(@PathParam("param") String id, @HeaderParam(
"SendBackUri") String uri) {
executorService.execute(new Runnable() {
public void run() {
Resource res = SomeBean().getResource(id);
Response response = ClientBuilder.newClient().target(uri).request().buildPost(Entity.entity(res, MediaType.WILDCARD_TYPE)).invoke();
//deal with response
}
});
return Response.status(202).build();
}

RunnableThreadrun 方法中的任何内容都在单独的线程中执行。当然,在此线程中,您可以处理来自 POST 请求的 Response,如果需要,您可以根据返回的状态码以某种方式使用react。


让两台服务器相互通信的情况是可能的,尽管不是很常见。如果客户端只是客户端,可以做异步客户端请求,如:

Future<Response> f = ClientBuilder.newClient().target(uri).request().async().get();
//do some client specific stuff which you probably do when receive 202;
Response r = f.get(); //wait for response
//do whatever you would do with the resource

对于服务器端,你可以使用你的异步方法

public void getResource(@PathParam("param") String id,
final @Suspended AsyncResponse response)

如你所愿。

关于java - 使用 JAX-RS 处理异步 http 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38485892/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com