views:

48

answers:

3

Hi All

I have a Runnable running inside a ThreadPoolExecutor long polling on an http request using the HttpClient .

So I am doing the request in a very way :

    httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    clientParams.setSoTimeout(timeout);
    HttpMethodBase method = new GetMethod(request.toString());
    int iGetResultCode = 0;
    iGetResultCode = httpClient.executeMethod(method);

Now I really would like to cancel the polling from an other thread ( or somehow ).

shutdown is not really working , so maybe you know what else could I try.

A: 

Try the next one down in the documentation you linked - shutdownNow();

As the documentation says - this calls Thread.interrupt() on the thread in question which will cause it to throw an InterruptedException - you should properly handle this exception and not ignore it.

Fish
Sorry, this will also not work.shutdown(true) does the same thing.As you know an InterruptException is a checked exception. There is no blocking / waiting operations that is throwing the exception.
Roman
@Roman - have you tried it? I think that `HttpClient.executeMethod(...)` will result in a call to `Socket.connect()`. A `Thread.interrupt()` during the `connect` call should throw an `InterruptedIOException`.
Stephen C
A: 

It is always a bad idea to try to kill a thread.

You should make the thread exit cleanly instead. Add a synchronized boolean field to check, if it is still OK to continue polling.

This still can leave the http request blocking to the socket. It might be OK to just leave this and just ignore the result after it returns. Alternative way is to close the socket from the other thread. (I don't know how to do it in this case.)

iny
You probably dont get it , the request is inside the long polling block == blocking operation , there is no one there to check any state.
Roman
You should be able to check it between the polls. Also note the thing about closing the socket.
iny
The poll is a blocking operation. I want to cancel the poll.There is no between polls
Roman
Then you can add a flag to ignore the result and, if needed, close the socket.
iny
+1  A: 

I'm with Stephen C on this one. httpclient.executeMethod looks like it should react to an InterruptedException (whether IO or other). If you are not seeing any exception propogation you can try closing the InputStream from the HttpClient.

method.getResponseBodyAsStream().close();

This should close the input stream reading from the website and also throw IOExceptions which the thread-pool thread will see. If the connection does close but you still do not see any IOException I would assume you are swallowing an exception somewhere

John V.
There is not response , since it has not returned yet.This will fail.But your point made me search a little bit more , and I found that HttpMethodBase has the simple method abort() ... lol
Roman