Skip to content

Commit 317b40c

Browse files
committed
Document Netty configuration
References #269
1 parent f52da5a commit 317b40c

File tree

6 files changed

+102
-2
lines changed

6 files changed

+102
-2
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@
416416
<attributes>
417417
<project-artifact-id>${project.artifactId}</project-artifact-id>
418418
<project-version>${project.version}</project-version>
419-
<protonj-version>${proton-j.version}</protonj-version>
419+
<netty-version>${netty.version}</netty-version>
420420
<build-number>${buildNumber}</build-number>
421421
<imagesdir>./images</imagesdir>
422422
<idprefix />
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
:test-examples: ../../test/java/com/rabbitmq/stream/docs
2+
3+
=== Advanced Topics
4+
5+
==== Using Native `epoll`
6+
7+
The stream Java client uses the https://netty.io/[Netty] network framework and its Java NIO transport implementation by default.
8+
This should be a reasonable default for most applications.
9+
10+
Netty also allows using https://netty.io/wiki/native-transports.html[JNI transports].
11+
They are less portable than Java NIO, but they can be more performant for some workloads (even though the RabbitMQ team has not seen any significant improvement in their own tests).
12+
13+
The https://en.wikipedia.org/wiki/Epoll[Linux `epoll` transport] is a popular choice, so we'll see how to configure with the stream Java client.
14+
Other JNI transports can be configured in the same way.
15+
16+
The native transport dependency must be added to the dependency manager.
17+
We must pull the native binaries compiled for our OS and architecture, in our example Linux x86-64, so we are using the `linux-x86_64` classifier.
18+
Here is the declaration for Maven:
19+
20+
.Declaring the Linux x86-64 native `epoll` transport dependency with Maven
21+
[source,xml,subs="attributes,specialcharacters"]
22+
----
23+
<dependencies>
24+
25+
<dependency>
26+
<groupId>io.netty</groupId>
27+
<artifactId>netty-transport-native-epoll</artifactId>
28+
<version>{netty-version}</version>
29+
<classifier>linux-x86_64</classifier>
30+
</dependency>
31+
32+
</dependencies>
33+
----
34+
35+
And for Gradle:
36+
37+
.Declaring the Linux x86-64 native `epoll` transport dependency with Gradle
38+
[source,groovy,subs="attributes,specialcharacters"]
39+
----
40+
dependencies {
41+
compile "io.netty:netty-transport-native-epoll:{netty-version}:linux-x86_64"
42+
}
43+
----
44+
45+
The native `epoll` transport is set up when the environment is configured:
46+
47+
.Configuring the native `epoll` transport in the environment
48+
[source,java,indent=0]
49+
--------
50+
include::{test-examples}/EnvironmentUsage.java[tag=native-epoll]
51+
--------
52+
<1> Create the `epoll` event loop group (don't forget to close it!)
53+
<2> Use the Netty configuration helper
54+
<3> Set the event loop group
55+
<4> Set the channel class to use
56+
57+
Note the event loop group must be closed explicitly: the environment will not close it itself as it is provided externally.

src/docs/asciidoc/api.adoc

+22
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,28 @@ elements that need to be configured.
227227
|Helper to configure a `SslContext` that trusts all server certificates
228228
and does not use a client private key. **Only for development**.
229229
|Disabled by default.
230+
231+
|`netty`
232+
|Configuration helper for Netty.
233+
|
234+
235+
|`netty#eventLoopGroup`
236+
|Netty's event dispatcher.
237+
It is the developer's responsibility to close the `EventLoopGroup` they provide.
238+
|`NioEventLoopGroup` instance closed automatically with the `Environment` instance.
239+
240+
|`netty#ByteBufAllocator`
241+
|`ByteBuf` allocator.
242+
|ByteBufAllocator.DEFAULT
243+
244+
|`netty#channelCustomizer`
245+
|Extension point to customize Netty's `Channel` instances used for connections.
246+
|None
247+
248+
|`netty#bootstrapCustomizer`
249+
|Extension point to customize Netty's `Bootstrap` instances used to configure connections.
250+
|None
251+
230252
|===
231253

232254
===== When a Load Balancer is in Use

src/docs/asciidoc/index.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ include::api.adoc[]
2424

2525
include::super-streams.adoc[]
2626

27+
include::advanced-topics.adoc[]
28+
2729
include::building.adoc[]
2830

2931
include::performance-tool.adoc[]

src/main/java/com/rabbitmq/stream/EnvironmentBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ interface NettyConfiguration {
431431

432432
/**
433433
* An extension point to customize Netty's {@link io.netty.channel.Channel}s used for
434-
* connection.
434+
* connections.
435435
*
436436
* @param channelCustomizer
437437
* @return the Netty configuration helper

src/test/java/com/rabbitmq/stream/docs/EnvironmentUsage.java

+19
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818
import com.rabbitmq.stream.ByteCapacity;
1919
import com.rabbitmq.stream.Environment;
2020

21+
import com.rabbitmq.stream.EnvironmentBuilder;
22+
import io.netty.channel.Channel;
23+
import io.netty.channel.EventLoopGroup;
24+
import io.netty.channel.epoll.EpollEventLoopGroup;
25+
import io.netty.channel.epoll.EpollSocketChannel;
2126
import io.netty.handler.ssl.SslContext;
2227
import io.netty.handler.ssl.SslContextBuilder;
2328
import java.io.FileInputStream;
2429
import java.security.cert.CertificateFactory;
2530
import java.security.cert.X509Certificate;
2631
import java.time.Duration;
2732
import java.util.Arrays;
33+
import java.util.Set;
34+
import java.util.concurrent.ConcurrentHashMap;
2835

2936
public class EnvironmentUsage {
3037

@@ -134,4 +141,16 @@ void deleteStream() {
134141
// end::stream-deletion[]
135142
}
136143

144+
void nativeEpoll() {
145+
// tag::native-epoll[]
146+
EventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(); // <1>
147+
Environment environment = Environment.builder()
148+
.netty() // <2>
149+
.eventLoopGroup(epollEventLoopGroup) // <3>
150+
.bootstrapCustomizer(b -> b.channel(EpollSocketChannel.class)) // <4>
151+
.environmentBuilder()
152+
.build();
153+
// end::native-epoll[]
154+
}
155+
137156
}

0 commit comments

Comments
 (0)