This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
We have one JSF actionListener with just one, long-runnig statement (for example, Oracle stored proc call).
Now, we want to provide our user with ability to cancel this actionListener, by clicking to another, 'Cancel' command Button
In general, wonder if this possible at all (so, to cancel some Java server side method) and if yes, how to do this , if this method actually a stored proc call ?
My idea (for now) is :
* To use that actionListener in order to start new Java Thread (which calls stored proc).
After that, the same actionListener saves reference to the PreparedStatement (whics executes updates) in order to call PreparedStatement.cancel() on later) AND finishes its works (along with JSF lifecycle), but stored proc continues execution i its own Thread, now outside of JSF lifecycle.
In this scenario, one question comes to mind :
Stored proc changes some database data, which needs to be displayed on the JSF page. How to detect that stored proc's thread finished its execution (or that exception happens), in order to refresh User interface with a new data ?
However (forget for stored proc for now), the main question is : how to cancel long-running JSF actionListener ? What needs to be done on the user interface side, and on the server side ?
You shouldn't HAVE long-running action listeners. In fact, be it JSF, Struts, or even simple JSPs, long-running actions should not be attached to the request/reply processors.
Even having the processor spawn a thread may lead to problems. A cleaner solution is to set up an independent engine and have the processors queue up requests to the engine. I often use what I call a "null servlet" for this. A null servlet is simply a servlet that has an init() method that sets up the engine thread(s) and request queue(s), but doesn't have a doGet/doPost or other request dispatcher. You normally configure web.xml to auto-start this servlet, since there won't be any web requests coming in to launch it.
Because JSF is based on HTTP and HTTP is solely request/response, with no way to post unrequested responses, you have to have the client poll the engine for status. Traditionally, that was done with a timed refresh HTML meta-tag, but if you're using an AJAX framework like RichFaces, there are other more JSF-friendly ways to do the polling as well.
An IDE is no substitute for an Intelligent Developer.