Skip to content

Commit 980034c

Browse files
mp911dechristophstrobl
authored andcommitted
Update MessageListener reference documentation.
Original Pull Request: #2052
1 parent 9480851 commit 980034c

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/main/asciidoc/new-features.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
This section briefly covers items that are new and noteworthy in the latest releases.
55

6+
[[new-in-2.6.0]]
7+
== New in Spring Data Redis 2.6
8+
9+
* Support for `SubscriptionListener` when using `MessageListener` for subscription confirmation callbacks. `ReactiveRedisMessageListenerContainer` and `ReactiveRedisOperations` provide `receiveLater(…)` and `listenToLater(…)` methods to await until Redis acknowledges the subscription.
10+
611
[[new-in-2.5.0]]
712
== New in Spring Data Redis 2.5
813

src/main/asciidoc/reference/reactive-messaging.adoc

+15
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ ReactiveRedisMessageListenerContainer container = new ReactiveRedisMessageListen
5757
Flux<ChannelMessage<String, String>> stream = container.receive(ChannelTopic.of("my-channel"));
5858
----
5959

60+
To await and ensure proper subscription, you can use the `receiveLater` method that returns a `Mono<Flux<ChannelMessage>>`.
61+
The resulting `Mono` completes with an inner publisher as a result of completing the subscription to the given topics. By intercepting `onNext` signals, you can synchronize server-side subscriptions.
62+
63+
[source,java]
64+
----
65+
ReactiveRedisConnectionFactory factory = …
66+
ReactiveRedisMessageListenerContainer container = new ReactiveRedisMessageListenerContainer(factory);
67+
68+
Mono<Flux<ChannelMessage<String, String>>> stream = container.receiveLater(ChannelTopic.of("my-channel"));
69+
70+
stream.doOnNext(inner -> // notification hook when Redis subscriptions are synchronized with the server)
71+
.flatMapMany(Function.identity())
72+
.…;
73+
----
74+
6075
[[redis:reactive:pubsub:subscribe:template]]
6176
=== Subscribing via template API
6277

src/main/asciidoc/reference/redis-messaging.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Due to its blocking nature, low-level subscription is not attractive, as it requ
4747

4848
`RedisMessageListenerContainer` acts as a message listener container. It is used to receive messages from a Redis channel and drive the `MessageListener` instances that are injected into it. The listener container is responsible for all threading of message reception and dispatches into the listener for processing. A message listener container is the intermediary between an MDP and a messaging provider and takes care of registering to receive messages, resource acquisition and release, exception conversion, and the like. This lets you as an application developer write the (possibly complex) business logic associated with receiving a message (and reacting to it) and delegates boilerplate Redis infrastructure concerns to the framework.
4949

50+
A `MessageListener` can additionally implement `SubscriptionListener` to receive notifications upon subscription/unsubscribe confirmation. Listening to subscription notifications can be useful when synchronizing invocations.
51+
5052
Furthermore, to minimize the application footprint, `RedisMessageListenerContainer` lets one connection and one thread be shared by multiple listeners even though they do not share a subscription. Thus, no matter how many listeners or channels an application tracks, the runtime cost remains the same throughout its lifetime. Moreover, the container allows runtime configuration changes so that you can add or remove listeners while an application is running without the need for a restart. Additionally, the container uses a lazy subscription approach, using a `RedisConnection` only when needed. If all the listeners are unsubscribed, cleanup is automatically performed, and the thread is released.
5153

5254
To help with the asynchronous nature of messages, the container requires a `java.util.concurrent.Executor` (or Spring's `TaskExecutor`) for dispatching the messages. Depending on the load, the number of listeners, or the runtime environment, you should change or tweak the executor to better serve your needs. In particular, in managed environments (such as app servers), it is highly recommended to pick a proper `TaskExecutor` to take advantage of its runtime.

0 commit comments

Comments
 (0)