Well, you could write a stop method and rewrite the "start" method to use
threads, then after you instantiate the class, store it as a session variable, then
start the "start" method as a thread. Then return a page containing the key used
to store the object and stop button. The stop button will call another jsp that
will retreive the object stored under that key and call the "stop" method. This is
all really loosely explained, but you should be able to extrapolate from here.
Unfortunately, your user will also get no notification that the process is finished.
You could, however, write a checkProgress method that another jsp can use to check the process and "refreshes" from time to time. This would have to called
using a refresh from the "stop button" page and also then must contain the "stop
button".
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
That was quick. I'm glad I was able to help.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
Sorry, I was away.
Well, what I am saying goes as follows:
the long running process should be contained in its own object rather than
being called directly from the jsp. Also, creating an instance of this
object should not start the process, but just create a "handle" to it. This
object should have "start", "stop", and "checkProgress" methods. The
start method should start the long running process in a thread. The stop
method should interupt this thread and perform and needed cleanup. The
checkProgress method should report back how far the process has
progressed (how you do/report/track this is completely up to you).
Now on to usage:
In initial JSP:
- create instance
- store instance as session object
- call instance start routine
- return an html page containing a "stop" link (url to "stop" page")
- this page will (after a set time) refresh to the "checkProgress" page
use the refresh header info for this
In refresh jsp:
- retrieve instance from session
- call the checkProgress method
- return an html page that will refresh to itself after a set time
use the refresh header info for this
- this page should also contain the "stop" link (url to the "stop" page)
In Stop page:
- retrieve instance from session
- call "stop" method
- delete the session object
- return an acknowledgement html page
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
I've never tried to do this, but I think you're going to have to track the thread ID of the long-running process
The Object that "controls" the thread is what is being stored in the session. IOW, already suggested. and terminate that thread when the user clicks your "stop processing" button in your app.
You can't terminate a thread (you can only interrupt, i.e. signal, it and let the thread clean itself up). IOW, already suggested.I don't think you can detect them pressing the browser's cancel button.
Already mentioned.Also, I have never dealt with threads so I have no idea how to implement this suggestion.
Well, I don't mean to be mean, but then why post this? He has already been given a very detailed answer and this post didn't add anything, I'm sorry to say.
Aside from the fact that this thread is 2 years old.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
IMO creating and manipulating threads in a managed environment is bad in itself since the request is already being processed by a thread and the chances of the programmer screwing up with threading and making the mess out of the entire affair is pretty high. Plus, there is no reliable way of completely stopping a thread since it's inherently unsafe and has been deprecated.
If the user isn't expecting a quick response, one possible way would be to use asynchronous messaging. A better method would be to make the entire process interactive and execute the process in chunks so that the user always has the option of going back or canceling the process under consideration.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
As far as the programmer making a mess of the programming, that can always happen.
And, I never said he should call thread destroy, or anything in that manner. I said to call interuppt and let the Thread clean itself up (and those actions are once again something the programmer must do), which is not, of course, deprecated.
The last paragraph, of course, is another very good option, though.
I agree, that it is not a very good idea to have a browser wait on a long running process, it would be a much better idea to have a "batch" style backend that the user can either check on at a later date, or that the backend can notify the user in some way (email, sms, etc.).
Some things cannot always be run in chunks, however. As an example, I recently had to write a Monte-Carlo financial simulation that operated on a few hundred thousand transactions, over a 10 year time period on a quarterly basis, over a minimum of 20000 iterations. The simulation could not be broken down (and still be effecient) and took a couple of hours to run. We used an event driven batch-style backend that sent an email upon completion.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
> As far as the programmer making a mess of the programming, that can always happen.
Though the probability increases in this case. :-)
> And, I never said he should call thread destroy, or anything in that manner.
I guess I misread your first post.
The stop button will call another jsp that will retreive the object stored under that key and call the "stop" method.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Sorry. It's explained (better, although still not perfectly) in the next post.
The stop method should interupt this thread and perform and needed cleanup.
;-)
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
NEVER use Thread.stop(). It's extremely dangerous and will more likely than not cause very nasty things to happen.
It's deprecated for a reason you know, and that reason is that it will NOT do any cleanup.
It will kill the thread, and everything in it, now. No questions asked.
If that leaves data in an incomplete or unstable state, it doesn't care. If it leaves network or database resources open it couldn't care less. If it keeps hardware locked, waiting for commands that never can come no more, it's oblivious of that.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
NEVER use Thread.stop(). It's extremely dangerous and will more likely than not cause very nasty things to happen.
It's deprecated for a reason you know, and that reason is that it will NOT do any cleanup.
It will kill the thread, and everything in it, now. No questions asked.
If that leaves data in an incomplete or unstable state, it doesn't care. If it leaves network or database resources open it couldn't care less. If it keeps hardware locked, waiting for commands that never can come no more, it's oblivious of that.
Yes. I know. Read again, that was suppossed to be self-written "stop" method in a class that created the thread (not the one from the thread class) designed to call interrupt on the actual thread and then perform some clean-up.
Edit: And yes, it was a very poor choice of a name, and it caused the confusion, but hey, at least I knew what Imeant. ;-)
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
of course including any such method in a JSP (which the kid was asking for) is as bad as calling Thread.stop()...
It's likely going to be ineffective as well, as the thread may well survive the context in which the JSP is executed.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
that's a different problem, and one you can't solve.
The server will time out the request, and even if not the client will at some point time out the request.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337