公開 (メッセージの送信)
メッセージを公開するには、アプリケーションのロールに合った抽象化レベルを選択してください。
[Reactive]RedisConnectionは低レベルの契約であり、すでに直列化された(byte[])チャネルおよびメッセージデータ上で動作します。[Reactive]RedisOperations(通常はRedisTemplate経由)は、テンプレートに設定されたシリアライザを使用して変換を行い、メッセージを公開します。RedisMessageSendingTemplateは Spring メッセージングと統合し、ペイロード変換をMessageConverterに委譲します。変換は、ペイロードの種類、存在する場合はcontentTypeヘッダーに基づいて選択されます。
命令型 API は 3 つのバリアントすべてを提供します。リアクティブ API は、低レベル接続と ReactiveRedisOperations バリアントを提供します。
以下の表は、送信オプション間の違いをまとめたものです。
| バリアント | 責務 | 変換モデル |
|---|---|---|
| バイナリチャネルとメッセージデータを公開します。 | アプリケーションコードはバイナリ値( |
| Redis データアクセス抽象化を通じてアプリケーション値を公開します。 | メッセージ本文には、設定済みの値シリアライザを使用します。パブリケーションはテンプレート固有の値シリアライザに紐付けられます。 |
| 柔軟なメッセージ変換のために、Spring の | ペイロード型と、存在する場合は |
以下の例は、利用可能な送信バリアントを示しています。
命令的
リアクティブ
Spring メッセージング
// send message through connection
RedisConnection connection = …
byte[] channel = …
byte[] message = …
connection.pubSubCommands().publish(channel, message);
// send message through RedisOperations
RedisOperations<String, Object> operations = …
Long numberOfClients = operations.convertAndSend("chatroom", "hello!");
// send message through connection
ReactiveRedisConnection connection = …
ByteBuffer channel = …
ByteBuffer message = …
connection.pubSubCommands().publish(channel, message);
// send message through ReactiveRedisOperations
ReactiveRedisOperations<String, String> operations = …
Mono<Long> numberOfClients = operations.convertAndSend("chatroom", "hello!");
RedisMessageSendingTemplate template = …
template.convertAndSend("chatroom", new Greeting("hello", "world"));
template.convertAndSend("chatroom", new Greeting("hello", "world"), (1)
Map.of(MessageHeaders.CONTENT_TYPE, "application/json"));
template.convertAndSend("chatroom", new Greeting("hello", "world"), (2)
Map.of(MessageHeaders.CONTENT_TYPE, JdkSerializerMessageConverter.APPLICATION_JAVA_SERIALIZED_OBJECT_VALUE));
| 1 | コンバーター選択のヒントとして contentType を指定してください。Redis および Pub/Sub はヘッダーをサポートしていないため、直列化されたメッセージのみを送信します。 |
| 2 | メッセージ本文には Java シリアライゼーションを選択してください。このコンテンツ型を使用するには JdkSerializerMessageConverter が必要です。 |