简体   繁体   中英

JAX-RS Endpoint triggering server-side actions

I have a lightweight Java application exposing a web service through Jersey/Grizzly. It's fairly simple as it just sends back JSON content:

@GET
public Response status() {
    CacheControl cc = new CacheControl();
    cc.setMaxAge(CLIENT_EXPIRY);
    cc.setPrivate(true);

    ResponseBuilder builder = Response.ok(someJsonString, MediaType.APPLICATION_JSON);
    builder.cacheControl(cc);
    return builder.build();
}

I would like to perform server-side operations when and only when a client requests this response, but without delaying the response itself.

These other methods will not influence the response, but will use up server resources, and take a significant time. That would make the end user experience less enjoyable if I simply pasted the call in the middle of response building.

What would be a good way to monitor the endpoint activity and trigger a server-side treatment without delaying the response?

Start a new thread that will do the work, and then return the response to the client. Consider using a thread pull, not to exhaust your server's resources. This way the client will not wait for response and the task will be executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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