public abstract class TcpServer extends Object
bind(ServerBootstrap)
is ultimately called.
Internally, materialization happens in two phases:
configure()
is called to retrieve a ready to use ServerBootstrap
bind(ServerBootstrap)
is 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 |
addressSupplier(java.util.function.Supplier<? extends SocketAddress> bindingAddressSupplier)
Deprecated.
as of 0.9.7. Use
bindAddress(Supplier) |
<T> TcpServer |
attr(AttributeKey<T> key,
T value)
Injects default attribute to the future child
Channel connections. |
Mono<? extends DisposableServer> |
bind()
|
abstract Mono<? extends DisposableServer> |
bind(ServerBootstrap b)
|
TcpServer |
bindAddress(java.util.function.Supplier<? extends SocketAddress> bindAddressSupplier)
The address to which this server should bind on subscribe.
|
DisposableServer |
bindNow()
Starts the server in a blocking fashion, and waits for it to finish initializing
or the startup timeout expires (the startup timeout is
45 seconds). |
DisposableServer |
bindNow(java.time.Duration timeout)
Start the server in a blocking fashion, and wait for it to finish initializing
or the provided startup timeout expires.
|
void |
bindUntilJavaShutdown(java.time.Duration timeout,
java.util.function.Consumer<DisposableServer> onStart)
Start the server in a fully blocking fashion, not only waiting for it to initialize
but also blocking during the full lifecycle of the server.
|
TcpServer |
bootstrap(java.util.function.Function<? super ServerBootstrap,? extends ServerBootstrap> bootstrapMapper)
Deprecated.
as of 0.9.10. Use the methods exposed on
TcpServer level. The method
will be removed in version 1.0.0. |
TcpServer |
channelGroup(ChannelGroup channelGroup)
Provide a
ChannelGroup to hold all active connected channels. |
abstract ServerBootstrap |
configure()
Materializes a
ServerBootstrap from the parent TcpServer chain to use with
bind(ServerBootstrap) or separately |
static TcpServer |
create()
Prepare a
TcpServer |
TcpServer |
doOnBind(java.util.function.Consumer<? super ServerBootstrap> doOnBind)
Setups a callback called when
ServerChannel is about to
bind. |
TcpServer |
doOnBound(java.util.function.Consumer<? super DisposableServer> doOnBound)
Setups a callback called when
ServerChannel is
bound. |
TcpServer |
doOnConnection(java.util.function.Consumer<? super Connection> doOnConnection)
Setups a callback called when a remote client is connected
|
TcpServer |
doOnLifecycle(java.util.function.Consumer<? super ServerBootstrap> onBind,
java.util.function.Consumer<? super DisposableServer> onBound,
java.util.function.Consumer<? super DisposableServer> onUnbound)
Deprecated.
as of 0.9.7. Use
doOnBind(Consumer) , doOnBound(Consumer)
or doOnUnbound(Consumer) |
TcpServer |
doOnUnbound(java.util.function.Consumer<? super DisposableServer> doOnUnbind)
Setups a callback called when
ServerChannel is
unbound. |
TcpServer |
handle(java.util.function.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.
|
boolean |
isSecure()
Returns true if that
TcpServer secured via SSL transport |
TcpServer |
metrics(boolean metricsEnabled)
Whether to enable metrics to be collected and registered in Micrometer's
globalRegistry
under the name Metrics.TCP_SERVER_PREFIX . |
TcpServer |
metrics(boolean metricsEnabled,
ChannelMetricsRecorder recorder)
Deprecated.
as of 0.9.7. Use
metrics(boolean, Supplier) |
TcpServer |
metrics(boolean metricsEnabled,
java.util.function.Supplier<? extends ChannelMetricsRecorder> recorder)
Specifies whether the metrics are enabled on the
TcpServer . |
TcpServer |
noSSL()
Removes any previously applied SSL configuration customization
|
TcpServer |
observe(ConnectionObserver observer)
Setups all lifecycle callbacks called on or after
Channel
has been connected and after it has been disconnected. |
<T> TcpServer |
option(ChannelOption<T> key,
T value)
|
TcpServer |
port(int port)
The port to which this server should bind.
|
TcpServer |
runOn(EventLoopGroup eventLoopGroup)
Runs I/O loops on the given
EventLoopGroup . |
TcpServer |
runOn(LoopResources channelResources)
Runs I/O loops on a supplied
EventLoopGroup from the LoopResources
container. |
TcpServer |
runOn(LoopResources channelResources,
boolean preferNative)
Runs I/O loops on a supplied
EventLoopGroup from the LoopResources
container. |
TcpServer |
secure(java.util.function.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 . |
<T> TcpServer |
selectorAttr(AttributeKey<T> key,
T value)
Injects default attribute to the future
ServerChannel
selector connection. |
<T> TcpServer |
selectorOption(ChannelOption<T> key,
T value)
|
SslProvider |
sslProvider()
Returns the current
SslProvider if that TcpServer secured via SSL
transport or null |
TcpServer |
wiretap(boolean enable)
Apply or remove a wire logger configuration using
TcpServer category
and DEBUG logger level |
TcpServer |
wiretap(String category)
Applies a wire logger configuration using the specified category
and
DEBUG logger level |
TcpServer |
wiretap(String category,
LogLevel level)
Applies a wire logger configuration using the specified category
and logger level
|
@Deprecated public final TcpServer addressSupplier(java.util.function.Supplier<? extends SocketAddress> bindingAddressSupplier)
bindAddress(Supplier)
bindingAddressSupplier
- A supplier of the address to bind to.TcpServer
public final TcpServer bindAddress(java.util.function.Supplier<? extends SocketAddress> bindAddressSupplier)
bindAddressSupplier
- A supplier of the address to bind to.TcpServer
public final <T> TcpServer attr(AttributeKey<T> key, @Nullable T value)
Channel
connections. It
will be available via AttributeMap.attr(AttributeKey)
.
If the value
is null
, the attribute of the specified key
is removed.T
- the attribute typekey
- the attribute keyvalue
- the attribute valueTcpServer
ServerBootstrap.childAttr(AttributeKey, Object)
@Deprecated public final TcpServer bootstrap(java.util.function.Function<? super ServerBootstrap,? extends ServerBootstrap> bootstrapMapper)
TcpServer
level. The method
will be removed in version 1.0.0.ServerBootstrap
configuration given mapper taking currently configured one
and returning a new one to be ultimately used for socket binding.
Configuration will apply during configure()
phase.
bootstrapMapper
- A bootstrap mapping function to update configuration and return an
enriched bootstrap.TcpServer
public final Mono<? extends DisposableServer> bind()
TcpServer
and returns a Mono
of DisposableServer
. If
Mono
is cancelled, the underlying binding will be aborted. Once the DisposableServer
has been emitted and is not necessary anymore, disposing the main server
loop must be done by the user via DisposableChannel.dispose()
.
If updateConfiguration phase fails, a Mono.error(Throwable)
will be returned;Mono
of DisposableServer
public abstract Mono<? extends DisposableServer> bind(ServerBootstrap b)
b
- the ServerBootstrap
to bindMono
of DisposableServer
public final DisposableServer bindNow()
45
seconds). The
returned DisposableServer
offers simple server API, including to DisposableChannel.disposeNow()
shut it down in a blocking fashion.DisposableServer
public final DisposableServer bindNow(java.time.Duration timeout)
DisposableServer
offers simple server API, including to DisposableChannel.disposeNow()
shut it down in a blocking fashion.timeout
- max startup timeoutDisposableServer
public final void bindUntilJavaShutdown(java.time.Duration timeout, @Nullable java.util.function.Consumer<DisposableServer> onStart)
sigkill
.
Note: JVM shutdown hook
is added by
this method in order to properly disconnect the server upon receiving a
sigkill
signal.
timeout
- a timeout for server shutdownonStart
- an optional callback on server startpublic abstract ServerBootstrap configure()
ServerBootstrap
from the parent TcpServer
chain to use with
bind(ServerBootstrap)
or separatelyServerBootstrap
public final TcpServer doOnBind(java.util.function.Consumer<? super ServerBootstrap> doOnBind)
ServerChannel
is about to
bind.doOnBind
- a consumer observing server start eventTcpServer
public final TcpServer doOnBound(java.util.function.Consumer<? super DisposableServer> doOnBound)
ServerChannel
is
bound.doOnBound
- a consumer observing server started eventTcpServer
public final TcpServer doOnConnection(java.util.function.Consumer<? super Connection> doOnConnection)
doOnConnection
- a consumer observing connected clientsTcpServer
public final TcpServer doOnUnbound(java.util.function.Consumer<? super DisposableServer> doOnUnbind)
ServerChannel
is
unbound.doOnUnbind
- a consumer observing server stop eventTcpServer
@Deprecated public final TcpServer doOnLifecycle(java.util.function.Consumer<? super ServerBootstrap> onBind, java.util.function.Consumer<? super DisposableServer> onBound, java.util.function.Consumer<? super DisposableServer> onUnbound)
doOnBind(Consumer)
, doOnBound(Consumer)
or doOnUnbound(Consumer)
Channel
has been bound and after it has been unbound.onBind
- a consumer observing server start eventonBound
- a consumer observing server started eventonUnbound
- a consumer observing server stop eventTcpServer
public final TcpServer handle(java.util.function.BiFunction<? super NettyInbound,? super NettyOutbound,? extends Publisher<Void>> handler)
public final TcpServer host(String host)
host
- The host to bind to.TcpServer
public final boolean isSecure()
TcpServer
secured via SSL transportTcpServer
secured via SSL transportpublic final TcpServer noSSL()
TcpServer
public final TcpServer observe(ConnectionObserver observer)
Channel
has been connected and after it has been disconnected.observer
- a consumer observing state changesTcpServer
public final <T> TcpServer option(ChannelOption<T> key, @Nullable T value)
ChannelOption
value for low level connection settings like SO_TIMEOUT
or SO_KEEPALIVE
. This will apply to each new channel from remote peer.
Use a value of null
to remove a previous set ChannelOption
.T
- the option typekey
- the option keyvalue
- the option valueTcpServer
ServerBootstrap.childOption(ChannelOption, Object)
public final TcpServer port(int port)
bind()
operation:port
- The port to bind to.TcpServer
public final TcpServer runOn(EventLoopGroup eventLoopGroup)
EventLoopGroup
.eventLoopGroup
- an eventLoopGroup to shareTcpServer
public final TcpServer runOn(LoopResources channelResources)
EventLoopGroup
from the LoopResources
container. Will prefer native (epoll/kqueue) implementation if available unless the
environment property reactor.netty.native
is set to false
.channelResources
- a LoopResources
accepting native runtime
expectation and returning an eventLoopGroupTcpServer
public final TcpServer runOn(LoopResources channelResources, boolean preferNative)
EventLoopGroup
from the LoopResources
container.channelResources
- a LoopResources
accepting native runtime
expectation and returning an eventLoopGroup.preferNative
- Should the connector prefer native (epoll/kqueue) if available.TcpServer
public final TcpServer secure(java.util.function.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.
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();
SslContextBuilder sslContextBuilder =
SslContextBuilder.forServer(cert.certificate(), cert.privateKey());
secure(sslContextSpec -> sslContextSpec.sslContext(sslContextBuilder));
sslProviderBuilder
- builder callback for further customization of SslContext.TcpServer
public final 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();
SslContextBuilder sslContextBuilder =
SslContextBuilder.forServer(cert.certificate(), cert.privateKey());
secure(sslContextSpec -> sslContextSpec.sslContext(sslContextBuilder));
sslProvider
- The provider to set when configuring SSLTcpServer
public final <T> TcpServer selectorAttr(AttributeKey<T> key, T value)
ServerChannel
selector connection.T
- the attribute typekey
- the attribute keyvalue
- the attribute valueTcpServer
AbstractBootstrap.attr(AttributeKey, Object)
public final <T> TcpServer selectorOption(ChannelOption<T> key, T value)
ChannelOption
value for low level connection settings like SO_TIMEOUT
or SO_KEEPALIVE
. This will apply to parent selector channel.T
- the option typekey
- the option keyvalue
- the option valueTcpServer
AbstractBootstrap.option(ChannelOption, Object)
@Nullable public SslProvider sslProvider()
SslProvider
if that TcpServer
secured via SSL
transport or nullSslProvider
if that TcpServer
secured via SSL
transport or nullpublic final TcpServer channelGroup(ChannelGroup channelGroup)
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
- a ChannelGroup
TcpServer
public final TcpServer wiretap(boolean enable)
TcpServer
category
and DEBUG
logger levelenable
- Specifies whether the wire logger configuration will be added to
the pipelineTcpServer
public final TcpServer metrics(boolean metricsEnabled)
globalRegistry
under the name Metrics.TCP_SERVER_PREFIX
. Applications can
separately register their own
filters
associated with this name.
For example, to put an upper bound on the number of tags produced:
MeterFilter filter = ... ; Metrics.globalRegistry.config().meterFilter(MeterFilter.maximumAllowableTags(TCP_SERVER_PREFIX, 100, filter));
By default this is not enabled.
metricsEnabled
- true enables metrics collection; false disables itTcpServer
@Deprecated public final TcpServer metrics(boolean metricsEnabled, ChannelMetricsRecorder recorder)
metrics(boolean, Supplier)
TcpServer
.
All generated metrics are provided to the specified recorder.metricsEnabled
- if true enables the metrics on the server.recorder
- the ChannelMetricsRecorder
TcpServer
public final TcpServer metrics(boolean metricsEnabled, java.util.function.Supplier<? extends ChannelMetricsRecorder> recorder)
TcpServer
.
All generated metrics are provided to the specified recorder
which is only instantiated if metrics are being enabled.metricsEnabled
- if true enables the metrics on the server.recorder
- a supplier for the ChannelMetricsRecorder
TcpServer
public final TcpServer wiretap(String category)
DEBUG
logger levelcategory
- the logger categoryTcpServer