Why is there no asyncContext.cancel()

无人久伴 提交于 2019-12-11 04:24:29

问题


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:


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.



来源:https://stackoverflow.com/questions/16328794/why-is-there-no-asynccontext-cancel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!