ServiceBusSenderClient クラス
- java.
lang. Object - com.
azure. messaging. servicebus. ServiceBusSenderClient
- com.
実装
public final class ServiceBusSenderClient
implements AutoCloseable
Azure Service Busのキューまたはトピックへの送信ServiceBusMessageを担当する同期送信者。
このドキュメントに示す例では、認証に DefaultAzureCredential という名前の資格情報オブジェクトを使用します。これは、ローカルの開発環境や運用環境など、ほとんどのシナリオに適しています。 さらに、運用環境での認証には マネージド ID を 使用することをお勧めします。 さまざまな認証方法と、それに対応する資格情報の種類の詳細については、 Azure Identity のドキュメントを参照してください。
サンプル: 送信者のインスタンスを作成する
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
// 'fullyQualifiedNamespace' will look similar to "{your-namespace}.servicebus.windows.net"
ServiceBusSenderClient sender = new ServiceBusClientBuilder()
.credential(fullyQualifiedNamespace, credential)
.sender()
.queueName(queueName)
.buildClient();
sender.sendMessage(new ServiceBusMessage("Foo bar"));
サンプル: Service Bus リソースにメッセージを送信する
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
// 'fullyQualifiedNamespace' will look similar to "{your-namespace}.servicebus.windows.net"
ServiceBusSenderClient sender = new ServiceBusClientBuilder()
.credential(fullyQualifiedNamespace, credential)
.sender()
.queueName(queueName)
.buildClient();
List<ServiceBusMessage> messages = Arrays.asList(
new ServiceBusMessage("test-1"),
new ServiceBusMessage("test-2"));
// Creating a batch without options set.
ServiceBusMessageBatch batch = sender.createMessageBatch();
for (ServiceBusMessage message : messages) {
if (batch.tryAddMessage(message)) {
continue;
}
// The batch is full. Send the current batch and create a new one.
sender.sendMessages(batch);
batch = sender.createMessageBatch();
// Add the message we couldn't before.
if (!batch.tryAddMessage(message)) {
throw new IllegalArgumentException("Message is too large for an empty batch.");
}
}
// Send the final batch if there are any messages in it.
if (batch.getCount() > 0) {
sender.sendMessages(batch);
}
// 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();
サンプル: サイズ制限を使用してメッセージを送信する ServiceBusMessageBatch
List<ServiceBusMessage> telemetryMessages = Arrays.asList(firstMessage, secondMessage, thirdMessage);
// 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);
ServiceBusMessageBatch currentBatch = sender.createMessageBatch(options);
// For each telemetry message, we try to add it to the current batch.
// When the batch is full, send it then create another batch to add more mesages to.
for (ServiceBusMessage message : telemetryMessages) {
if (!currentBatch.tryAddMessage(message)) {
sender.sendMessages(currentBatch);
currentBatch = sender.createMessageBatch(options);
// Add the message we couldn't before.
if (!currentBatch.tryAddMessage(message)) {
throw new IllegalArgumentException("Message is too large for an empty batch.");
}
}
}
// Send the final batch if there are any messages in it.
if (currentBatch.getCount() > 0) {
sender.sendMessages(currentBatch);
}
// 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();
サンプル: セッションが有効なキューへのメッセージの送信
次のスニペットは、 Service Bus セッション が有効なキューにメッセージを送信する方法を示しています。 プロパティを "greetings" に設定 setMessageId(String messageId) すると、ID が "greetings" の Service Bus セッションにメッセージが送信されます。
// 'fullyQualifiedNamespace' will look similar to "{your-namespace}.servicebus.windows.net"
ServiceBusSenderClient sender = new ServiceBusClientBuilder()
.credential(fullyQualifiedNamespace, new DefaultAzureCredentialBuilder().build())
.sender()
.queueName(sessionEnabledQueueName)
.buildClient();
// Setting sessionId publishes that message to a specific session, in this case, "greeting".
ServiceBusMessage message = new ServiceBusMessage("Hello world")
.setSessionId("greetings");
sender.sendMessage(message);
// Dispose of the sender.
sender.close();
メソッドの概要
メソッドの継承元: java.lang.Object
メソッドの詳細
cancelScheduledMessage
public void cancelScheduledMessage(long sequenceNumber)
まだエンキューされていない場合は、スケジュールされたメッセージのエンキューを取り消します。
Parameters:
cancelScheduledMessages
public void cancelScheduledMessages(Iterable
まだエンキューされていない場合は、スケジュールされたメッセージのエンキューを取り消します。
Parameters:
close
public void close()
を破棄します ServiceBusSenderClient。 クライアントに専用接続がある場合は、基になる接続も閉じられます。
commitTransaction
public void commitTransaction(ServiceBusTransactionContext transactionContext)
指定 ServiceBusTransactionContextしたトランザクションをコミットします。
Parameters:
createMessageBatch
public ServiceBusMessageBatch createMessageBatch()
トランスポートで ServiceBusMessageBatch 許可される数のメッセージに収まる を作成します。
Returns:
createMessageBatch
public ServiceBusMessageBatch createMessageBatch(CreateMessageBatchOptions options)
指定した ServiceBusMessageBatch オプションで構成された を作成します。
Parameters:
Returns:
createTransaction
public ServiceBusTransactionContext createTransaction()
Service Bus で新しいトランザクションを開始します。 は ServiceBusTransactionContext 、このトランザクションに存在する必要があるすべての操作に渡す必要があります。
Returns:
getEntityPath
public String getEntityPath()
Service Bus リソースの名前を取得します。
Returns:
getFullyQualifiedNamespace
public String getFullyQualifiedNamespace()
完全修飾名前空間を取得します。
Returns:
getIdentifier
public String getIdentifier()
のインスタンス ServiceBusSenderClientの識別子を取得します。
Returns:
rollbackTransaction
public void rollbackTransaction(ServiceBusTransactionContext transactionContext)
指定されたトランザクションとそれに関連付けられているすべての操作をロールバックします。
Parameters:
scheduleMessage
public Long scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime)
この送信者が接続しているAzure Service Bus エンティティにスケジュールされたメッセージを送信します。 スケジュールされたメッセージがエンキューされ、スケジュールされたエンキュー時間にのみ受信者が使用できるようになります。
Parameters:
Returns:
scheduleMessage
public Long scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime, ServiceBusTransactionContext transactionContext)
この送信者が接続しているAzure Service Bus エンティティにスケジュールされたメッセージを送信します。 スケジュールされたメッセージがエンキューされ、スケジュールされたエンキュー時間にのみ受信者が使用できるようになります。
Parameters:
Returns:
scheduleMessages
public Iterable
スケジュールされたメッセージのバッチを、この送信者が接続しているAzure Service Bus エンティティに送信します。 スケジュールされたメッセージがエンキューされ、スケジュールされたエンキュー時間にのみ受信者が使用できるようになります。
Parameters:
Returns:
scheduleMessages
public Iterable
スケジュールされたメッセージのバッチを、この送信者が接続しているAzure Service Bus エンティティに送信します。 スケジュールされたメッセージがエンキューされ、スケジュールされたエンキュー時間にのみ受信者が使用できるようになります。
Parameters:
Returns:
sendMessage
public void sendMessage(ServiceBusMessage message)
Service Bus キューまたはトピックにメッセージを送信します。
Parameters:
sendMessage
public void sendMessage(ServiceBusMessage message, ServiceBusTransactionContext transactionContext)
Service Bus キューまたはトピックにメッセージを送信します。
Parameters:
sendMessages
public void sendMessages(ServiceBusMessageBatch batch)
送信者が接続しているAzure Service Bus エンティティにメッセージ バッチを送信します。
Parameters:
sendMessages
public void sendMessages(ServiceBusMessageBatch batch, ServiceBusTransactionContext transactionContext)
送信者が接続しているAzure Service Bus エンティティにメッセージ バッチを送信します。
Parameters:
sendMessages
public void sendMessages(Iterable
バッチ処理されたアプローチを使用して、 の ServiceBusMessage セットを Service Bus キューまたはトピックに送信します。 メッセージのサイズが 1 つのバッチの最大サイズを超えると、例外がトリガーされ、送信は失敗します。 既定では、メッセージ サイズはリンクで許可される最大量です。
Parameters:
sendMessages
public void sendMessages(Iterable
バッチ処理されたアプローチを使用して、 の ServiceBusMessage セットを Service Bus キューまたはトピックに送信します。 メッセージのサイズが 1 つのバッチの最大サイズを超えると、例外がトリガーされ、送信は失敗します。 既定では、メッセージ サイズはリンクで許可される最大量です。
Parameters:
適用対象
Azure SDK for Java