Reactor Netty provides the easy-to-use and easy-to-configure
UdpServer
.
It hides most of the Netty functionality that is required to create a UDP
server
and adds Reactive Streams
backpressure.
To start a UDP server, a UdpServer
instance has to be created and configured.
By default, the host is configured to be localhost
and the port is 12012
.
The following example shows how to create and start a UDP server:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/udp/server/create/Application.java[role=include]
-
Creates a
UdpServer
instance that is ready for configuring. -
Starts the server in a blocking fashion and waits for it to finish initializing.
The returned Connection
offers a simple server API, including disposeNow()
,
which shuts the server down in a blocking fashion.
In order to serve on a specific host and port, you can apply the following configuration to the UDP
server:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/udp/server/address/Application.java[role=include]
-
Configures the
UDP
server host -
Configures the
UDP
server port
By default, the initialization of the UdpServer
resources happens on demand. This means that the bind
operation
absorbs the extra time needed to initialize and load:
-
the event loop group
-
the native transport libraries (when native transport is used)
When you need to preload these resources, you can configure the UdpServer
as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/udp/server/warmup/Application.java[role=include]
-
Initialize and load the event loop group and the native transport libraries
To send data to the remote peer, you must attach an I/O handler.
The I/O handler has access to UdpOutbound
,
to be able to write data.
The following example shows how to send hello
:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/udp/server/send/Application.java[role=include]
-
Sends a
hello
string to the remote peer
To receive data from a remote peer, you must attach an I/O handler.
The I/O handler has access to UdpInbound
,
to be able to read data.
The following example shows how to consume data:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/udp/server/read/Application.java[role=include]
-
Receives data from the remote peer
The following lifecycle callbacks are provided to let you extend the UdpServer
:
Callback | Description |
---|---|
|
Invoked when the server channel is about to bind. |
|
Invoked when the server channel is bound. |
|
Invoked when initializing the channel. |
|
Invoked when the server channel is unbound. |
The following example uses the doOnBound
and doOnChannelInit
callbacks:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/udp/server/lifecycle/Application.java[role=include]
-
Netty
pipeline is extended withLineBasedFrameDecoder
when the server channel is bound. -
Netty
pipeline is extended withLoggingHandler
when initializing the channel.
This section describes three kinds of configuration that you can use at the UDP level:
By default, the UDP
server is configured with the following options:
link:./../../reactor-netty-core/src/main/java/reactor/netty/udp/UdpServerBind.java[role=include]
If you need additional options or need to change the current options, you can apply the following configuration:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/udp/server/channeloptions/Application.java[role=include]
For more information about Netty channel options, see the following links:
By default, the UDP server uses “Event Loop Group,” where the number of the worker threads equals the number of
processors available to the runtime on initialization (but with a minimum value of 4). When you need a different configuration,
you can use one of the LoopResource#create
methods.
The default configuration for the “Event Loop Group” is the following:
link:./../../reactor-netty-core/src/main/java/reactor/netty/ReactorNetty.java[role=include]
If you need changes to these settings, you can apply the following configuration:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/udp/server/eventloop/Application.java[role=include]
The UDP server supports built-in integration with Micrometer
.
It exposes all metrics with a prefix of reactor.netty.udp.server
.
The following table provides information for the UDP server metrics:
metric name | type | description |
---|---|---|
reactor.netty.udp.server.data.received |
DistributionSummary |
Amount of the data received, in bytes |
reactor.netty.udp.server.data.sent |
DistributionSummary |
Amount of the data sent, in bytes |
reactor.netty.udp.server.errors |
Counter |
Number of errors that occurred |
These additional metrics are also available:
The following example enables that integration:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/udp/server/metrics/Application.java[role=include]
-
Enables the built-in integration with Micrometer
When UDP server metrics are needed for an integration with a system other than Micrometer
or you want
to provide your own integration with Micrometer
, you can provide your own metrics recorder, as follows:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/udp/server/metrics/custom/Application.java[role=include]
-
Enables UDP server metrics and provides
ChannelMetricsRecorder
implementation.
The UdpServer
supports Unix Domain Datagram Sockets (UDS) when native transport is in use.
The following example shows how to use UDS support:
link:./../../reactor-netty-examples/src/main/java/reactor/netty/examples/documentation/udp/server/uds/Application.java[role=include]
-
Specifies
DomainSocketAddress
that will be used