简体   繁体   English

从Jersey中的过滤器发送响应后,如何继续请求处理?

[英]How to continue request processing after sending response from filter in Jersey?

I have a situation where I need to return an "accepted" response for every request received and publish the actual response later to a separate endpoint outside the service. 我遇到一种情况,我需要为收到的每个请求返回一个“接受的”响应,然后将实际响应发布到服务之外的另一个端点。

To implement the 'accepted' Response I implemented a filter. 为了实现“接受的”响应,我实现了一个过滤器。

public class AcknowledgementFilter implements ContainerRequestFilter{

@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
    containerRequestContext.abortWith(Response.accepted().build());
    // call Resource method in new Thread() . <------ ?
}

}

Implementation of service endpoints: 服务端点的实现:

@Path("/vendor")
public class VendorHandler {

@POST
public void addVendor(VendorRequest addVendorRequest)){
    vendor  = new Vendor();
    Client.publish(vendor);  // publish request to an endpoint
    return null;
}

How do I call the addVendor of VendorHandler(or any method depends on request) from the acknowledgement filter? 如何从确认过滤器调用VendorHandler的addVendor(或任何方法取决于请求)?

Is there any other way to implement an accepted response for every request then process the request separately? 还有其他方法可以为每个请求实现一个可接受的响应,然后分别处理该请求吗?

You can use AsyncResponse, 您可以使用AsyncResponse,

@GET
@ManagedAsync
@Produces(MediaType.APPLICATION_JSON)
public void getLives(@Suspended final AsyncResponse asyncResponse,
                     @DefaultValue("0") @QueryParam("newestid") final int newestId,
                     @QueryParam("oldestid") final Integer oldestId) {

    asyncResponse.setTimeoutHandler(asyncResponse1 -> {
        logger.info("reached timeout");
        asyncResponse1.resume(Response.ok().build());
    });

    asyncResponse.setTimeout(5, TimeUnit.MINUTES);

    try {
        List<Life> lives = oldestId == null ?
                Lifes.getLastLives(newestId) : Lifes.getOlderLives(oldestId);

        if (lives.size() > 0) {
            final GenericEntity<List<Life>> entity = new GenericEntity<List<Life>>(lives) {
            };
            asyncResponse.resume(entity);
        } else LifeProvider.suspend(asyncResponse);

    } catch (SQLException e) {
        logger.error(e, e);
        asyncResponse.resume(new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR));
    }

}

Check this Link for more details. 检查此链接以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM