开发者

How to stop/interrupt a servlet handler?

If I have a long-running request handler, and the user hits the browser Stop button, how do I terminate the handler so it doesn't continue working? I'm thinking something simple like raising a thread interrupt based on the client connection state as checked from another watchdog thread (who's given the connections as they come in), but perhaps 开发者_如何学JAVAthere are better solutions.


Another, simpler approach would be to make that long-running process asynchronous, using ExecutorService from java.util.concurrent. Return immediately and give the client a way to get the response when it's ready, a la Federal Express.

Servlets should never be executing processes, synch or asynch. They're for listening for HTTP requests, binding and validating input parameters, routing to the appropriate service to fulfill the request, and marshaling the response to the appropriate view.


If the user hits the Stop button you will never know about it other than as a connection reset. You don't get any opportunity to raise a thread interrupt: that would imply another communication from the browser to another thread in the server, which doesn't happen.


one way to handle to this is to implement the long work/req/proc separately and expose status flags to give status. options. Now call it from your servlet async. When a client presses STOP, come to servlet check the running status and interrupt if required and update user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜