/** * 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 connecting to localhost * * @param port the port to connect to * @return a new instance */ public static TcpClientTransport create(int port) { TcpClient tcpClient = TcpClient.create().port(port); return create(tcpClient); }
/** * Creates a new instance connecting to localhost * * @param port the port to connect to * @return a new instance */ public static WebsocketClientTransport create(int port) { TcpClient client = TcpClient.create().port(port); return create(client); }
/** * 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())); }
/** * The port to which this client should connect. * * @param port The port to connect to. * * @return a new {@link HttpClient} */ public final HttpClient port(int port) { return tcpConfiguration(tcpClient -> tcpClient.port(port)); }
/** * The port to which this client should connect. * * @param port The port to connect to. * * @return a new {@link HttpClient} */ public final HttpClient port(int port) { return tcpConfiguration(tcpClient -> tcpClient.port(port)); }
/** * Prepare an {@link HttpClient}. {@link UriConfiguration#uri(String)} or * {@link #baseUrl(String)} should be invoked before a verb * {@link #request(HttpMethod)} is selected. * * @return a {@link HttpClient} */ public static HttpClient create(ConnectionProvider connectionProvider) { return new HttpClientConnect(TcpClient.create(connectionProvider) .port(80)); }
/** * Creates a new instance connecting to localhost * * @param port the port to connect to * @return a new instance */ public static WebsocketClientTransport create(int port) { TcpClient client = TcpClient.create().port(port); return create(client); }
/** * Creates a new instance connecting to localhost * * @param port the port to connect to * @return a new instance */ public static TcpClientTransport create(int port) { TcpClient tcpClient = TcpClient.create().port(port); return create(tcpClient); }
/** * Prepare an {@link HttpClient}. {@link UriConfiguration#uri(String)} or * {@link #baseUrl(String)} should be invoked before a verb * {@link #request(HttpMethod)} is selected. * * @return a {@link HttpClient} */ public static HttpClient create(ConnectionProvider connectionProvider) { return new HttpClientConnect(TcpClient.create(connectionProvider) .port(80)); }
@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()); }
@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 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 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) ); }