Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » How to correctly cancel a running job(I want the user to be able to cancel a job he has started running, if possibly taking too long. Is there a correct way to do this.)
How to correctly cancel a running job [message #833015] Fri, 30 March 2012 17:36 Go to next message
George R Malary is currently offline George R Malary
Messages: 36
Registered: March 2012
Location: New Jersey
Member
I know there is a method job.cancel(), but that does not ensure job will be cancelled, and only set a flag for cancellation. I thought there might be a way to get the job thread, and interrupt it, using technique used for threads. I dont know if I should continue down that path, as it may not be correct. Below is a sample I am trying to implement. I have a "Test Connection" button, and when pressed, it will start JobTestConnection, and the button text will change to "Stop Test Connection". The test connection may take a long time, if trying to resolve a hostname, and I want to be able to stop the job, and no longer see it running in the progress bar. Is there a way to do this?

private class JobTestConnection{
....
    protect IStatus run(IProgressMonitor monitor) {
        ...
        //This call will try to test the connection, and can hang sometimes, or
        //take a long time.
        validateNodeConnection(credentials);
        [i]Here will be code to perform actions on our UI.Thread[/i]

Re: How to correctly cancel a running job [message #833348 is a reply to message #833015] Sat, 31 March 2012 06:23 Go to previous messageGo to next message
Rüdiger Herrmann is currently offline Rüdiger Herrmann
Messages: 335
Registered: July 2009
Senior Member
George,

to obtain the thread that a Job is running on, call Job#getThread(). For
cancelling a Job, this won't bring you much further though.
A thread can only be interrupted in certain situations (see the JavaDoc
for Thread#interrupt()). The same accounts for Jobs, as they naturally
also run on threads.

In order to terminate the code in your validateNodeConnection method,
you'd have to change that code to react to interrupt() signals.

Another option might be to let the Job run even if it was cancelled and
just discard the result from the connection validation method. It
wouldn't be my first choice though.

HTH
Rüdiger


On 30.03.2012 23:36, George R Malary wrote:
> I know there is a method job.cancel(), but that does not ensure job will
> be cancelled, and only set a flag for cancellation. I thought there
> might be a way to get the job thread, and interrupt it, using technique
> used for threads. I dont know if I should continue down that path, as it
> may not be correct. Below is a sample I am trying to implement. I have a
> "Test Connection" button, and when pressed, it will start
> JobTestConnection, and the button text will change to "Stop Test
> Connection". The test connection may take a long time, if trying to
> resolve a hostname, and I want to be able to stop the job, and no longer
> see it running in the progress bar. Is there a way to do this?
>
>
> private class JobTestConnection{
> ....
> protect IStatus run(IProgressMonitor monitor) {
> ...
> //This call will try to test the connection, and can hang sometimes, or
> //take a long time.
> validateNodeConnection(credentials);
> Here will be code to perform actions on our UI.Thread
>
>
--
Rüdiger Herrmann
http://codeaffine.com
Re: How to correctly cancel a running job [message #833415 is a reply to message #833348] Sat, 31 March 2012 08:45 Go to previous messageGo to next message
George R Malary is currently offline George R Malary
Messages: 36
Registered: March 2012
Location: New Jersey
Member
Thanks for the reply Rudiger. The first suggestion you make is exactly what I was thinking from everything I researched. I dont know if that's the solution we want though, since we would have to implement all our backend services to handle Interrupted Exceptions.

Maybe as you mentioned, the better solution is to just let the thread die on its own, and ignore the result. Is there a way in that case to stop showing the job in the Progress Bar? Also, will we be able to run another instance of the job while the cancelled one is run in the background? I assume that is not a problem, since we create a new thread, but could this lead to deadlock, and should we manage that?

So I guess the easiest solution it to just let the job finish or cancel naturally, and ignore and try to hide that it is running from the user ...?
Re: How to correctly cancel a running job [message #834378 is a reply to message #833415] Sun, 01 April 2012 17:55 Go to previous messageGo to next message
Chris Fairhall is currently offline Chris Fairhall
Messages: 99
Registered: February 2011
Member
To implement the second suggested solution you could have your job start yet another thread to do the connection test and use Thread.join to wait for it to finish. When you interrupt your job thread it will interrupt the join so your job can terminate correctly. A new job would use a new thread so you could easily run it again while the old thread is still active. If you're concerned about too many threads being created you could use a thread pool with a blocking queue and an upper limit.
Re: How to correctly cancel a running job [message #835557 is a reply to message #834378] Tue, 03 April 2012 07:43 Go to previous messageGo to next message
Rüdiger Herrmann is currently offline Rüdiger Herrmann
Messages: 335
Registered: July 2009
Senior Member
George,

I don't know your backend, but a validateNodeConnection() method sounds
like there is one place where the interrupt is to be handled.

Having an abandoned job or even worse spawning an extra thread just to
be able to abandon it sounds way more scary to me than handling the
issue where its source is.
Having an extra thread adds extra costs at runtime. Having an extra
thread makes implementing test-driven extra difficult. Having an extra
thread may make a thread-pool necessary, which adds another dependency.

If it was my decision, I would make the backend deal properly with
interrupt requests. However, that's just my two cents.

Regards,
Rüdiger

On 01.04.2012 23:55, Chris Fairhall wrote:
> To implement the second suggested solution you could have your job start
> yet another thread to do the connection test and use Thread.join to wait
> for it to finish. When you interrupt your job thread it will interrupt
> the join so your job can terminate correctly. A new job would use a new
> thread so you could easily run it again while the old thread is still
> active. If you're concerned about too many threads being created you
> could use a thread pool with a blocking queue and an upper limit.
--
Rüdiger Herrmann
http://codeaffine.com
Re: How to correctly cancel a running job [message #835642 is a reply to message #835557] Tue, 03 April 2012 09:51 Go to previous message
George R Malary is currently offline George R Malary
Messages: 36
Registered: March 2012
Location: New Jersey
Member
Yes, there are definitely some design issues to be addressed regarding this. I will have to live with letting the job run in the background, and fail on it's own until it is decided best approach.

I guess no easy solution, but I agree it is best to handle Interrupt at service level, for which we will have to handle Interrupts for all long running service calls. That is probably a good decision regardless, so I would like to go that route.

Thanks for the help and opinions.
Previous Topic:Block Mouse/Key Events during server request
Next Topic:[Solved] Problem Integrating RAP/Spring DM
Goto Forum:
  


Current Time: Thu May 23 23:50:02 EDT 2013

Powered by FUDForum. Page generated in 0.02012 seconds