The example of this article tells the way Java realizes the asynchronous request resources of HTTPClient. Share it for everyone for your reference. The specific implementation method is as follows:
Package demo; import java.util.concurrent.countdownlatch; Import org.apache.httpresponse; Import Get; Import org.apache.http.impl.nio.client.defaultTPasyncclient; Import Org.apache.http.nio.Client.httpasyncclient; Import org.apache.nio.concurrent.futureCallback; Import org.APACHE.HTTP.REACTOR.io. Reactorexception; Public Class main { /** * @param ARGS * @Throws IOREACTOREXCEPTION * @throws InterruptedException */ Public Static Void Main (String [] ARGS) Throws IOREACTOREXCETION, Internet ON {Final httpasynclient httpClient = New DefaultTPasyncClient (); httpClient.start (); httpget [] requests = new httpget Httpget ("http://www.apache.org/"), new httpget ("https://www.verisign.com/"), new httpget ("http://www.google.com/")}}}}}}} ; Final Countdownlatch Latch = New Countdownlatch (Requests.Length); Try {for (Final Httpget Requests) {httpclient.execute (Request, New Fu TURECALLBACK <httpresponse> () {Public Void Completed (Final HTTPRESPONSE Response) {LATCH.COUNTDOWN (); System.out.println (request.getRequestline () + "->" + Response.getstatusline ());} Public void failed (final exception ex) {latch.countdown () ; ex.printstacktrace ();} public void cancelled () {latch.countdown ();}});} System.out.println ("doing ..."); STEM.out .println ("done");}}
It is hoped that this article is helpful to everyone's Java program design.