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 aChannelGroupto hold all active connected channels.static TcpServercreate()Prepare aTcpServer.doOnBind(Consumer<? super TcpServerConfig> doOnBind) Set or add a callback called whenServerTransportis about to start listening for incoming traffic.doOnBound(Consumer<? super DisposableServer> doOnBound) Set or add a callback called afterDisposableServerhas 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 afterDisposableServerhas 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 suppliedEventLoopGroupfrom theLoopResourcescontainer.runOn(LoopResources loopResources, boolean preferNative) Run IO loops on a suppliedEventLoopGroupfrom theLoopResourcescontainer.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 aMonothat 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 operationabsorbs the extra time needed to load resources.wiretap(boolean enable) Apply or remove a wire logger configuration usingTransportcategory (logger),DEBUGlogger level andAdvancedByteBufFormat.HEX_DUMPforByteBufformat, 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),DEBUGlogger level andAdvancedByteBufFormat.HEX_DUMPforByteBufformat, 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_DUMPforByteBufformat, 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, childOptionMethods 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:TransportSet a new local address to which this transport should bind on subscribe.- Overrides:
bindAddressin classTransport<TcpServer,TcpServerConfig> - Parameters:
bindAddressSupplier- A supplier of the address to bind to.- Returns:
- a new
Transport
-
channelGroup
Description copied from class:TransportProvide aChannelGroupto hold all active connected channels.Graceful Shutdown:
When a
ChannelGroupis 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:
channelGroupin classTransport<TcpServer,TcpServerConfig> - Parameters:
channelGroup- aChannelGroup- Returns:
- a new
Transportreference
-
doOnBind
Description copied from class:ServerTransportSet or add a callback called whenServerTransportis about to start listening for incoming traffic.- Overrides:
doOnBindin classServerTransport<TcpServer,TcpServerConfig> - Parameters:
doOnBind- a consumer observing connected events- Returns:
- a new
ServerTransportreference
-
doOnBound
Description copied from class:ServerTransportSet or add a callback called afterDisposableServerhas been started.- Overrides:
doOnBoundin classServerTransport<TcpServer,TcpServerConfig> - Parameters:
doOnBound- a consumer observing connected events- Returns:
- a new
ServerTransportreference
-
doOnConnection
Description copied from class:ServerTransportSet or add a callback called on new remoteConnection.- Overrides:
doOnConnectionin classServerTransport<TcpServer,TcpServerConfig> - Parameters:
doOnConnection- a consumer observing remote connections- Returns:
- a new
ServerTransportreference
-
doOnUnbound
Description copied from class:ServerTransportSet or add a callback called afterDisposableServerhas been shutdown.- Overrides:
doOnUnboundin classServerTransport<TcpServer,TcpServerConfig> - Parameters:
doOnUnbound- a consumer observing unbound events- Returns:
- a new
ServerTransportreference
-
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:ServerTransportThe host to which this server should bind.- Overrides:
hostin classServerTransport<TcpServer,TcpServerConfig> - Parameters:
host- the host to bind to.- Returns:
- a new
ServerTransportreference
-
metrics
Description copied from class:TransportWhether 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:
metricsin classTransport<TcpServer,TcpServerConfig> - Parameters:
enable- true enables metrics collection; false disables it- Returns:
- a new
Transportreference
-
metrics
Description copied from class:TransportSpecifies 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:
metricsin classTransport<TcpServer,TcpServerConfig> - Parameters:
enable- if true enables the metrics on theTransport.recorder- a supplier for theChannelMetricsRecorder- Returns:
- a new
Transportreference
-
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:
portin classServerTransport<TcpServer,TcpServerConfig> - Parameters:
port- The port to bind to.- Returns:
- a new
TcpServer
-
runOn
Description copied from class:TransportRun IO loops on the givenEventLoopGroup.- Overrides:
runOnin classTransport<TcpServer,TcpServerConfig> - Parameters:
eventLoopGroup- an eventLoopGroup to share- Returns:
- a new
Transportreference
-
runOn
Description copied from class:TransportRun IO loops on a suppliedEventLoopGroupfrom theLoopResourcescontainer. Will prefer native (epoll/io_uring/kqueue) implementation if available unless the environment propertyreactor.netty.nativeis set tofalse.- Overrides:
runOnin classTransport<TcpServer,TcpServerConfig> - Parameters:
channelResources- aLoopResourcesaccepting native runtime expectation and returning an eventLoopGroup- Returns:
- a new
Transportreference
-
runOn
Description copied from class:TransportRun IO loops on a suppliedEventLoopGroupfrom theLoopResourcescontainer.- Overrides:
runOnin 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
Transportreference
-
secure
Apply an SSL configuration customization via the passed builder. The builder will produce theSslContextto be passed to with a default value of:10seconds handshake timeout unless the environment propertyreactor.netty.tcp.sslHandshakeTimeoutis set.3seconds close_notify flush timeout0second 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 aMonothat 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 operationabsorbs the extra time needed to load resources.- Overrides:
warmupin classServerTransport<TcpServer,TcpServerConfig> - Returns:
- a
Monorepresenting the completion of the warmup - Since:
- 1.0.3
-
wiretap
Description copied from class:TransportApply or remove a wire logger configuration usingTransportcategory (logger),DEBUGlogger level andAdvancedByteBufFormat.HEX_DUMPforByteBufformat, which means both events and content will be logged and the content will be in hex format.- Overrides:
wiretapin classTransport<TcpServer,TcpServerConfig> - Parameters:
enable- specifies whether the wire logger configuration will be added to the pipeline- Returns:
- a new
Transportreference
-
wiretap
Description copied from class:TransportApply a wire logger configuration using the specified category (logger),DEBUGlogger level andAdvancedByteBufFormat.HEX_DUMPforByteBufformat, which means both events and content will be logged and the content will be in hex format.- Overrides:
wiretapin classTransport<TcpServer,TcpServerConfig> - Parameters:
category- the logger category- Returns:
- a new
Transportreference
-
wiretap
Description copied from class:TransportApply a wire logger configuration using the specified category (logger), logger level andAdvancedByteBufFormat.HEX_DUMPforByteBufformat, which means both events and content will be logged and the content will be in hex format.- Overrides:
wiretapin classTransport<TcpServer,TcpServerConfig> - Parameters:
category- the logger categorylevel- the logger level- Returns:
- a new
Transportreference
-