The short answer to this question is to call HttpClient.stop () after the asynchronous call completes. This case study describes a solution approach:
The main aspect is that Stop () cannot be called until (all) listeners have finished and completed processing. The above example uses a CountdownLatch to synchronize the stop () call. This is not explained in Jetty examples, for example:
Some asynchronous interfaces use calls, such as consume content, etc. However, with Jetty 9, the stop () call does the cleanup.
Our precedent was fairly straightforward. In the onComplete () method, a call to the setCompleted () method is performed in the listener, which reduces the count delay for Httpclient.send (). After calling send (), we have a synchronization method called completed () that waits for setCompleted () to be called.
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; }
The stop call seems to be pleased with the call while setCompleted () is called at the end of our onComplete () method. I donโt understand if you can be sure of the internal time for onComplete. Otherwise, all this seems satisfactory. I really believe that API documentation should include a โcleanup codeโ for such operations - data, data, etc. Hope this post saves others time.