Skip to content

Commit 35eddae

Browse files
committed
Document the usage of Netty for AMQP 091 Java client
References rabbitmq/rabbitmq-java-client#1663
1 parent 7c63100 commit 35eddae

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

client-libraries/java-api-guide.md

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -965,42 +965,51 @@ ConnectionFactory cf = new ConnectionFactory();
965965
cf.setThreadFactory(ThreadManager.backgroundThreadFactory());
966966
```
967967

968-
### Support for Java non-blocking IO {#java-nio}
968+
### Use of Netty for Network I/O {#netty}
969969

970-
Version 4.0 of the Java client brings support for Java non-blocking
971-
IO (a.k.a Java NIO). NIO isn't supposed to be faster than blocking IO,
972-
it simply allows to control resources (in this case, threads) more easily.
970+
Version 5.27.0 of the Java client brings support for [Netty](https://netty.io/) for network I/O.
971+
Netty isn't supposed to be faster than blocking I/O, it simply allows to control resources (in this case, threads) more easily.
973972

974-
With the default blocking IO mode, each connection uses a thread to read
975-
from the network socket. With the NIO mode, you can control the number of
976-
threads that read and write from/to the network socket.
973+
With the default blocking I/O mode, each connection uses a thread to read from the network socket.
974+
With Netty, you can control the number of threads that read and write from/to the network.
977975

978-
Use the NIO mode if your Java process uses many connections (dozens or hundreds).
979-
You should use fewer threads than with the default blocking mode. With the
980-
appropriate number of threads set, you shouldn't
981-
experience any decrease in performance, especially if the connections are
982-
not so busy.
976+
Use Netty if your Java process uses many connections (dozens or hundreds).
977+
You should use fewer threads than with the default blocking mode.
978+
With the appropriate number of threads set, you shouldn't experience any decrease in performance, especially if the connections are not so busy.
983979

984-
NIO must be enabled explicitly:
980+
Netty is activated and configured with the `ConnectionFactory#netty()` helper.
981+
Netty's `EventLoopGroup` is the most important setting for an application picky about the number of threads.
982+
Here is an example of how to set it with 4 threads:
985983

986984
```java
987-
ConnectionFactory connectionFactory = new ConnectionFactory();
988-
connectionFactory.useNio();
985+
int nbThreads = 4;
986+
IoHandlerFactory ioHandlerFactory = NioIoHandler.newFactory();
987+
EventLoopGroup eventLoopGroup = new MultiThreadIoEventLoopGroup(
988+
nbThreads, ioHandlerFactory
989+
);
990+
connectionFactory.netty().eventLoopGroup(eventLoopGroup);
991+
// ...
992+
// dispose the event loop group after closing all connections
993+
eventLoopGroup.shutdownGracefully();
989994
```
990995

991-
The NIO mode can be configured through the `NioParams` class:
996+
Note the event loop group must be disposed of after the connection closes its connections.
997+
If no event loop group is set, each connection will use its own, 1-thread event loop group (and will take care of closing it).
998+
This is far from optimal, this is why setting an `EventLoopGroup` is highly recommended when using Netty.
999+
1000+
Netty uses its own `SslContext` API for [TLS](#tls) configuration (_not_ JDK's `SSLContext`), so the `ConnectionFactory#useSslProtocol()` methods have no effect when Netty is activated.
1001+
Use `ConnectionFactory.netty().sslContext(SslContext)` instead, along with Netty's `SslContextBuilder` class.
1002+
Here is an example:
9921003

9931004
```java
994-
connectionFactory.setNioParams(new NioParams().setNbIoThreads(4));
1005+
X509Certificate caCertificate = ...;
1006+
connectionFactory.netty()
1007+
.sslContext(SslContextBuilder
1008+
.forClient() // mandatory, do not forget to call
1009+
.trustManager(caCertificate) // pass in certificate directly
1010+
.build());
9951011
```
9961012

997-
The NIO mode uses reasonable defaults, but you may need to change them according
998-
to your own workload. Some of the settings are: the total number of IO
999-
threads used, the size of buffers, a service executor to use for the IO loops,
1000-
parameters for the in-memory write queue (write requests are enqueued before
1001-
being sent on the network). Please read the Javadoc for details and defaults.
1002-
1003-
10041013
## Automatic Recovery From Network Failures {#recovery}
10051014
### Connection Recovery {#connection-recovery}
10061015

@@ -1475,6 +1484,9 @@ the [TLS guide](/docs/ssl). If you only want to configure
14751484
the Java client (especially the peer verification and trust manager parts),
14761485
read [the appropriate section](/docs/ssl#java-client) of the TLS guide.
14771486

1487+
Note Netty requires to use its own `SslContext` API when it is used for network I/O.
1488+
See the [Netty](#netty) section for more details.
1489+
14781490
## OAuth 2 Support {#oauth2-support}
14791491

14801492
The client can authenticate against an OAuth 2 server like [UAA](https://github.com/cloudfoundry/uaa).

0 commit comments

Comments
 (0)