|
此版本仍在开发中,尚未被视为稳定版本。如需使用最新稳定版本,请访问 Spring Data Redis 4.0.4! |
发布(发送消息)
要发布消息,您可以像其他操作一样,使用底层的 [Reactive]RedisConnection 或高层的 [Reactive]RedisOperations。
这两个实体都提供 publish 方法,该方法接受消息和目标通道作为参数。
虽然 RedisConnection 需要原始数据(字节数组),但 [Reactive]RedisOperations 允许将任意对象作为消息传递,如下例所示:
-
Imperative
-
Reactive
// send message through connection
RedisConnection con = …
byte[] msg = …
byte[] channel = …
con.pubSubCommands().publish(msg, channel);
// send message through RedisOperations
RedisOperations operations = …
Long numberOfClients = operations.convertAndSend("hello!", "world");
// send message through connection
ReactiveRedisConnection con = …
ByteBuffer[] msg = …
ByteBuffer[] channel = …
con.pubSubCommands().publish(msg, channel);
// send message through ReactiveRedisOperations
ReactiveRedisOperations operations = …
Mono<Long> numberOfClients = operations.convertAndSend("hello!", "world");