简体   繁体   English

取消调用方线程的HttpClient行为

[英]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. 我们有一个可调用的class A ,它实际上通过HttpClient.executeMethod(GetMethod)与许多其他预计算进行HttpCalls。 HttpClient is initialized in the constructor with MultiThreadedHttpConnectionManager . 使用MultiThreadedHttpConnectionManager在构造函数中初始化HttpClient。

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. 另一个class B通过ExecutorService为类A创建线程列表,并将任务提交给池,并期望将来返回对象。 We have following logic in class B: 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? 如果通过HttpClient建立了外部连接,该线程的future.cancel如何处理?
  2. In above case or in general, does the http connection pool gets the connection back by properly releasing the previous one? 在上述情况下或通常情况下,http连接池是否通过正确释放前一个连接池来恢复连接? 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. 如果Http Client使用java.net.Socket,其I / O不会中断,因此取消将无效。

  • If it uses NIO, the interrupt will close the channel and cause an exception. 如果使用NIO,则中断将关闭通道并导致异常。 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. 在服务器上,这将导致流过早结束或写入异常,服务器应严格解决这两种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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