@@ -965,42 +965,51 @@ ConnectionFactory cf = new ConnectionFactory();
965
965
cf. setThreadFactory(ThreadManager . backgroundThreadFactory());
966
966
```
967
967
968
- ### Support for Java non-blocking IO {#java-nio }
968
+ ### Use of Netty for Network I/O {#netty }
969
969
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.
973
972
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.
977
975
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.
983
979
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:
985
983
986
984
``` 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();
989
994
```
990
995
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:
992
1003
993
1004
``` 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());
995
1011
```
996
1012
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
-
1004
1013
## Automatic Recovery From Network Failures {#recovery}
1005
1014
### Connection Recovery {#connection-recovery}
1006
1015
@@ -1475,6 +1484,9 @@ the [TLS guide](/docs/ssl). If you only want to configure
1475
1484
the Java client (especially the peer verification and trust manager parts),
1476
1485
read [ the appropriate section] ( /docs/ssl#java-client ) of the TLS guide.
1477
1486
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
+
1478
1490
## OAuth 2 Support {#oauth2-support}
1479
1491
1480
1492
The client can authenticate against an OAuth 2 server like [ UAA] ( https://github.com/cloudfoundry/uaa ) .
0 commit comments