| How to correctly cancel a running job [message #833015] |
Fri, 30 March 2012 17:36  |
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   |
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 #835642 is a reply to message #835557] |
Tue, 03 April 2012 09:51  |
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.
|
|
|
Powered by
FUDForum. Page generated in 0.02012 seconds