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

IReliableConcurrentQueue<T>.EnqueueAsync 方法

定义

将值的排队暂存到队列中。

public System.Threading.Tasks.Task EnqueueAsync (Microsoft.ServiceFabric.Data.ITransaction tx, T value, System.Threading.CancellationToken cancellationToken = default, TimeSpan? timeout = default);
abstract member EnqueueAsync : Microsoft.ServiceFabric.Data.ITransaction * 'T * System.Threading.CancellationToken * Nullable<TimeSpan> -> System.Threading.Tasks.Task
Public Function EnqueueAsync (tx As ITransaction, value As T, Optional cancellationToken As CancellationToken = Nothing, Optional timeout As Nullable(Of TimeSpan) = Nothing) As Task

参数

tx
ITransaction

要与此操作关联的事务。

value
T

要添加到队列末尾的值。 对于引用类型,该值可以为 null。

cancellationToken
CancellationToken

要监视取消请求的标记。 默认为 None

timeout
Nullable<TimeSpan>

在引发 TimeoutException 之前等待操作完成的时间量。 默认值为 NULL。 如果传递 null,将使用默认超时。

返回

表示异步排队操作的任务。

例外

副本 (replica) 不再位于 中。

副本 (replica) 当前不可读。

副本 (replica) 出现暂时性故障。 对新事务重试操作

副本 (replica) 看到除上面定义的类型以外的不可重试的故障。 清理并重新引发异常

在给定的超时内无法完成该操作。 应中止事务,并创建一个新事务以重试。

tx 为 null。

操作已通过 cancellationToken取消。

事务在内部由系统出错。 对新事务重试操作

当方法调用对对象的当前状态无效时引发。 例如,使用的事务已终止:用户已提交或中止。 如果引发此异常,则很可能使用事务的服务代码中存在 bug。

示例

此示例演示如何使用 EnqueueAsync(ITransaction, T, CancellationToken, Nullable<TimeSpan>) 通过重试将值排队。

protected override async Task RunAsync(CancellationToken cancellationToken)
{
    var concurrentQueue = await this.StateManager.GetOrAddAsync<IReliableConcurrentQueue<long>>(new Uri("fabric:/concurrentQueue"));

    while (true)
    {
        cancellationToken.ThrowIfCancellationRequested();

        try
        {
            using (var tx = this.StateManager.CreateTransaction())
            {
                await concurrentQueue.EnqueueAsync(tx, 12L, cancellationToken);
                await tx.CommitAsync();

                return;
            }
        }
        catch (TransactionFaultedException e)
        {
            // This indicates that the transaction was internally faulted by the system. One possible cause for this is that the transaction was long running
            // and blocked a checkpoint. Increasing the "ReliableStateManagerReplicatorSettings.CheckpointThresholdInMB" will help reduce the chances of running into this exception
            Console.WriteLine("Transaction was internally faulted, retrying the transaction: " + e);
        }
        catch (FabricNotPrimaryException e)
        {
            // Gracefully exit RunAsync as the new primary should have RunAsync invoked on it and continue work.
            // If instead enqueue was being executed as part of a client request, the client would be signaled to re-resolve.
            Console.WriteLine("Replica is not primary, exiting RunAsync: " + e);
            return;
        }
        catch (FabricNotReadableException e)
        {
            // Retry until the queue is readable or a different exception is thrown.
            Console.WriteLine("Queue is not readable, retrying the transaction: " + e);
        }
        catch (FabricObjectClosedException e)
        {
            // Gracefully exit RunAsync as this is happening due to replica close.
            // If instead enqueue was being executed as part of a client request, the client would be signaled to re-resolve.
            Console.WriteLine("Replica is closing, exiting RunAsync: " + e);
            return;
        }
        catch (TimeoutException e)
        {
            Console.WriteLine("Encountered TimeoutException during EnqueueAsync, retrying the transaction: " + e);
        }
        catch (FabricTransientException e)
        {
            // Retry until the queue is writable or a different exception is thrown.
            Console.WriteLine("Queue is currently not writable, retrying the transaction: " + e);
        }

        // Delay and retry.
        await Task.Delay(TimeSpan.FromMilliseconds(100), cancellationToken);
    }
}

注解

操作 TryDequeueAsync(ITransaction, CancellationToken, Nullable<TimeSpan>) 无法返回尚未为其提交的任何值 EnqueueAsync(ITransaction, T, CancellationToken, Nullable<TimeSpan>) 。 这包括值排队的事务;因此, IReliableConcurrentQueue<T> 不支持读写。

适用于