Package reactor.netty.tcp
Class TcpServer
A TcpServer allows building in a safe immutable way a TCP server that is materialized
and connecting when
ServerTransport.bind()
is ultimately called.
Example:
TcpServer.create()
.doOnBind(startMetrics)
.doOnBound(startedMetrics)
.doOnUnbound(stopMetrics)
.host("127.0.0.1")
.port(1234)
.bind()
.block()
- Author:
- Stephane Maldini, Violeta Georgieva
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbindAddress
(Supplier<? extends SocketAddress> bindAddressSupplier) Set a new local address to which this transport should bind on subscribe.channelGroup
(ChannelGroup channelGroup) Provide aChannelGroup
to hold all active connected channels.static TcpServer
create()
Prepare aTcpServer
.doOnBind
(Consumer<? super TcpServerConfig> doOnBind) Set or add a callback called whenServerTransport
is about to start listening for incoming traffic.doOnBound
(Consumer<? super DisposableServer> doOnBound) Set or add a callback called afterDisposableServer
has been started.doOnConnection
(Consumer<? super Connection> doOnConnection) Set or add a callback called on new remoteConnection
.doOnUnbound
(Consumer<? super DisposableServer> doOnUnbound) Set or add a callback called afterDisposableServer
has been shutdown.handle
(BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> handler) Attaches an I/O handler to react on a connected client.The host to which this server should bind.metrics
(boolean enable) Whether to enable metrics to be collected and registered in Micrometer'sglobalRegistry
.metrics
(boolean enable, Supplier<? extends ChannelMetricsRecorder> recorder) Specifies whether the metrics are enabled on theTransport
.noSSL()
Removes any previously applied SSL configuration customization.port
(int port) The port to which this server should bind.runOn
(EventLoopGroup eventLoopGroup) Run IO loops on the givenEventLoopGroup
.runOn
(LoopResources channelResources) Run IO loops on a suppliedEventLoopGroup
from theLoopResources
container.runOn
(LoopResources loopResources, boolean preferNative) Run IO loops on a suppliedEventLoopGroup
from theLoopResources
container.secure
(Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder) Apply an SSL configuration customization via the passed builder.secure
(SslProvider sslProvider) Applies an SSL configuration via the passedSslProvider
.warmup()
Based on the actual configuration, returns aMono
that triggers: an initialization of the event loop groups 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, thebind operation
absorbs the extra time needed to load resources.wiretap
(boolean enable) Apply or remove a wire logger configuration usingTransport
category (logger),DEBUG
logger level andAdvancedByteBufFormat.HEX_DUMP
forByteBuf
format, which means both events and content will be logged and the content will be in hex format.Apply a wire logger configuration using the specified category (logger),DEBUG
logger level andAdvancedByteBufFormat.HEX_DUMP
forByteBuf
format, which means both events and content will be logged and the content will be in hex format.Apply a wire logger configuration using the specified category (logger), logger level andAdvancedByteBufFormat.HEX_DUMP
forByteBuf
format, which means both events and content will be logged and the content will be in hex format.Methods inherited from class reactor.netty.transport.ServerTransport
bind, bindNow, bindNow, bindUntilJavaShutdown, childAttr, childObserve, childOption
Methods inherited from class reactor.netty.transport.Transport
attr, configuration, doOnChannelInit, duplicate, observe, option, wiretap, wiretap
-
Constructor Details
-
TcpServer
public TcpServer()
-
-
Method Details
-
create
Prepare aTcpServer
.- Returns:
- a new
TcpServer
-
bindAddress
Description copied from class:Transport
Set a new local address to which this transport should bind on subscribe.- Overrides:
bindAddress
in classTransport<TcpServer,
TcpServerConfig> - Parameters:
bindAddressSupplier
- A supplier of the address to bind to.- Returns:
- a new
Transport
-
channelGroup
Description copied from class:Transport
Provide aChannelGroup
to hold all active connected channels.Graceful Shutdown:
When a
ChannelGroup
is set, calls toDisposableChannel.disposeNow()
andDisposableChannel.disposeNow(Duration)
not only stop accepting new requests but also additionally wait for all active requests, in theChannelGroup
, to complete, within the given timeout.- Overrides:
channelGroup
in classTransport<TcpServer,
TcpServerConfig> - Parameters:
channelGroup
- aChannelGroup
- Returns:
- a new
Transport
reference
-
doOnBind
Description copied from class:ServerTransport
Set or add a callback called whenServerTransport
is about to start listening for incoming traffic.- Overrides:
doOnBind
in classServerTransport<TcpServer,
TcpServerConfig> - Parameters:
doOnBind
- a consumer observing connected events- Returns:
- a new
ServerTransport
reference
-
doOnBound
Description copied from class:ServerTransport
Set or add a callback called afterDisposableServer
has been started.- Overrides:
doOnBound
in classServerTransport<TcpServer,
TcpServerConfig> - Parameters:
doOnBound
- a consumer observing connected events- Returns:
- a new
ServerTransport
reference
-
doOnConnection
Description copied from class:ServerTransport
Set or add a callback called on new remoteConnection
.- Overrides:
doOnConnection
in classServerTransport<TcpServer,
TcpServerConfig> - Parameters:
doOnConnection
- a consumer observing remote connections- Returns:
- a new
ServerTransport
reference
-
doOnUnbound
Description copied from class:ServerTransport
Set or add a callback called afterDisposableServer
has been shutdown.- Overrides:
doOnUnbound
in classServerTransport<TcpServer,
TcpServerConfig> - Parameters:
doOnUnbound
- a consumer observing unbound events- Returns:
- a new
ServerTransport
reference
-
handle
public TcpServer handle(BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> handler) Attaches an I/O handler to react on a connected client. -
host
Description copied from class:ServerTransport
The host to which this server should bind.- Overrides:
host
in classServerTransport<TcpServer,
TcpServerConfig> - Parameters:
host
- the host to bind to.- Returns:
- a new
ServerTransport
reference
-
metrics
Description copied from class:Transport
Whether to enable metrics to be collected and registered in Micrometer'sglobalRegistry
. Applications can separately register their ownfilters
. 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.
- Overrides:
metrics
in classTransport<TcpServer,
TcpServerConfig> - Parameters:
enable
- true enables metrics collection; false disables it- Returns:
- a new
Transport
reference
-
metrics
Description copied from class:Transport
Specifies whether the metrics are enabled on theTransport
. 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 theTransport
).- Overrides:
metrics
in classTransport<TcpServer,
TcpServerConfig> - Parameters:
enable
- if true enables the metrics on theTransport
.recorder
- a supplier for theChannelMetricsRecorder
- Returns:
- a new
Transport
reference
-
noSSL
Removes any previously applied SSL configuration customization.- Returns:
- a new
TcpServer
-
port
The port to which this server should bind. If a port is not specified, the system picks up an ephemeral port.- Overrides:
port
in classServerTransport<TcpServer,
TcpServerConfig> - Parameters:
port
- The port to bind to.- Returns:
- a new
TcpServer
-
runOn
Description copied from class:Transport
Run IO loops on the givenEventLoopGroup
.- Overrides:
runOn
in classTransport<TcpServer,
TcpServerConfig> - Parameters:
eventLoopGroup
- an eventLoopGroup to share- Returns:
- a new
Transport
reference
-
runOn
Description copied from class:Transport
Run IO loops on a suppliedEventLoopGroup
from theLoopResources
container. Will prefer native (epoll/io_uring/kqueue) implementation if available unless the environment propertyreactor.netty.native
is set tofalse
.- Overrides:
runOn
in classTransport<TcpServer,
TcpServerConfig> - Parameters:
channelResources
- aLoopResources
accepting native runtime expectation and returning an eventLoopGroup- Returns:
- a new
Transport
reference
-
runOn
Description copied from class:Transport
Run IO loops on a suppliedEventLoopGroup
from theLoopResources
container.- Overrides:
runOn
in classTransport<TcpServer,
TcpServerConfig> - Parameters:
loopResources
- a new loop resourcespreferNative
- should prefer running on epoll, io_uring, kqueue or similar instead of java NIO- Returns:
- a new
Transport
reference
-
secure
Apply an SSL configuration customization via the passed builder. The builder will produce theSslContext
to be passed to with a default value of:10
seconds handshake timeout unless the environment propertyreactor.netty.tcp.sslHandshakeTimeout
is set.3
seconds close_notify flush timeout0
second close_notify read timeout
X509Bundle cert = new CertificateBuilder().subject("CN=localhost").setIsCertificateAuthority(true).buildSelfSigned(); TcpSslContextSpec tcpSslContextSpec = TcpSslContextSpec.forServer(cert.toTempCertChainPem(), cert.toTempPrivateKeyPem()); secure(sslContextSpec -> sslContextSpec.sslContext(tcpSslContextSpec));
- Parameters:
sslProviderBuilder
- builder callback for further customization of SslContext.- Returns:
- a new
TcpServer
-
secure
Applies an SSL configuration via the passedSslProvider
. If self-signed certificate needs to be used, the sample below can be used (the functionality is provided by io.netty:netty-pkitesting). Note that self-signed certificate should not be used in production.X509Bundle cert = new CertificateBuilder().subject("CN=localhost").setIsCertificateAuthority(true).buildSelfSigned(); TcpSslContextSpec tcpSslContextSpec = TcpSslContextSpec.forServer(cert.toTempCertChainPem(), cert.toTempPrivateKeyPem()); secure(sslContextSpec -> sslContextSpec.sslContext(tcpSslContextSpec));
- Parameters:
sslProvider
- The provider to set when configuring SSL- Returns:
- a new
TcpServer
-
warmup
Based on the actual configuration, returns aMono
that triggers:- an initialization of the event loop groups
- loads the necessary native libraries for the transport
- loads the necessary native libraries for the security if there is such
bind operation
absorbs the extra time needed to load resources.- Overrides:
warmup
in classServerTransport<TcpServer,
TcpServerConfig> - Returns:
- a
Mono
representing the completion of the warmup - Since:
- 1.0.3
-
wiretap
Description copied from class:Transport
Apply or remove a wire logger configuration usingTransport
category (logger),DEBUG
logger level andAdvancedByteBufFormat.HEX_DUMP
forByteBuf
format, which means both events and content will be logged and the content will be in hex format.- Overrides:
wiretap
in classTransport<TcpServer,
TcpServerConfig> - Parameters:
enable
- specifies whether the wire logger configuration will be added to the pipeline- Returns:
- a new
Transport
reference
-
wiretap
Description copied from class:Transport
Apply a wire logger configuration using the specified category (logger),DEBUG
logger level andAdvancedByteBufFormat.HEX_DUMP
forByteBuf
format, which means both events and content will be logged and the content will be in hex format.- Overrides:
wiretap
in classTransport<TcpServer,
TcpServerConfig> - Parameters:
category
- the logger category- Returns:
- a new
Transport
reference
-
wiretap
Description copied from class:Transport
Apply a wire logger configuration using the specified category (logger), logger level andAdvancedByteBufFormat.HEX_DUMP
forByteBuf
format, which means both events and content will be logged and the content will be in hex format.- Overrides:
wiretap
in classTransport<TcpServer,
TcpServerConfig> - Parameters:
category
- the logger categorylevel
- the logger level- Returns:
- a new
Transport
reference
-