嗨,我需要在 apache cxf 服务器中实现异步处理请求。到目前为止,我认为我需要将 AsyncResponse
添加到请求中。但它不起作用。
@GET
@Path("/test")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_XML)
void sample(@Suspended AsyncResponse asyncResponse);
@Override
public void sample(AsyncResponse asyncResponse) {
asyncResponse.setTimeout(3, TimeUnit.MINUTES);
asyncResponse.setTimeoutHandler(new TimeoutHandler() {
@Override
public void handleTimeout(AsyncResponse asyncResponse) {
asyncResponse.resume("Sample");
}
});
}
当我向端点发送请求时,它会抛出异常。
WebApplicationExceptionMapper - WebApplicationException has been caught, status: 415
javax.ws.rs.WebApplicationException
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1054)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:614)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:578)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:238)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:89)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:122)
at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.serviceRequest(JettyHTTPDestination.java:344)
at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.doService(JettyHTTPDestination.java:310)
at org.apache.cxf.transport.http_jetty.JettyHTTPHandler.handle(JettyHTTPHandler.java:72)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:943)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:879)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
at org.eclipse.jetty.server.Server.handle(Server.java:345)
at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:441)
at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:919)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:582)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:218)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:51)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:586)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:44)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:533)
我想知道我异步实现的方式是否正确,如果是这样,我的实现有什么问题。
最佳答案
不确定您是否仍在寻找答案。您的实现看起来很好,通过查看堆栈跟踪,服务器似乎无法正确理解请求。请检查 web.xml 中是否有您正在使用的过滤器。过滤器应该具有此属性( true )以支持异步调用。
你的过滤器应该是这样的..
<filter>
<filter-name>XXXXXX</filter-name>
<filter-class>xxx.xxx.xxx.XXXXXX</filter-class>
<async-supported>true</async-supported>
</filter>
关于java - Apache cxf 服务器异步请求处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35498240/