Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

Commit d56836a

Browse files
committed
Document channel pooling
References #48
1 parent bacc365 commit d56836a

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

src/docs/asciidoc/api-guide.adoc

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,36 @@ include::{test-examples}/ApiGuideSender.java[tag=resource-management-options]
468468

469469
In the example above, each operation will use the same `Channel` as it is cached.
470470
This way these operations won't interfer with any other thread using the default
471-
resource management `Mono<Channel>` in the `Sender` instance.
471+
resource management `Mono<Channel>` in the `Sender` instance.
472+
473+
==== Channel pooling in `Sender`
474+
475+
By default, `Sender#send*` methods open a new `Channel` for every call. This is
476+
OK for long-running calls, e.g. when the flux of outbound messages is
477+
infinite. For workloads whereby `Sender#send*` is called often for finite,
478+
short flux of messages, opening a new `Channel` every time may not be optimal.
479+
480+
It is possible to use a pool of channels as part of the `SendOptions` when
481+
sending outbound messages with `Sender`, as illustrated in the following snippet:
482+
483+
[source,java,indent=0]
484+
-------
485+
include::{test-examples}/ApiGuideSender.java[tag=channel-pool]
486+
-------
487+
<1> Create `ChannelPool` with factory
488+
<2> Set the maximum size to 5 channels
489+
<3> Use a channel from the pool to send messages
490+
<4> Close the pool when no longer needed
491+
492+
Note it is developer's responsibility to close the pool when it is no longer
493+
necessary, typically at application shutdown.
494+
495+
https://github.com/reactor/reactor-rabbitmq/tree/master/src/jmh/java/reactor/rabbitmq[Micro-benchmarks]
496+
revealed channel pooling performs much better for sending short sequence
497+
of messages (1 to 10) repeatedly, without publisher confirms. With longer sequence
498+
of messages (100 or more), channel pooling can perform worse than
499+
without pooling at all. According to the same micro-benchmarks, channel pooling
500+
does not make sending with publisher confirms perform better, it appears to perform
501+
even worse. Don't take these conclusions for granted, you should always make your
502+
own benchmarks depending on your workloads.
503+

src/test/java/reactor/rabbitmq/docs/ApiGuideSender.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,7 @@
2727
import reactor.core.publisher.Flux;
2828
import reactor.core.publisher.Mono;
2929
import reactor.core.scheduler.Schedulers;
30-
import reactor.rabbitmq.BindingSpecification;
31-
import reactor.rabbitmq.ExceptionHandlers;
32-
import reactor.rabbitmq.ExchangeSpecification;
33-
import reactor.rabbitmq.OutboundMessage;
34-
import reactor.rabbitmq.QueueSpecification;
35-
import reactor.rabbitmq.RabbitFlux;
36-
import reactor.rabbitmq.RabbitFluxException;
37-
import reactor.rabbitmq.ResourceManagementOptions;
38-
import reactor.rabbitmq.RpcClient;
39-
import reactor.rabbitmq.SendOptions;
40-
import reactor.rabbitmq.Sender;
41-
import reactor.rabbitmq.SenderOptions;
30+
import reactor.rabbitmq.*;
4231

4332
import java.time.Duration;
4433
import java.util.UUID;
@@ -206,4 +195,20 @@ void resourceManagementOptions() {
206195
// end::resource-management-options[]
207196
}
208197

198+
void channelPool() {
199+
Mono<Connection> connectionMono = null;
200+
Flux<OutboundMessage> outboundFlux = null;
201+
Sender sender = createSender();
202+
203+
// tag::channel-pool[]
204+
ChannelPool channelPool = ChannelPoolFactory.createChannelPool( // <1>
205+
connectionMono,
206+
new ChannelPoolOptions().maxCacheSize(5) // <2>
207+
);
208+
sender.send(outboundFlux, new SendOptions().channelPool(channelPool)); // <3>
209+
// ...
210+
channelPool.close(); // <4>
211+
// end::channel-pool[]
212+
}
213+
209214
}

0 commit comments

Comments
 (0)