public abstract class TcpServer extends ServerTransport<TcpServer,TcpServerConfig>
ServerTransport.bind()
is ultimately called.
Example:
TcpServer.create()
.doOnBind(startMetrics)
.doOnBound(startedMetrics)
.doOnUnbound(stopMetrics)
.host("127.0.0.1")
.port(1234)
.bind()
.block()
Constructor and Description |
---|
TcpServer() |
Modifier and Type | Method and Description |
---|---|
TcpServer |
bindAddress(Supplier<? extends SocketAddress> bindAddressSupplier)
Set a new local address to which this transport should bind on subscribe.
|
TcpServer |
channelGroup(ChannelGroup channelGroup)
Provide a
ChannelGroup to hold all active connected channels. |
static TcpServer |
create()
Prepare a
TcpServer . |
TcpServer |
doOnBind(Consumer<? super TcpServerConfig> doOnBind)
Set or add a callback called when
ServerTransport is about to start listening for incoming traffic. |
TcpServer |
doOnBound(Consumer<? super DisposableServer> doOnBound)
Set or add a callback called after
DisposableServer has been started. |
TcpServer |
doOnConnection(Consumer<? super Connection> doOnConnection)
Set or add a callback called on new remote
Connection . |
TcpServer |
doOnUnbound(Consumer<? super DisposableServer> doOnUnbound)
Set or add a callback called after
DisposableServer has been shutdown. |
TcpServer |
handle(BiFunction<? super NettyInbound,? super NettyOutbound,? extends Publisher<Void>> handler)
Attaches an I/O handler to react on a connected client.
|
TcpServer |
host(String host)
The host to which this server should bind.
|
TcpServer |
metrics(boolean enable)
Whether to enable metrics to be collected and registered in Micrometer's
globalRegistry . |
TcpServer |
metrics(boolean enable,
Supplier<? extends ChannelMetricsRecorder> recorder)
Specifies whether the metrics are enabled on the
Transport . |
TcpServer |
noSSL()
Removes any previously applied SSL configuration customization.
|
TcpServer |
port(int port)
The port to which this server should bind.
|
TcpServer |
runOn(EventLoopGroup eventLoopGroup)
Run IO loops on the given
EventLoopGroup . |
TcpServer |
runOn(LoopResources channelResources)
Run IO loops on a supplied
EventLoopGroup from the LoopResources container. |
TcpServer |
runOn(LoopResources loopResources,
boolean preferNative)
Run IO loops on a supplied
EventLoopGroup from the LoopResources container. |
TcpServer |
secure(Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder)
Apply an SSL configuration customization via the passed builder.
|
TcpServer |
secure(SslProvider sslProvider)
Applies 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 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, the bind operation absorbs the extra time needed to load resources. |
TcpServer |
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. |
TcpServer |
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. |
TcpServer |
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. |
bind, bindNow, bindNow, bindUntilJavaShutdown, childAttr, childObserve, childOption
attr, configuration, doOnChannelInit, duplicate, observe, option, wiretap, wiretap
public TcpServer bindAddress(Supplier<? extends SocketAddress> bindAddressSupplier)
Transport
bindAddress
in class Transport<TcpServer,TcpServerConfig>
bindAddressSupplier
- A supplier of the address to bind to.Transport
public TcpServer channelGroup(ChannelGroup channelGroup)
Transport
ChannelGroup
to hold all active connected channels.
Graceful Shutdown:
When a ChannelGroup
is set, calls to DisposableChannel.disposeNow()
and DisposableChannel.disposeNow(Duration)
not only stop accepting new requests
but also additionally wait for all active requests, in the ChannelGroup
, to
complete, within the given timeout.
channelGroup
in class Transport<TcpServer,TcpServerConfig>
channelGroup
- a ChannelGroup
Transport
referencepublic TcpServer doOnBind(Consumer<? super TcpServerConfig> doOnBind)
ServerTransport
ServerTransport
is about to start listening for incoming traffic.doOnBind
in class ServerTransport<TcpServer,TcpServerConfig>
doOnBind
- a consumer observing connected eventsServerTransport
referencepublic TcpServer doOnBound(Consumer<? super DisposableServer> doOnBound)
ServerTransport
DisposableServer
has been started.doOnBound
in class ServerTransport<TcpServer,TcpServerConfig>
doOnBound
- a consumer observing connected eventsServerTransport
referencepublic TcpServer doOnConnection(Consumer<? super Connection> doOnConnection)
ServerTransport
Connection
.doOnConnection
in class ServerTransport<TcpServer,TcpServerConfig>
doOnConnection
- a consumer observing remote connectionsServerTransport
referencepublic TcpServer doOnUnbound(Consumer<? super DisposableServer> doOnUnbound)
ServerTransport
DisposableServer
has been shutdown.doOnUnbound
in class ServerTransport<TcpServer,TcpServerConfig>
doOnUnbound
- a consumer observing unbound eventsServerTransport
referencepublic TcpServer handle(BiFunction<? super NettyInbound,? super NettyOutbound,? extends Publisher<Void>> handler)
public TcpServer host(String host)
ServerTransport
host
in class ServerTransport<TcpServer,TcpServerConfig>
host
- the host to bind to.ServerTransport
referencepublic TcpServer 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<TcpServer,TcpServerConfig>
enable
- true enables metrics collection; false disables itTransport
referencepublic TcpServer 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<TcpServer,TcpServerConfig>
enable
- if true enables the metrics on the Transport
.recorder
- a supplier for the ChannelMetricsRecorder
Transport
referencepublic TcpServer noSSL()
TcpServer
public TcpServer port(int port)
port
in class ServerTransport<TcpServer,TcpServerConfig>
port
- The port to bind to.TcpServer
public TcpServer runOn(EventLoopGroup eventLoopGroup)
Transport
EventLoopGroup
.runOn
in class Transport<TcpServer,TcpServerConfig>
eventLoopGroup
- an eventLoopGroup to shareTransport
referencepublic TcpServer 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<TcpServer,TcpServerConfig>
channelResources
- a LoopResources
accepting native runtime expectation and
returning an eventLoopGroupTransport
referencepublic TcpServer runOn(LoopResources loopResources, boolean preferNative)
Transport
EventLoopGroup
from the LoopResources
container.runOn
in class Transport<TcpServer,TcpServerConfig>
loopResources
- a new loop resourcespreferNative
- should prefer running on epoll, kqueue or similar instead of java NIOTransport
referencepublic TcpServer 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 timeoutSelfSignedCertificate
needs to be used, the sample below can be
used. Note that SelfSignedCertificate
should not be used in production.
SelfSignedCertificate cert = new SelfSignedCertificate();
TcpSslContextSpec tcpSslContextSpec =
TcpSslContextSpec.forServer(cert.certificate(), cert.privateKey());
secure(sslContextSpec -> sslContextSpec.sslContext(tcpSslContextSpec));
sslProviderBuilder
- builder callback for further customization of SslContext.TcpServer
public TcpServer secure(SslProvider sslProvider)
SslProvider
.
If SelfSignedCertificate
needs to be used, the sample below can be
used. Note that SelfSignedCertificate
should not be used in production.
SelfSignedCertificate cert = new SelfSignedCertificate();
TcpSslContextSpec tcpSslContextSpec =
TcpSslContextSpec.forServer(cert.certificate(), cert.privateKey());
secure(sslContextSpec -> sslContextSpec.sslContext(tcpSslContextSpec));
sslProvider
- The provider to set when configuring SSLTcpServer
public Mono<Void> warmup()
Mono
that triggers:
bind operation
absorbs the extra time needed to load resources.warmup
in class ServerTransport<TcpServer,TcpServerConfig>
Mono
representing the completion of the warmuppublic TcpServer 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<TcpServer,TcpServerConfig>
enable
- specifies whether the wire logger configuration will be added to the pipelineTransport
referencepublic TcpServer 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<TcpServer,TcpServerConfig>
category
- the logger categoryTransport
referencepublic TcpServer 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<TcpServer,TcpServerConfig>
category
- the logger categorylevel
- the logger levelTransport
reference