Keep your Web applications running when tasks lock up. High volume Web sites often require asynchronous or threaded operations to achieve target performance criteria. While threads in Web containers are considered bad practice, the alternative is for developers to make blocking calls to code they cannot control. It becomes important that dependencies of this nature fail-fast. This developerWorks article covers a homegrown short-circuit pattern that ensures threaded execution and completion of a process in a fixed window of time.
This example would be cake w/ delegates in C#. Call BeginInvoke on a delegate, then check the IsCompleted property of the IAsyncResult returned by BeginInvoke. This is the preferred .Net paradigm for the UI to execute async calls. Network/IO calls? Call BeginInvoke, do some work, then call EndInvoke (though it will block at this point, at least the main thread can be ready for the return). Don’t want to risk blocking? Use a callback method…
sheesh, java makes it so complicated.
A web site under heavy load which implements this technique will spend all of it’s time beginning but then abandoning tasks.
Much better would be to check load before the task is begun and reject immediately if the response from already running tasks is sluggish.
…there are already pre-build APIs to assist in this. The idea proposed is not to be used “willy-nilly”, all over a site, but for those operations which you wish to “disconnect” temporarily the request/response from. Just firing a thread blindly is poor practice…one would really like more control over the process to ensure one doesn’t overload the container, wait on the task forever,etc.
For those cases, I’ve found the ThreadWorks API to be more than capable.
http://www.dvt.com/threadworks/DvtThreadWorks.html
Licensed under the LGPL, BTW. Very nice api, you can place a cap on max tasks, max runtime, etc. Invaluable when the situation truly warrants it. Most developers would likely build (or try to build) something like this anyway. I’ve used and tested this one fairly extensively, and it’s held up perfectly.
Another framework for similar requirements is
http://www.coopsoft.com/JavaProduct.html (Tymeac). The site isn’t too impressive, but in my initial tests it too seems acceptable.
Cheers!