简体   繁体   中英

Behavior of HttpClient with caller thread being cancelled

We have a callable class A which actually makes HttpCalls through HttpClient.executeMethod(GetMethod) with a lot of other pre-computations. HttpClient is initialized in the constructor with MultiThreadedHttpConnectionManager .

Another class B creates list of threads for class A through ExecutorService and submits task to the pool and expects future objects to be returned. We have following logic in class B:

for( Future f : futures ){
try{
String str = f.get(timeOut, TimeUnit.SECONDS);
}catch(TimeoutException te){
f.cancel(true);
}
}

This way, our thread gets terminated after a specified time and execution of the task will be terminated and this thread will be available for next task.

I want to confirm the following:

  1. If an external connection is made though HttpClient, how does that get handled on future.cancel of the thread?
  2. In above case or in general, does the http connection pool gets the connection back by properly releasing the previous one? We do release the connection in finally but I don't think interrupting the thread will hit that block.
  3. Could it cause any kind of leak on client or extra resource consumption on the server?

Thanks!

It depends.

  • If the Http Client uses java.net.Socket, its I/O isn't interrruptible, so the cancel will have no effect.

  • If it uses NIO, the interrupt will close the channel and cause an exception. At the server this will cause a premature end of stream or an exception on write, either of which the server should cope with corectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM