本文整理汇总了Java中reactor.ipc.netty.http.client.HttpClient.create方法的典型用法代码示例。如果您正苦于以下问题:Java HttpClient.create方法的具体用法?Java HttpClient.create怎么用?Java HttpClient.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reactor.ipc.netty.http.client.HttpClient
的用法示例。
在下文中一共展示了HttpClient.create方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: httpRespondsEmpty
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
@Test
public void httpRespondsEmpty() {
NettyContext server =
HttpServer.create(0)
.newRouter(r ->
r.post("/test/{param}", (req, res) -> Mono.empty()))
.block(Duration.ofSeconds(30));
HttpClient client =
HttpClient.create("localhost", server.address().getPort());
Mono<ByteBuf> content = client
.post("/test/World", req -> req.header("Content-Type", "text/plain")
.sendString(Mono.just("Hello")
.log("client-send")))
.flatMap(res -> res.receive()
.log("client-received")
.next())
.doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage()));
StepVerifier.create(content)
.expectComplete()
.verify(Duration.ofSeconds(5000));
server.dispose();
}
示例2: testIssue186
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
@Test
public void testIssue186() {
NettyContext server =
HttpServer.create(0)
.newHandler((req, res) -> res.status(200).send())
.block(Duration.ofSeconds(300));
HttpClient client =
HttpClient.create(ops -> ops.connectAddress(() -> server.address())
.poolResources(PoolResources.fixed("test", 1)));
try {
doTestIssue186(client);
doTestIssue186(client);
}
finally {
server.dispose();
}
}
示例3: testIssue196
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
@Test
@Ignore
public void testIssue196() throws Exception {
ExecutorService threadPool = Executors.newCachedThreadPool();
int testServerPort = SocketUtils.findAvailableTcpPort();
TestServer testServer = new TestServer(testServerPort);
threadPool.submit(testServer);
if(!testServer.await(10, TimeUnit.SECONDS)){
throw new IOException("Fail to start test server");
}
HttpClient client =
HttpClient.create(opt -> opt.port(testServerPort)
.poolResources(PoolResources.fixed("test", 1)));
Flux.range(0, 2)
.flatMap(i -> client.get("/205")
.flatMap(res -> res.receive()
.aggregate()
.asString()))
.blockLast(Duration.ofSeconds(100));
testServer.close();
}
示例4: nettyNetChannelAcceptsNettyChannelHandlers
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
@Test
public void nettyNetChannelAcceptsNettyChannelHandlers() throws InterruptedException {
HttpClient client = HttpClient.create();
final CountDownLatch latch = new CountDownLatch(1);
System.out.println(client.get("http://www.google.com/?q=test%20d%20dq")
.flatMap(r -> r.receive()
.asString()
.collectList())
.doOnSuccess(v -> latch.countDown())
.block(Duration.ofSeconds(30)));
assertTrue("Latch didn't time out", latch.await(15, TimeUnit.SECONDS));
}
示例5: getClientDataPromise
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
private Mono<List<String>> getClientDataPromise() throws Exception {
HttpClient httpClient = HttpClient.create("localhost", httpServer.address()
.getPort());
return httpClient.get("/data")
.flatMapMany(s -> s.receive()
.asString()
.log("client")
.next())
.collectList()
.cache()
.toProcessor();
}
示例6: httpRespondsToRequestsFromClients
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
@Test
public void httpRespondsToRequestsFromClients() {
NettyContext server =
HttpServer.create(0)
.newRouter(r ->
r.post("/test/{param}", (req, res) ->
res.sendString(req.receive()
.asString()
.log("server-received")
.map(it -> it + ' ' + req.param("param") + '!')
.log("server-reply"))))
.block(Duration.ofSeconds(30));
HttpClient client =
HttpClient.create("localhost", server.address().getPort());
Mono<String> content =
client.post("/test/World", req -> req.header("Content-Type", "text/plain")
.sendString(Flux.just("Hello")
.log("client-send")))
.flatMap(res -> res.receive()
.aggregate()
.asString()
.log("client-received"))
.doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage()));
StepVerifier.create(content)
.expectNextMatches(s -> s.equals("Hello World!"))
.expectComplete()
.verify(Duration.ofSeconds(5000));
server.dispose();
}
示例7: getClientDataPromise
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
private List<String> getClientDataPromise() throws Exception {
HttpClient httpClient =
HttpClient.create(opts -> opts.host("localhost")
.port(httpServer.address().getPort()));
Mono<List<String>> content = httpClient.get("/data")
.flatMap(f -> f.receive()
.asString()
.collectList())
.cache();
List<String> res = content.block(Duration.ofSeconds(30));
return res;
}
示例8: test
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
@Test
public void test() {
NettyContext server = HttpServer.create(0)
.newRouter(httpServerRoutes -> httpServerRoutes.get(
"/",
(httpServerRequest, httpServerResponse) -> {
return httpServerResponse.sendString(
Mono.error(new IllegalArgumentException()));
}))
.block(Duration.ofSeconds(30));
HttpClient client = HttpClient.create(opt -> opt.host("localhost")
.port(server.address().getPort())
.disablePool());
HttpClientResponse r = client.get("/")
.block(Duration.ofSeconds(30));
Mono<List<String>> result = r.receive()
.asString(StandardCharsets.UTF_8)
.collectList();
StepVerifier.create(result)
.expectError(IOException.class)
.verify(Duration.ofSeconds(30));
System.out.println("END");
FutureMono.from(r.context().channel().closeFuture()).block(Duration.ofSeconds(30));
r.dispose();
server.dispose();
}
示例9: httpStatusCode404IsHandledByTheClient
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
@Test
public void httpStatusCode404IsHandledByTheClient() {
NettyContext server =
HttpServer.create(0)
.newRouter(r -> r.post("/test", (req, res) -> res.send(req.receive()
.log("server-received"))))
.block(Duration.ofSeconds(30));
HttpClient client = HttpClient.create("localhost", server.address().getPort());
List<String> replyReceived = new ArrayList<>();
Mono<String> content = client.get("/unsupportedURI", req ->
req.addHeader("Content-Type", "text/plain")
.sendString(Flux.just("Hello")
.log("client-send"))
)
.flatMapMany(res -> res.receive()
.asString()
.log("client-received")
.doOnNext(s -> replyReceived.add(s)))
.next()
.doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage()));
StepVerifier.create(content)
.expectErrorMatches(t -> t.getMessage().equals("HTTP request failed with code: 404.\nFailing URI: " +
"/unsupportedURI"))
.verify(Duration.ofSeconds(30));
Assertions.assertThat(replyReceived).isEmpty();
server.dispose();
}
示例10: httpClient
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
@Bean
@ConditionalOnMissingBean
public HttpClient httpClient(@Qualifier("nettyClientOptions") Consumer<? super HttpClientOptions.Builder> options) {
return HttpClient.create(options);
}
示例11: webSocketRespondsToRequestsFromClients
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
@Test
public void webSocketRespondsToRequestsFromClients() {
AtomicInteger clientRes = new AtomicInteger();
AtomicInteger serverRes = new AtomicInteger();
NettyContext server =
HttpServer.create(0)
.newRouter(r -> r.get("/test/{param}", (req, res) -> {
System.out.println(req.requestHeaders().get("test"));
return res.header("content-type", "text/plain")
.sendWebsocket((in, out) ->
out.options(c -> c.flushOnEach())
.sendString(in.receive()
.asString()
.publishOn(Schedulers.single())
.doOnNext(s -> serverRes.incrementAndGet())
.map(it -> it + ' ' + req.param("param") + '!')
.log("server-reply")));
}))
.block(Duration.ofSeconds(5));
HttpClient client = HttpClient.create("localhost", server.address().getPort());
Mono<List<String>> response =
client.get("/test/World", req ->
req.header("Content-Type", "text/plain")
.header("test", "test")
.options(c -> c.flushOnEach())
.sendWebsocket()
.sendString(Flux.range(1, 1000)
.log("client-send")
.map(i -> "" + i)))
.flatMapMany(res -> res.receive()
.asString()
.log("client-received")
.publishOn(Schedulers.parallel())
.doOnNext(s -> clientRes.incrementAndGet()))
.take(1000)
.collectList()
.cache()
.doOnError(i -> System.err.println("Failed requesting server: " + i));
System.out.println("STARTING: server[" + serverRes.get() + "] / client[" + clientRes.get() + "]");
StepVerifier.create(response)
.expectNextMatches(list -> "1000 World!".equals(list.get(999)))
.expectComplete()
.verify(Duration.ofSeconds(30));
System.out.println("FINISHED: server[" + serverRes.get() + "] / client[" + clientRes + "]");
server.dispose();
}
示例12: httpClient
import reactor.ipc.netty.http.client.HttpClient; //导入方法依赖的package包/类
@Bean
HttpClient httpClient() {
return HttpClient.create();
}