/** * Simple constructor with the host and port to use to connect to. * <p>This constructor manages the lifecycle of the {@link TcpClient} and * underlying resources such as {@link ConnectionProvider}, * {@link LoopResources}, and {@link ChannelGroup}. * <p>For full control over the initialization and lifecycle of the * TcpClient, use {@link #ReactorNettyTcpClient(TcpClient, ReactorNettyCodec)}. * @param host the host to connect to * @param port the port to connect to * @param codec for encoding and decoding the input/output byte streams * @see org.springframework.messaging.simp.stomp.StompReactorNettyCodec */ public ReactorNettyTcpClient(String host, int port, ReactorNettyCodec<P> codec) { Assert.notNull(host, "host is required"); Assert.notNull(codec, "ReactorNettyCodec is required"); this.channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE); this.loopResources = LoopResources.create("tcp-client-loop"); this.poolResources = ConnectionProvider.elastic("tcp-client-pool"); this.codec = codec; this.tcpClient = TcpClient.create(this.poolResources) .host(host).port(port) .runOn(this.loopResources, false) .doOnConnected(conn -> this.channelGroup.add(conn.channel())); }
/** * Creates a new instance * * @param bindAddress the address to connect to * @param port the port to connect to * @return a new instance * @throws NullPointerException if {@code bindAddress} is {@code null} */ public static TcpClientTransport create(String bindAddress, int port) { Objects.requireNonNull(bindAddress, "bindAddress must not be null"); TcpClient tcpClient = TcpClient.create().host(bindAddress).port(port); return create(tcpClient); }
/** * Creates a new instance * * @param bindAddress the address to connect to * @param port the port to connect to * @return a new instance * @throws NullPointerException if {@code bindAddress} is {@code null} */ public static WebsocketClientTransport create(String bindAddress, int port) { Objects.requireNonNull(bindAddress, "bindAddress must not be null"); TcpClient client = TcpClient.create().host(bindAddress).port(port); return create(client); }
/** * Simple constructor with the host and port to use to connect to. * <p>This constructor manages the lifecycle of the {@link TcpClient} and * underlying resources such as {@link ConnectionProvider}, * {@link LoopResources}, and {@link ChannelGroup}. * <p>For full control over the initialization and lifecycle of the * TcpClient, use {@link #ReactorNettyTcpClient(TcpClient, ReactorNettyCodec)}. * @param host the host to connect to * @param port the port to connect to * @param codec for encoding and decoding the input/output byte streams * @see org.springframework.messaging.simp.stomp.StompReactorNettyCodec */ public ReactorNettyTcpClient(String host, int port, ReactorNettyCodec<P> codec) { Assert.notNull(host, "host is required"); Assert.notNull(codec, "ReactorNettyCodec is required"); this.channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE); this.loopResources = LoopResources.create("tcp-client-loop"); this.poolResources = ConnectionProvider.elastic("tcp-client-pool"); this.codec = codec; this.tcpClient = TcpClient.create(this.poolResources) .host(host).port(port) .runOn(this.loopResources, false) .doOnConnected(conn -> this.channelGroup.add(conn.channel())); }
/** * Creates a new instance * * @param bindAddress the address to connect to * @param port the port to connect to * @return a new instance * @throws NullPointerException if {@code bindAddress} is {@code null} */ public static TcpClientTransport create(String bindAddress, int port) { Objects.requireNonNull(bindAddress, "bindAddress must not be null"); TcpClient tcpClient = TcpClient.create().host(bindAddress).port(port); return create(tcpClient); }
/** * Creates a new instance * * @param bindAddress the address to connect to * @param port the port to connect to * @return a new instance * @throws NullPointerException if {@code bindAddress} is {@code null} */ public static WebsocketClientTransport create(String bindAddress, int port) { Objects.requireNonNull(bindAddress, "bindAddress must not be null"); TcpClient client = TcpClient.create().host(bindAddress).port(port); return create(client); }
@Test public void closingPromiseIsFulfilled() { TcpClient client = TcpClient.newConnection() .host("localhost") .port(abortServerPort); client.handle((in, out) -> Mono.empty()) .wiretap(true) .connectNow() .disposeNow(); }
@Test public void connectionWillRetryConnectionAttemptWhenItFailsElastic() throws InterruptedException { connectionWillRetryConnectionAttemptWhenItFails( TcpClient.create() .host("localhost") .port(abortServerPort + 3) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)); }
@Test public void gettingOptionsDuplicates() { TcpClient client = TcpClient.create().host("foo").port(123); Assertions.assertThat(client.configure()) .isNotSameAs(TcpClient.DEFAULT_BOOTSTRAP) .isNotSameAs(client.configure()); }
private WebsocketClientTransport createRSocketTransport(ClientSettings settings) { String path = "/"; HttpClient httpClient = HttpClient.newConnection() .followRedirect(settings.followRedirect()) .tcpConfiguration( tcpClient -> { if (settings.sslProvider() != null) { tcpClient = tcpClient.secure(settings.sslProvider()); } return tcpClient.runOn(loopResources).host(settings.host()).port(settings.port()); }); return WebsocketClientTransport.create(httpClient, path); }
@Test public void connectionWillRetryConnectionAttemptWhenItFailsFixedChannelPool() throws InterruptedException { connectionWillRetryConnectionAttemptWhenItFails( TcpClient.create(ConnectionProvider.fixed("test", 1)) .host("localhost") .port(abortServerPort + 3) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)); }
@Test public void disableChunkForced2() { Tuple2<HttpResponseStatus, String> r = HttpClient.newConnection() .tcpConfiguration(tcpClient -> tcpClient.host("google.com")) .wiretap(true) .keepAlive(false) .chunkedTransfer(false) .get() .uri("/unsupportedURI") .responseSingle((res, conn) -> Mono.just(res.status()) .zipWith(conn.asString())) .block(Duration.ofSeconds(30)); assertThat(r).isNotNull(); Assert.assertEquals(r.getT1(), HttpResponseStatus.NOT_FOUND); }
@Test public void gettingOptionsDuplicates() { HttpClient client = HttpClient.create() .tcpConfiguration(tcpClient -> tcpClient.host("foo")) .wiretap(true) .port(123) .compress(true); assertThat(client.tcpConfiguration()) .isNotSameAs(HttpClient.DEFAULT_TCP_CLIENT) .isNotSameAs(client.tcpConfiguration()); }
@Test public void testTcpClient1ThreadAcquire() { LoopResources resources = LoopResources.create("test", 1, true); Connection client = TcpClient.create() .host("localhost") .port(echoServerPort) .runOn(resources) .wiretap(true) .connectNow(); client.disposeNow(); resources.dispose(); assertThat("client was configured", client instanceof ChannelOperations); }
@Test public void disableChunkForced() { Tuple2<HttpResponseStatus, String> r = HttpClient.newConnection() .tcpConfiguration(tcpClient -> tcpClient.host("google.com")) .wiretap(true) .chunkedTransfer(false) .request(HttpMethod.GET) .uri("/unsupportedURI") .send(ByteBufFlux.fromString(Flux.just("hello"))) .responseSingle((res, conn) -> Mono.just(res.status()) .zipWith(conn.asString())) .block(Duration.ofSeconds(30)); assertThat(r).isNotNull(); Assert.assertEquals(r.getT1(), HttpResponseStatus.NOT_FOUND); }
@Test public void tcpClientHandlesLineFeedDataElasticPool() throws InterruptedException { Consumer<? super Connection> channelInit = c -> c .addHandler("codec", new LineBasedFrameDecoder(8 * 1024)); tcpClientHandlesLineFeedData( TcpClient.create(ConnectionProvider.elastic("tcpClientHandlesLineFeedDataElasticPool")) .host("localhost") .port(echoServerPort) .doOnConnected(channelInit) ); }
@Test public void simpleTest404_1() { ConnectionProvider pool = ConnectionProvider.fixed("http", 1); HttpClient client = HttpClient.create(pool) .port(80) .tcpConfiguration(tcpClient -> tcpClient.host("google.com")) .wiretap(true); doSimpleTest404(client); doSimpleTest404(client); pool.dispose(); }
@Test public void tcpClientHandlesLineFeedDataFixedPool() throws InterruptedException { Consumer<? super Connection> channelInit = c -> c .addHandler("codec", new LineBasedFrameDecoder(8 * 1024)); // ConnectionProvider p = ConnectionProvider.fixed // ("tcpClientHandlesLineFeedDataFixedPool", 1); ConnectionProvider p = ConnectionProvider.newConnection(); tcpClientHandlesLineFeedData( TcpClient.create(p) .host("localhost") .port(echoServerPort) .doOnConnected(channelInit) ); }