/// <summary>初始化延迟队列功能。生产者自动初始化,消费者最好能够按队列初始化一次</summary> /// <remarks> /// 该功能是附加功能,需要消费者主动调用,每个队列的多消费者开一个即可。 /// 核心工作是启动延迟队列的TransferAsync大循环,每个进程内按队列开一个最合适,多了没有用反而形成争夺。 /// </remarks> public RedisDelayQueue <T> InitDelay() { if (_delay == null) { lock (this) { if (_delay == null) { _delay = new RedisDelayQueue <T>(Redis, $"{Key}:Delay"); } } } if (_delayTask == null || _delayTask.IsCompleted) { lock (this) { if (_delayTask == null || _delayTask.IsCompleted) { _source = new CancellationTokenSource(); _delayTask = TaskEx.Run(() => _delay.TransferAsync(this, null, _source.Token)); } } } return(_delay); }
/// <summary>释放</summary> /// <param name="disposing"></param> protected virtual void Dispose(Boolean disposing) { if (_delay != null) { _delay = null; _delayTask = null; _source?.Cancel(); } }
/// <summary>初始化延迟队列功能。该功能是附加功能,需要生产者或消费者主动初始化</summary> public void InitDelay() { if (_delay == null) { lock (this) { if (_delay == null) { _delay = new RedisDelayQueue <T>(Redis, $"{Key}:Delay", false); _source = new CancellationTokenSource(); _delayTask = Task.Factory.StartNew(() => _delay.TransferAsync(this, null, _source.Token), TaskCreationOptions.LongRunning); } } } }