public class AsyncTest{
public static HttpAsyncClient httpAsyncClient;
public static CompletableFuture<String> getHttpData(String url){
CompletableFuture asyncFuture = new completableFuture();
HttpAsyncRequestProducer producer = HttpAsyncMethods.create(new HttpPost(url));
BasicAsyncResponseConsumer consumer = new BasicAsyncResponseConsumer();
FutureCallback callback = new FutureCallback<>(HttpResponse){
public void completed(HttpResponse response){
asyncFuture.complete(response);
}
public void failed(Exception e){
asyncFuture.completeExceptionally(e);
}
public void cancelled(){
asyncFuture.cancel(true);
}
}
httpAsyncClient.execute(producer,consumer,callback);
return asyncFuture;
}
public static void main(String[] args) throws Exception{
CompletableFuture<> future = AsyncTest.getHttpData("http://www.xx.com");
String result = future.get();
}
}
02-03
680