Why is there no asyncContext.cancel()

2019-07-25 04:36发布

While the Servlet 3.0 spec has request.startAsync() and asyncContext.start(), why has it not provided a asyncContext.stop() or asyncContext.cancel() to initiate necessary clean-up on the server-side ?

Pls view this in the context of this other question to understand where I am coming from.

  • One HTTP request starts the Async processing and returns a .../outstandingRequests/requestId link to the client.
  • Another HTTP request calls DELETE on that link to cancel the request

In this case, if I had a way to clean-up the server-side (servlet container stuff like AsyncListeners), instead of having to call asyncContext.complete() which will probably try and send a response back to the client, it will make sense. Doesnt it ?

1条回答
一夜七次
2楼-- · 2019-07-25 05:34

In this scenario, call 1 is still hanging there, waiting for its response when call 2 comes in and wants to kill it. In this scenario, why would you not want to call complete() on call 1, thus finishing that call so that client stops waiting? You would probably want to set the status code to something other than 200 in this type of situation, but complete seems too be the best option given any scenario because it returns control back to the original caller and performs any request related cleanup work.

When a timeout happens, which is an error, the container calls complete (with a non-200 response code I imagine). The scenario you describe is similar to a timeout (albeit a forced timeout), so why not do the same thing the container does. Just call something like this before calling complete:

ac.getResponse().setStatus(500);

Any maybe write something to the output stream describing what caused this error.

查看更多
登录 后发表回答