你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

ServiceBusTransactionContext 类

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

public final class ServiceBusTransactionContext

表示服务中的事务。 此对象仅包含事务 ID。事务管理操作(如创建事务、回滚和提交操作)需要使用发送方或接收方客户端来完成。

事务在 2 分钟后超时。 事务计时器在事务中的第一个操作启动时启动。

创建和使用事务

// This mono creates a transaction and caches the output value, so we can associate operations with the
 // transaction. It does not cache the value if it is an error or completes with no items, effectively retrying
 // the operation.
 Mono<ServiceBusTransactionContext> transactionContext = asyncReceiver.createTransaction()
     .cache(value -> Duration.ofMillis(Long.MAX_VALUE),
         error -> Duration.ZERO,
         () -> Duration.ZERO);

 // Dispose of the disposable to cancel the operation.
 Disposable disposable = transactionContext.flatMap(transaction -> {
     // Process messages and associate operations with the transaction.
     Mono<Void> operations = Mono.when(
         asyncReceiver.receiveDeferredMessage(sequenceNumber).flatMap(message ->
             asyncReceiver.complete(message, new CompleteOptions().setTransactionContext(transaction))),
         asyncReceiver.abandon(receivedMessage, new AbandonOptions().setTransactionContext(transaction)));

     // Finally, either commit or rollback the transaction once all the operations are associated with it.
     return operations.then(asyncReceiver.commitTransaction(transaction));
 }).subscribe(unused -> {
 }, error -> {
     System.err.println("Error occurred processing transaction: " + error);
 }, () -> {
     System.out.println("Completed transaction");
 });

方法摘要

修饰符和类型 方法和描述
ByteBuffer getTransactionId()

获取事务 ID。

方法继承自 java.lang.Object

方法详细信息

getTransactionId

public ByteBuffer getTransactionId()

获取事务 ID。

Returns:

事务 ID

适用于