这个问题的简短答案是在异步调用完成处理后调用 HttpClient.stop ()。本教程示例中概述了一种解决方法:
- Jetty的HTTP客户端
主要方面是在(所有)侦听器都完成并且处理完成之前,可能不会调用 stop ()。在上面的示例中,
CountdownLatch
用于同步stop()调用。这在Jetty示例中并没有真正解释,例如:
- 新的Jetty 9 HTTP客户端
- Chapter 28. HTTP客户端-异步API
- 类HttpClient
某些异步接口使用诸如“ 消费内容 ”之类的调用。但是,对于Jetty 9,stop()调用会进行清理。
我们的用例很简单。在侦听器的onComplete()方法中,将调用 setCompleted ()方法,该方法将减少
Httpclient.send ()的倒计时锁存器。在send()调用之后,我们有一个名为 完成
()的同步方法,该方法等待对setCompleted()的调用。
public boolean finish(){ boolean result = false; int retryCount = 0; try { if( !this.latch.await( 5, TimeUnit.SEConDS ) ) { Util.warn(LOG,"Timed-out -- msg #", String.format("%0,4d", this.msgId) ); } this.httpClient.stop(); result = this.httpClient.isStopped(); } catch (InterruptedException ex ) { log.warn("Interrupted -- msg #", String.format("%0,4d", this.msgId)" ); } catch (Exception ex) { log.error(LOG,"Stop-exception -- msg #", String.format("%0,4d", this.msgId) ); } if( !result ) { log.warn(" * HttpClient NOT stopped -- msg #", String.format("%0,4d", this.msgId) ); } return result;}只要在我们的onComplete()方法的末尾调用setCompleted(),stop调用似乎就对调用感到满意。我不清楚您是否不确定onComplete的内部时间。否则,一切似乎都令人满意。我真的相信API文档应该包括用于此类操作的“
清理代码 ”-数据通信,数据库等。希望本文能够为他人节省时间。



