views:

132

answers:

2

Hello everyone,

I've been playing with Java Servlets and Ajax a bit, and I've got a situation on which I would really appreciate advice.

Let's say I have HTML page with a start and stop buttons, and as a result of clicking start button,
overridden doGet (or doPost) method on a servlet is invoked which computes something that takes a long time to complete. (e.g. a giant loop, or even Infinite loop, doesn't matter, I'm interested in concepts here).

So, I'm asking you:

1.What would be my options to kill / shut down / halt / exit doGet method whan I hit stop button on a web page? Do I use threading here, or there is simpler way? I take it that using System exit is not a very good idea, right? ;)

2.So, let's say I implement code for stopping doGet method. What would happen If I hit start on one browser(e.g.IE), and while this long computation takes place open new tab or other browser(e.g.Firefox) and open same url and hit stop? Would that stop my original computation? Is there any easy way to avoid this?

I know that questions are a bit off, as I'm just starting with server-side of things. :)

Any suggestions would be greatly appreciated!

+2  A: 

hi, your stop handler can set a flag in the session context, which the long-running thread will occasionally check and exit if necessary.

you can avoid the multiple browser issue, by generating a unique task id each time the page is loaded. then you can only start or stop a specific task. this id can be a key in the session.

Correct, with exception of clustered environment with no sticky sessions. There one have to use shared objects like distributed cache or database to pass the flag.
Vladimir Dyuzhev
A: 

I think you need some kind of new process started after you submit request and this process should answer responses during runtime (showing progress for example via AJAX). Also it should check if there is new request with stop command. Page should be AJAX one with progress/result/stop button.

Artic