ServiceBusSenderAsyncClient クラス

  • java.lang.Object
    • com.azure.messaging.servicebus.ServiceBusSenderAsyncClient

実装

public final class ServiceBusSenderAsyncClient
implements AutoCloseable

Service Bus リソースにメッセージを送信する 非同期 クライアント。

このドキュメントに示す例では、認証に DefaultAzureCredential という名前の資格情報オブジェクトを使用します。これは、ローカルの開発環境や運用環境を含むほとんどのシナリオに適しています。 さらに、運用環境での認証に マネージド ID を 使用することをお勧めします。 認証のさまざまな方法とそれに対応する資格情報の種類の詳細については、 Azure ID のドキュメントを参照してください

サンプル: 送信者のインスタンスを作成する

TokenCredential credential = new DefaultAzureCredentialBuilder().build();

 // 'fullyQualifiedNamespace' will look similar to "{your-namespace}.servicebus.windows.net"
 ServiceBusSenderAsyncClient asyncSender = new ServiceBusClientBuilder()
     .credential(fullyQualifiedNamespace, credential)
     .sender()
     .queueName(queueName)
     .buildAsyncClient();

 // When users are done with the sender, they should dispose of it.
 // Clients should be long-lived objects as they require resources
 // and time to establish a connection to the service.
 asyncSender.close();

サンプル: Service Bus リソースにメッセージを送信する

// `subscribe` is a non-blocking call. The program will move onto the next line of code when it starts the
 // operation.  Users should use the callbacks on `subscribe` to understand the status of the send operation.
 asyncSender.createMessageBatch().flatMap(batch -> {
     batch.tryAddMessage(new ServiceBusMessage("test-1"));
     batch.tryAddMessage(new ServiceBusMessage("test-2"));
     return asyncSender.sendMessages(batch);
 }).subscribe(unused -> {
 }, error -> {
     System.err.println("Error occurred while sending batch:" + error);
 }, () -> {
     System.out.println("Send complete.");
 });

サンプル: Service Bus リソースに制限 ServiceBusMessageBatch されたサイズを使用してメッセージを送信する

Flux<ServiceBusMessage> telemetryMessages = Flux.just(firstMessage, secondMessage);

 // Setting `setMaximumSizeInBytes` when creating a batch, limits the size of that batch.
 // In this case, all the batches created with these options are limited to 256 bytes.
 CreateMessageBatchOptions options = new CreateMessageBatchOptions()
     .setMaximumSizeInBytes(256);
 AtomicReference<ServiceBusMessageBatch> currentBatch = new AtomicReference<>();

 // Sends the current batch if it is not null and not empty.  If the current batch is null, sets it.
 // Returns the batch to work with.
 Mono<ServiceBusMessageBatch> sendBatchAndGetCurrentBatchOperation = Mono.defer(() -> {
     ServiceBusMessageBatch batch = currentBatch.get();

     if (batch == null) {
         return asyncSender.createMessageBatch(options);
     }

     if (batch.getCount() > 0) {
         return asyncSender.sendMessages(batch).then(
             asyncSender.createMessageBatch(options)
                 .handle((ServiceBusMessageBatch newBatch, SynchronousSink<ServiceBusMessageBatch> sink) -> {
                     // Expect that the batch we just sent is the current one. If it is not, there's a race
                     // condition accessing currentBatch reference.
                     if (!currentBatch.compareAndSet(batch, newBatch)) {
                         sink.error(new IllegalStateException(
                             "Expected that the object in currentBatch was batch. But it is not."));
                     } else {
                         sink.next(newBatch);
                     }
                 }));
     } else {
         return Mono.just(batch);
     }
 });

 // The sample Flux contains two messages, but it could be an infinite stream of telemetry messages.
 Flux<Void> sendMessagesOperation = telemetryMessages.flatMap(message -> {
     return sendBatchAndGetCurrentBatchOperation.flatMap(batch -> {
         if (batch.tryAddMessage(message)) {
             return Mono.empty();
         } else {
             return sendBatchAndGetCurrentBatchOperation
                 .handle((ServiceBusMessageBatch newBatch, SynchronousSink<Void> sink) -> {
                     if (!newBatch.tryAddMessage(message)) {
                         sink.error(new IllegalArgumentException(
                             "Message is too large to fit in an empty batch."));
                     } else {
                         sink.complete();
                     }
                 });
         }
     });
 });

 // `subscribe` is a non-blocking call. The program will move onto the next line of code when it starts the
 // operation.  Users should use the callbacks on `subscribe` to understand the status of the send operation.
 Disposable disposable = sendMessagesOperation.then(sendBatchAndGetCurrentBatchOperation)
     .subscribe(batch -> {
         System.out.println("Last batch should be empty: " + batch.getCount());
     }, error -> {
         System.err.println("Error sending telemetry messages: " + error);
     }, () -> {
         System.out.println("Completed.");

         // Continue using the sender and finally, dispose of the sender.
         // Clients should be long-lived objects as they require resources
         // and time to establish a connection to the service.
         asyncSender.close();
     });

サンプル: セッションが有効なキューへのメッセージの送信

次のスニペットは、 Service Bus セッション が有効なキューにメッセージを送信する方法を示しています。 プロパティを "greetings" に設定 setMessageId(String messageId) すると、ID が "greetings" の Service Bus セッションにメッセージが送信されます。

// 'fullyQualifiedNamespace' will look similar to "{your-namespace}.servicebus.windows.net"
 ServiceBusSenderAsyncClient sender = new ServiceBusClientBuilder()
     .credential(fullyQualifiedNamespace, new DefaultAzureCredentialBuilder().build())
     .sender()
     .queueName(sessionEnabledQueueName)
     .buildAsyncClient();

 // Setting sessionId publishes that message to a specific session, in this case, "greeting".
 ServiceBusMessage message = new ServiceBusMessage("Hello world")
     .setSessionId("greetings");

 // `subscribe` is a non-blocking call. The program will move onto the next line of code when it starts the
 // operation.  Users should use the callbacks on `subscribe` to understand the status of the send operation.
 sender.sendMessage(message).subscribe(unused -> {
 }, error -> {
     System.err.println("Error occurred publishing batch: " + error);
 }, () -> {
     System.out.println("Send complete.");
 });

 // Continue using the sender and finally, dispose of the sender.
 // Clients should be long-lived objects as they require resources
 // and time to establish a connection to the service.
 sender.close();

メソッドの概要

修飾子と型 メソッドと説明
Mono<Void> cancelScheduledMessage(long sequenceNumber)

スケジュールされたメッセージのエンキューを取り消します (まだエンキューされていない場合)。

Mono<Void> cancelScheduledMessages(Iterable<Long> sequenceNumbers)

既にスケジュールされているメッセージのエンキューを取り消します (まだエンキューされていない場合)。

void close()

を破棄します ServiceBusSenderAsyncClient

Mono<Void> commitTransaction(ServiceBusTransactionContext transactionContext)

指定 ServiceBusTransactionContextされたトランザクションをコミットします。

Mono<ServiceBusMessageBatch> createMessageBatch()

トランスポートで ServiceBusMessageBatch 許容される数のメッセージに収まる を作成します。

Mono<ServiceBusMessageBatch> createMessageBatch(CreateMessageBatchOptions options)

指定したオプションで ServiceBusMessageBatch 構成された を作成します。

Mono<ServiceBusTransactionContext> createTransaction()

Service Bus で新しいトランザクションを開始します。

String getEntityPath()

Service Bus リソースの名前を取得します。

String getFullyQualifiedNamespace()

完全修飾名前空間を取得します。

String getIdentifier()

のインスタンス ServiceBusSenderAsyncClientの識別子を取得します。

Mono<Void> rollbackTransaction(ServiceBusTransactionContext transactionContext)

指定 ServiceBusTransactionContextされたトランザクションをロールバックします。

Mono<Long> scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime)

この送信者が接続しているAzure Service Bus エンティティにスケジュールされたメッセージを送信します。

Mono<Long> scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime, ServiceBusTransactionContext transactionContext)

この送信者が接続しているAzure Service Bus エンティティにスケジュールされたメッセージを送信します。

Flux<Long> scheduleMessages(Iterable<ServiceBusMessage> messages, OffsetDateTime scheduledEnqueueTime)

スケジュールされたメッセージのバッチを、この送信者が接続しているAzure Service Bus エンティティに送信します。

Flux<Long> scheduleMessages(Iterable<ServiceBusMessage> messages, OffsetDateTime scheduledEnqueueTime, ServiceBusTransactionContext transactionContext)

この送信者が接続しているAzure Service Busエンティティにスケジュールされたメッセージを送信します。

Mono<Void> sendMessage(ServiceBusMessage message)

Service Bus キューまたはトピックにメッセージを送信します。

Mono<Void> sendMessage(ServiceBusMessage message, ServiceBusTransactionContext transactionContext)

Service Bus キューまたはトピックにメッセージを送信します。

Mono<Void> sendMessages(ServiceBusMessageBatch batch)

送信者が接続しているAzure Service Bus エンティティにメッセージ バッチを送信します。

Mono<Void> sendMessages(ServiceBusMessageBatch batch, ServiceBusTransactionContext transactionContext)

送信者が接続しているAzure Service Bus エンティティにメッセージ バッチを送信します。

Mono<Void> sendMessages(Iterable<ServiceBusMessage> messages)

バッチ処理されたアプローチを使用して、一連のメッセージを Service Bus キューまたはトピックに送信します。

Mono<Void> sendMessages(Iterable<ServiceBusMessage> messages, ServiceBusTransactionContext transactionContext)

バッチ処理されたアプローチを使用して、一連のメッセージを Service Bus キューまたはトピックに送信します。

メソッドの継承元: java.lang.Object

メソッドの詳細

cancelScheduledMessage

public Mono cancelScheduledMessage(long sequenceNumber)

スケジュールされたメッセージのエンキューを取り消します (まだエンキューされていない場合)。

Parameters:

sequenceNumber - キャンセルするスケジュールされたメッセージの 。

Returns:

Mono Service Bus リソースでこの操作を完了する 。

cancelScheduledMessages

public Mono cancelScheduledMessages(Iterable sequenceNumbers)

既にスケジュールされているメッセージのエンキューを取り消します (まだエンキューされていない場合)。

Parameters:

sequenceNumbers - キャンセルするスケジュールされたメッセージの。

Returns:

Mono Service Bus リソースでこの操作を完了する 。

close

public void close()

を破棄します ServiceBusSenderAsyncClient。 クライアントに専用接続がある場合は、基になる接続も閉じられます。

commitTransaction

public Mono commitTransaction(ServiceBusTransactionContext transactionContext)

指定 ServiceBusTransactionContextされたトランザクションをコミットします。 これにより、Service Bus が呼び出されます。

Parameters:

transactionContext - コミットされます。

Returns:

Mono Service Bus リソースでこの操作を完了する 。

createMessageBatch

public Mono createMessageBatch()

トランスポートで ServiceBusMessageBatch 許容される数のメッセージに収まる を作成します。

Returns:

ServiceBusMessageBatchトランスポートで許容される数のメッセージに収まる 。

createMessageBatch

public Mono createMessageBatch(CreateMessageBatchOptions options)

指定したオプションで ServiceBusMessageBatch 構成された を作成します。

Parameters:

options - を構成するために使用される一連の ServiceBusMessageBatchオプション。

Returns:

指定されたオプションで構成された新しい ServiceBusMessageBatch

createTransaction

public Mono createTransaction()

Service Bus で新しいトランザクションを開始します。 は ServiceBusTransactionContext 、このトランザクションに含める必要があるすべての操作と ServiceBusReceivedMessage 共に渡す必要があります。

Returns:

getEntityPath

public String getEntityPath()

Service Bus リソースの名前を取得します。

Returns:

Service Bus リソースの名前。

getFullyQualifiedNamespace

public String getFullyQualifiedNamespace()

完全修飾名前空間を取得します。

Returns:

完全修飾名前空間。

getIdentifier

public String getIdentifier()

のインスタンス ServiceBusSenderAsyncClientの識別子を取得します。

Returns:

のインスタンス ServiceBusSenderAsyncClientを識別できる識別子。

rollbackTransaction

public Mono rollbackTransaction(ServiceBusTransactionContext transactionContext)

指定 ServiceBusTransactionContextされたトランザクションをロールバックします。 これにより、Service Bus が呼び出されます。

Parameters:

transactionContext - ロールバックするトランザクション。

Returns:

Mono Service Bus リソースでこの操作を完了する 。

scheduleMessage

public Mono scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime)

この送信者が接続しているAzure Service Bus エンティティにスケジュールされたメッセージを送信します。 スケジュールされたメッセージがエンキューされ、スケジュールされたエンキュー時刻にのみ受信者が使用できるようになります。

Parameters:

message - Service Bus キューに送信されるメッセージ。
scheduledEnqueueTime - メッセージが Service Bus キューまたはトピックに表示される OffsetDateTime。

Returns:

メッセージのスケジュール設定を取り消すために使用できる、スケジュールされたメッセージのシーケンス番号。

scheduleMessage

public Mono scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime, ServiceBusTransactionContext transactionContext)

この送信者が接続しているAzure Service Bus エンティティにスケジュールされたメッセージを送信します。 スケジュールされたメッセージがエンキューされ、スケジュールされたエンキュー時刻にのみ受信者が使用できるようになります。

Parameters:

message - Service Bus キューに送信されるメッセージ。
scheduledEnqueueTime - メッセージが Service Bus キューまたはトピックに表示される OffsetDateTime。
transactionContext - Service Bus に送信する前にメッセージに設定する必要があります。

Returns:

メッセージのスケジュール設定を取り消すために使用できる、スケジュールされたメッセージのシーケンス番号。

scheduleMessages

public Flux scheduleMessages(Iterable messages, OffsetDateTime scheduledEnqueueTime)

スケジュールされたメッセージのバッチを、この送信者が接続しているAzure Service Bus エンティティに送信します。 スケジュールされたメッセージがエンキューされ、スケジュールされたエンキュー時刻にのみ受信者が使用できるようになります。

Parameters:

messages - Service Bus キューまたはトピックに送信されるメッセージ。
scheduledEnqueueTime - メッセージが Service Bus キューまたはトピックに表示される OffsetDateTime。

Returns:

メッセージを取り消すために使用できるスケジュールされたメッセージのシーケンス番号。

scheduleMessages

public Flux scheduleMessages(Iterable messages, OffsetDateTime scheduledEnqueueTime, ServiceBusTransactionContext transactionContext)

この送信者が接続しているAzure Service Busエンティティにスケジュールされたメッセージを送信します。 スケジュールされたメッセージがエンキューされ、スケジュールされたエンキュー時刻にのみ受信者が使用できるようになります。

Parameters:

messages - Service Bus キューに送信されるメッセージ。
scheduledEnqueueTime - メッセージが Service Bus キューまたはトピックに表示される OffsetDateTime。
transactionContext - 操作に関連付けるトランザクション。

Returns:

メッセージを取り消すために使用できるスケジュールされたメッセージのシーケンス番号。

sendMessage

public Mono sendMessage(ServiceBusMessage message)

Service Bus キューまたはトピックにメッセージを送信します。

Parameters:

message - Service Bus キューまたはトピックに送信されるメッセージ。

Returns:

Mono 、Service Bus リソースでこの操作を完了します。

sendMessage

public Mono sendMessage(ServiceBusMessage message, ServiceBusTransactionContext transactionContext)

Service Bus キューまたはトピックにメッセージを送信します。

Parameters:

message - Service Bus キューまたはトピックに送信されるメッセージ。
transactionContext - Service Bus に送信する前にバッチ メッセージに設定する必要があります。

Returns:

Mono 、Service Bus リソースでこの操作を完了します。

sendMessages

public Mono sendMessages(ServiceBusMessageBatch batch)

送信者が接続しているAzure Service Bus エンティティにメッセージ バッチを送信します。

Parameters:

batch - クライアントがメッセージのバッチに対して最大許容サイズを送信できるようにするメッセージの 。

Returns:

Mono 、Service Bus リソースでこの操作を完了します。

sendMessages

public Mono sendMessages(ServiceBusMessageBatch batch, ServiceBusTransactionContext transactionContext)

送信者が接続しているAzure Service Bus エンティティにメッセージ バッチを送信します。

Parameters:

batch - クライアントがメッセージのバッチに対して最大許容サイズを送信できるようにするメッセージの 。
transactionContext - Service Bus に送信する前にバッチ メッセージに設定する必要があります。

Returns:

Mono 、Service Bus リソースでこの操作を完了します。

sendMessages

public Mono sendMessages(Iterable messages)

バッチ処理されたアプローチを使用して、一連のメッセージを Service Bus キューまたはトピックに送信します。 メッセージのサイズが 1 つのバッチの最大サイズを超えると、例外がトリガーされ、送信は失敗します。 既定では、メッセージ サイズはリンクで許可される最大量です。

Parameters:

messages - Service Bus キューまたはトピックに送信されるメッセージ。

Returns:

Monoすべてのメッセージが Service Bus リソースに送信されたときに完了する 。

sendMessages

public Mono sendMessages(Iterable messages, ServiceBusTransactionContext transactionContext)

バッチ処理されたアプローチを使用して、一連のメッセージを Service Bus キューまたはトピックに送信します。 メッセージのサイズが 1 つのバッチの最大サイズを超えると、例外がトリガーされ、送信は失敗します。 既定では、メッセージ サイズはリンクで許可される最大量です。

Parameters:

messages - Service Bus キューまたはトピックに送信されるメッセージ。
transactionContext - Service Bus に送信する前にバッチ メッセージに設定する必要があります。

Returns:

Monoすべてのメッセージが Service Bus リソースに送信されたときに完了する 。

適用対象