vertx core http 客户端 例子
所属分类 vertx
浏览量 342
vertx4
https://vertx-china.github.io/docs/4.2.7/vertx-core/java/
import io.vertx.core.AsyncResult;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;
import java.time.LocalDateTime;
public class VertxHttpClientDemo {
static Vertx vertx = Vertx.vertx();
public static void main(String[] args) throws Exception {
HttpClientOptions options = new HttpClientOptions().setKeepAlive(true).setMaxPoolSize(10);
HttpClient client = vertx.createHttpClient(options);
System.out.println(LocalDateTime.now());
client.request(HttpMethod.GET, 8000, "127.0.0.1", "/hello?sleep=2000", (AsyncResult< HttpClientRequest > ar) -> {
System.out.println(LocalDateTime.now());
System.out.println(ar);
if (ar.succeeded()) {
HttpClientRequest request = ar.result();
request.send(ar2 -> {
System.out.println(LocalDateTime.now());
System.out.println(ar2);
if (ar2.succeeded()) {
HttpClientResponse response = ar2.result();
System.out.println(response.statusCode());
response.body((AsyncResult< Buffer > ar3) -> {
if (ar2.succeeded()) {
Buffer body = ar3.result();
System.out.println("Total response body length is " + body.length());
System.out.println(body.toString());
}
});
}
});
}
});
Thread.sleep(3000);
// 优雅版本
client.request(HttpMethod.GET, 8000, "127.0.0.1", "/hello?sleep=2000")
.compose(HttpClientRequest::send)
.compose(HttpClientResponse::body)
.onSuccess(System.out::println);
}
}
上一篇
下一篇
vertx future高级组合操作
vertx future 与 CompletionStage 互操作
vertx webclient 使用
MQTT协议简介
MQTT 术语
物联网网关和边缘计算