public abstract class TcpClient extends ClientTransport<TcpClient,TcpClientConfig>
connect()
is ultimately called.
Example:
TcpClient.create()
.doOnConnect(connectMetrics)
.doOnConnected(connectedMetrics)
.doOnDisconnected(disconnectedMetrics)
.host("127.0.0.1")
.port(1234)
.secure()
.connect()
.block()
Constructor and Description |
---|
TcpClient() |
Modifier and Type | Method and Description |
---|---|
TcpClient |
addressSupplier(Supplier<? extends SocketAddress> connectAddressSupplier)
Deprecated.
Use
remoteAddress(Supplier) . This method will be removed in version 1.1.0. |
<A> TcpClient |
attr(AttributeKey<A> key,
A value)
Update the given attribute key or remove it if the value is null.
|
TcpClient |
bindAddress(Supplier<? extends SocketAddress> bindAddressSupplier)
Set a new local address to which this transport should bind on subscribe.
|
TcpClient |
bootstrap(Function<? super Bootstrap,? extends Bootstrap> bootstrapMapper)
Deprecated.
as of 0.9.10. Use the other methods exposed by
TcpClient to achieve the same configurations.
The method will be removed in version 1.1.0. |
Mono<? extends Connection> |
connect()
|
Connection |
connectNow()
Block the
ClientTransport and return a Connection . |
Connection |
connectNow(Duration timeout)
Block the
ClientTransport and return a Connection . |
static TcpClient |
create()
Prepare a pooled
TcpClient . |
static TcpClient |
create(ConnectionProvider provider)
Prepare a
TcpClient . |
TcpClient |
doOnConnect(Consumer<? super TcpClientConfig> doOnConnect)
Set or add a callback called when
ClientTransport is about to connect to the remote endpoint. |
TcpClient |
doOnConnected(Consumer<? super Connection> doOnConnected)
Set or add a callback called after
Connection has been connected. |
TcpClient |
doOnDisconnected(Consumer<? super Connection> doOnDisconnected)
Set or add a callback called after
Connection has been disconnected. |
TcpClient |
handle(BiFunction<? super NettyInbound,? super NettyOutbound,? extends Publisher<Void>> handler)
Attach an IO handler to react on connected client.
|
TcpClient |
host(String host)
The host to which this client should connect.
|
TcpClient |
metrics(boolean enable)
Whether to enable metrics to be collected and registered in Micrometer's
globalRegistry . |
TcpClient |
metrics(boolean enable,
Supplier<? extends ChannelMetricsRecorder> recorder)
Specifies whether the metrics are enabled on the
Transport . |
static TcpClient |
newConnection()
Prepare a non pooled
TcpClient . |
TcpClient |
noProxy()
Remove any previously applied Proxy configuration customization.
|
TcpClient |
noSSL()
Remove any previously applied SSL configuration customization.
|
TcpClient |
observe(ConnectionObserver observer)
Set or add the given
ConnectionObserver to observe the connection state changes. |
<O> TcpClient |
option(ChannelOption<O> key,
O value)
Update the given option key or remove it if the value is null.
|
TcpClient |
port(int port)
The port to which this client should connect.
|
TcpClient |
proxy(Consumer<? super ProxyProvider.TypeSpec> proxyOptions)
Apply a proxy configuration.
|
TcpClient |
remoteAddress(Supplier<? extends SocketAddress> remoteAddressSupplier)
The address to which this client should connect on each subscribe.
|
TcpClient |
resolver(AddressResolverGroup<?> resolver)
Assign an
AddressResolverGroup . |
TcpClient |
runOn(EventLoopGroup eventLoopGroup)
Run IO loops on the given
EventLoopGroup . |
TcpClient |
runOn(LoopResources channelResources)
Run IO loops on a supplied
EventLoopGroup from the LoopResources container. |
TcpClient |
runOn(LoopResources loopResources,
boolean preferNative)
Run IO loops on a supplied
EventLoopGroup from the LoopResources container. |
TcpClient |
secure()
Enable default sslContext support.
|
TcpClient |
secure(Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder)
Apply an SSL configuration customization via the passed builder.
|
TcpClient |
secure(SslProvider sslProvider)
Apply an SSL configuration via the passed
SslProvider . |
Mono<Void> |
warmup()
Based on the actual configuration, returns a
Mono that triggers:
an initialization of the event loop group
an initialization of the host name resolver
loads the necessary native libraries for the transport
loads the necessary native libraries for the security if there is such
By default, when method is not used, the connect operation absorbs the extra time needed to load resources. |
TcpClient |
wiretap(boolean enable)
Apply or remove a wire logger configuration using
Transport category (logger),
DEBUG logger level and AdvancedByteBufFormat.HEX_DUMP for ByteBuf format,
which means both events and content will be logged and the content will be in hex format. |
TcpClient |
wiretap(String category)
Apply a wire logger configuration using the specified category (logger),
DEBUG logger level and AdvancedByteBufFormat.HEX_DUMP for ByteBuf format,
which means both events and content will be logged and the content will be in hex format. |
TcpClient |
wiretap(String category,
LogLevel level)
Apply a wire logger configuration using the specified category (logger),
logger level and
AdvancedByteBufFormat.HEX_DUMP for ByteBuf format,
which means both events and content will be logged and the content will be in hex format. |
doAfterResolve, doOnResolve, doOnResolveError, proxyWithSystemProperties, resolver
channelGroup, configuration, doOnChannelInit, duplicate, wiretap, wiretap
public static TcpClient create(ConnectionProvider provider)
TcpClient
.provider
- a ConnectionProvider
to acquire connectionsTcpClient
public static TcpClient newConnection()
TcpClient
.TcpClient
@Deprecated public final TcpClient addressSupplier(Supplier<? extends SocketAddress> connectAddressSupplier)
remoteAddress(Supplier)
. This method will be removed in version 1.1.0.connectAddressSupplier
- A supplier of the address to connect to.TcpClient
public <A> TcpClient attr(AttributeKey<A> key, @Nullable A value)
Transport
attr
in class Transport<TcpClient,TcpClientConfig>
A
- the attribute typekey
- the AttributeKey
keyvalue
- the AttributeKey
valueTransport
referencepublic TcpClient bindAddress(Supplier<? extends SocketAddress> bindAddressSupplier)
Transport
bindAddress
in class Transport<TcpClient,TcpClientConfig>
bindAddressSupplier
- A supplier of the address to bind to.Transport
@Deprecated public final TcpClient bootstrap(Function<? super Bootstrap,? extends Bootstrap> bootstrapMapper)
TcpClient
to achieve the same configurations.
The method will be removed in version 1.1.0.Bootstrap
mapping function to update TcpClient
configuration and
return an enriched TcpClient
to use.
Note:
There isn't only one method that replaces this deprecated method.
The configuration that can be done with this deprecated method,
can also be done with the other methods exposed by TcpClient
.
Examples:
Configuration via the deprecated '.bootstrap(...)' method
TcpClient.bootstrap(b ->
b.attr(...) // configures the channel attributes
.group(...) // configures the event loop group
.handler(...) // configures the channel handler
.localAddress(...) // configures the bind (local) address
.option(...) // configures the channel options
.remoteAddress(...) // configures the remote address
.resolver(...)) // configures the host names resolver
Configuration via the other methods exposed by TcpClient
TcpClient.attr(...) // configures the channel attributes
.runOn(...) // configures the event loop group
.doOnChannelInit(...) // configures the channel handler
.bindAddress(...) // configures the bind (local) address
.option(...) // configures the channel options
.remoteAddress(...) // configures the remote address
.resolver(...) // configures the host names resolver
Wire logging in plain text
TcpClient.wiretap("logger", LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL)
public Mono<? extends Connection> connect()
ClientTransport
ClientTransport
and return a Mono
of Connection
. If
Mono
is cancelled, the underlying connection will be aborted. Once the
Connection
has been emitted and is not necessary anymore, disposing must be
done by the user via DisposableChannel.dispose()
.connect
in class ClientTransport<TcpClient,TcpClientConfig>
Mono
of Connection
public final Connection connectNow()
ClientTransport
ClientTransport
and return a Connection
. Disposing must be
done by the user via DisposableChannel.dispose()
. The max connection
timeout is 45 seconds.connectNow
in class ClientTransport<TcpClient,TcpClientConfig>
Connection
public final Connection connectNow(Duration timeout)
ClientTransport
ClientTransport
and return a Connection
. Disposing must be
done by the user via DisposableChannel.dispose()
.connectNow
in class ClientTransport<TcpClient,TcpClientConfig>
timeout
- connect timeout (resolution: ns)Connection
public TcpClient doOnConnect(Consumer<? super TcpClientConfig> doOnConnect)
ClientTransport
ClientTransport
is about to connect to the remote endpoint.doOnConnect
in class ClientTransport<TcpClient,TcpClientConfig>
doOnConnect
- a consumer observing connect eventsClientTransport
referencepublic TcpClient doOnConnected(Consumer<? super Connection> doOnConnected)
ClientTransport
Connection
has been connected.doOnConnected
in class ClientTransport<TcpClient,TcpClientConfig>
doOnConnected
- a consumer observing connected eventsClientTransport
referencepublic TcpClient doOnDisconnected(Consumer<? super Connection> doOnDisconnected)
ClientTransport
Connection
has been disconnected.doOnDisconnected
in class ClientTransport<TcpClient,TcpClientConfig>
doOnDisconnected
- a consumer observing disconnected eventsClientTransport
referencepublic TcpClient handle(BiFunction<? super NettyInbound,? super NettyOutbound,? extends Publisher<Void>> handler)
public TcpClient host(String host)
ClientTransport
host
in class ClientTransport<TcpClient,TcpClientConfig>
host
- the host to connect toClientTransport
referencepublic TcpClient metrics(boolean enable)
Transport
globalRegistry
.
Applications can separately register their own
filters
.
For example, to put an upper bound on the number of tags produced:
MeterFilter filter = ... ; Metrics.globalRegistry.config().meterFilter(MeterFilter.maximumAllowableTags(prefix, 100, filter));
By default this is not enabled.
metrics
in class Transport<TcpClient,TcpClientConfig>
enable
- true enables metrics collection; false disables itTransport
referencepublic TcpClient metrics(boolean enable, Supplier<? extends ChannelMetricsRecorder> recorder)
Transport
Transport
.
All generated metrics are provided to the specified recorder
which is only instantiated if metrics are being enabled (the instantiation is not lazy,
but happens immediately, while configuring the Transport
).metrics
in class Transport<TcpClient,TcpClientConfig>
enable
- if true enables the metrics on the Transport
.recorder
- a supplier for the ChannelMetricsRecorder
Transport
referencepublic TcpClient noProxy()
ClientTransport
noProxy
in class ClientTransport<TcpClient,TcpClientConfig>
ClientTransport
referencepublic TcpClient noSSL()
TcpClient
public TcpClient observe(ConnectionObserver observer)
Transport
ConnectionObserver
to observe the connection state changes.observe
in class Transport<TcpClient,TcpClientConfig>
observer
- the ConnectionObserver
to be set or addTransport
referencepublic <O> TcpClient option(ChannelOption<O> key, @Nullable O value)
Transport
ChannelOption.AUTO_READ
option will be ignored. It is configured to be false
.option
in class Transport<TcpClient,TcpClientConfig>
O
- the option typekey
- the ChannelOption
keyvalue
- the ChannelOption
value or nullTransport
referencepublic TcpClient port(int port)
ClientTransport
port
in class ClientTransport<TcpClient,TcpClientConfig>
port
- the port to connect toClientTransport
referencepublic TcpClient proxy(Consumer<? super ProxyProvider.TypeSpec> proxyOptions)
ClientTransport
proxy
in class ClientTransport<TcpClient,TcpClientConfig>
proxyOptions
- the proxy configuration callbackClientTransport
referencepublic TcpClient remoteAddress(Supplier<? extends SocketAddress> remoteAddressSupplier)
ClientTransport
remoteAddress
in class ClientTransport<TcpClient,TcpClientConfig>
remoteAddressSupplier
- A supplier of the address to connect to.ClientTransport
public TcpClient resolver(AddressResolverGroup<?> resolver)
ClientTransport
AddressResolverGroup
.resolver
in class ClientTransport<TcpClient,TcpClientConfig>
resolver
- the new AddressResolverGroup
ClientTransport
referencepublic TcpClient runOn(EventLoopGroup eventLoopGroup)
Transport
EventLoopGroup
.runOn
in class Transport<TcpClient,TcpClientConfig>
eventLoopGroup
- an eventLoopGroup to shareTransport
referencepublic TcpClient runOn(LoopResources channelResources)
Transport
EventLoopGroup
from the LoopResources
container.
Will prefer native (epoll/kqueue) implementation if available
unless the environment property reactor.netty.native
is set to false
.runOn
in class Transport<TcpClient,TcpClientConfig>
channelResources
- a LoopResources
accepting native runtime expectation and
returning an eventLoopGroupTransport
referencepublic TcpClient runOn(LoopResources loopResources, boolean preferNative)
Transport
EventLoopGroup
from the LoopResources
container.runOn
in class ClientTransport<TcpClient,TcpClientConfig>
loopResources
- a new loop resourcespreferNative
- should prefer running on epoll, kqueue or similar instead of java NIOTransport
referencepublic TcpClient secure()
SslContext
will be
assigned to with a default value of:
10
seconds handshake timeout unless the environment property
reactor.netty.tcp.sslHandshakeTimeout
is set.3
seconds close_notify flush timeout0
second close_notify read timeoutTcpClient
public TcpClient secure(Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder)
SslContext
to be passed to with a default value of:
10
seconds handshake timeout unless the environment property
reactor.netty.tcp.sslHandshakeTimeout
is set.3
seconds close_notify flush timeout0
second close_notify read timeoutsslProviderBuilder
- builder callback for further customization of SslContext.TcpClient
public TcpClient secure(SslProvider sslProvider)
SslProvider
.sslProvider
- The provider to set when configuring SSLTcpClient
public Mono<Void> warmup()
Mono
that triggers:
connect operation
absorbs the extra time needed to load resources.warmup
in class ClientTransport<TcpClient,TcpClientConfig>
Mono
representing the completion of the warmuppublic TcpClient wiretap(boolean enable)
Transport
Transport
category (logger),
DEBUG
logger level and AdvancedByteBufFormat.HEX_DUMP
for ByteBuf
format,
which means both events and content will be logged and the content will be in hex format.wiretap
in class Transport<TcpClient,TcpClientConfig>
enable
- specifies whether the wire logger configuration will be added to the pipelineTransport
referencepublic TcpClient wiretap(String category)
Transport
DEBUG
logger level and AdvancedByteBufFormat.HEX_DUMP
for ByteBuf
format,
which means both events and content will be logged and the content will be in hex format.wiretap
in class Transport<TcpClient,TcpClientConfig>
category
- the logger categoryTransport
referencepublic TcpClient wiretap(String category, LogLevel level)
Transport
AdvancedByteBufFormat.HEX_DUMP
for ByteBuf
format,
which means both events and content will be logged and the content will be in hex format.wiretap
in class Transport<TcpClient,TcpClientConfig>
category
- the logger categorylevel
- the logger levelTransport
reference