コード例 #1
0
        internal static IPullConsumer DefaultConsumer(string subject, string group, ConsumeMode mode, string appCode, string metaServer, TagType tagType, string[] tags)
        {
            if (string.IsNullOrEmpty(subject))
            {
                throw new ArgumentException("subject不能为空");
            }
            if (string.IsNullOrEmpty(group))
            {
                throw new ArgumentException("group不能为空");
            }

            lock (CacheGuard)
            {
                var key = BuildConsumerKey(subject, group);
                if (ConsumerCache.TryGetValue(key, out var consumer))
                {
                    if (consumer.ConsumeMode == mode)
                    {
                        return(consumer);
                    }
                    else
                    {
                        throw new ArgumentException($"已经以{consumer.ConsumeMode}模式创建了消费者,不允许同一消费族使用两种不同的模式消费同一主题");
                    }
                }
                else
                {
                    var newConsumer = new DefaultPullConsumer(subject, group, mode, false, appCode, metaServer, tagType, tags);
                    ConsumerCache.Add(key, newConsumer);
                    return(newConsumer);
                }
            }
        }
コード例 #2
0
        public Task <string> BasicConsume(ConsumeMode mode, Func <MessageDelivery, Task> consumer,
                                          string queue, string consumerTag, bool withoutAcks, bool exclusive,
                                          IDictionary <string, object> arguments, bool waitConfirmation)
        {
            EnsureOpen();

            if (consumer == null)
            {
                throw new ArgumentNullException("consumer");
            }
            if (!waitConfirmation && string.IsNullOrEmpty(consumerTag))
            {
                throw new ArgumentException("You must specify a consumer tag if waitConfirmation = false");
            }

            if (!string.IsNullOrEmpty(consumerTag))
            {
                RegisterConsumer(mode, consumer, consumerTag);
            }

            return(_io.__BasicConsume(mode, queue, consumerTag, withoutAcks, exclusive, arguments, waitConfirmation,
                                      consumerTag2 =>
            {
                RegisterConsumer(mode, consumer, consumerTag2);
            }));
        }
コード例 #3
0
 public QueueConsumerRecovery(ConsumeMode mode, IQueueConsumer consumer,
                              string queue, string consumerTag2, bool withoutAcks, bool exclusive,
                              IDictionary <string, object> arguments)
     : this(mode, queue, consumerTag2, withoutAcks, exclusive, arguments)
 {
     _consumer2 = consumer;
 }
コード例 #4
0
        public Task <string> BasicConsume(ConsumeMode mode, Func <MessageDelivery, Task> consumer,
                                          string queue, string consumerTag, bool withoutAcks, bool exclusive,
                                          IDictionary <string, object> arguments, bool waitConfirmation)
        {
            if (consumer == null)
            {
                throw new ArgumentNullException("consumer");
            }
            if (!waitConfirmation && string.IsNullOrEmpty(consumerTag))
            {
                throw new ArgumentException("You must specify a consumer tag if waitConfirmation = false");
            }

            if (!string.IsNullOrEmpty(consumerTag))
            {
                _consumerSubscriptions[consumerTag] = new BasicConsumerSubscriptionInfo
                {
                    Mode     = mode,
                    Callback = consumer
                };
            }

            return(_io.__BasicConsume(mode, queue, consumerTag, withoutAcks, exclusive, arguments, waitConfirmation,
                                      consumerTag2 =>
            {
                _consumerSubscriptions[consumerTag2] = new BasicConsumerSubscriptionInfo
                {
                    Mode = mode,
                    Callback = consumer
                };
            }));
        }
コード例 #5
0
        public static IPullConsumer BroadcastConsumer(string subject, ConsumeMode mode, string appCode, string metaServer)
        {
            if (string.IsNullOrEmpty(subject))
            {
                throw new ArgumentException("subject不能为空");
            }

            lock (CacheGuard)
            {
                var key = BuildConsumerKey(subject, "");
                if (ConsumerCache.TryGetValue(key, out var consumer))
                {
                    if (consumer.ConsumeMode == mode)
                    {
                        return(consumer);
                    }
                    else
                    {
                        throw new ArgumentException($"已经以{consumer.ConsumeMode}模式创建了消费者,不允许同一消费族使用两种不同的模式消费同一主题");
                    }
                }
                else
                {
                    var newConsumer = new DefaultPullConsumer(subject, "", mode, true, appCode, metaServer, TagType.OR, null);
                    ConsumerCache.Add(key, newConsumer);
                    return(newConsumer);
                }
            }
        }
コード例 #6
0
 public ExtraListenerConfig(ConsumeMode mode, int pullBatchSize, TimeSpan pullBatchTimeout, TagType tagType, string[] tags)
 {
     Mode             = mode;
     PullBatchSize    = pullBatchSize;
     PullBatchTimeout = pullBatchTimeout;
     TagType          = tagType;
     Tags             = tags;
 }
コード例 #7
0
 private void RegisterConsumer(ConsumeMode mode, IQueueConsumer consumer, string consumerTag)
 {
     RegisterConsumer(consumerTag, new BasicConsumerSubscriptionInfo
     {
         ConsumerTag = consumerTag,
         Mode        = mode,
         _consumer   = consumer
     });
 }
コード例 #8
0
 private void RegisterConsumer(ConsumeMode mode, Func <MessageDelivery, Task> consumer, string consumerTag)
 {
     RegisterConsumer(consumerTag, new BasicConsumerSubscriptionInfo
     {
         ConsumerTag = consumerTag,
         Mode        = mode,
         Callback    = consumer
     });
 }
コード例 #9
0
        public async Task <RpcAggregateHelper> CreateRpcAggregateHelper(ConsumeMode mode, int?timeoutInMs, int maxConcurrentCalls, bool captureContext)
        {
            ThrowIfRecoveryInProcess();

            var helper = await _channel.CreateRpcAggregateHelper(this, mode, timeoutInMs, maxConcurrentCalls, captureContext).ConfigureAwait(false);

            lock (_rpcHelpers) _rpcHelpers.Add(helper);

            return(helper);
        }
コード例 #10
0
        public Task <RpcAggregateHelper> CreateRpcAggregateHelper(ConsumeMode mode, int?timeoutInMs, int maxConcurrentCalls, bool captureContext)
        {
            EnsureOpen();

            if (_confirmationKeeper != null)
            {
                throw new Exception("This channel is set up for confirmations");
            }

            return(RpcAggregateHelper.Create(this, maxConcurrentCalls, mode, captureContext, timeoutInMs));
        }
コード例 #11
0
        public async Task <string> BasicConsume(ConsumeMode mode, Func <MessageDelivery, Task> consumer, string queue, string consumerTag, bool withoutAcks, bool exclusive,
                                                IDictionary <string, object> arguments, bool waitConfirmation)
        {
            ThrowIfRecoveryInProcess();

            var consumerTag2 = await _channel.BasicConsume(mode, consumer, queue, consumerTag, withoutAcks, exclusive, arguments, waitConfirmation).ConfigureAwait(false);

            lock (_consumersRegistered) _consumersRegistered.Add(new QueueConsumerRecovery(mode, consumer, queue, consumerTag2, withoutAcks, exclusive, arguments));

            return(consumerTag2);
        }
コード例 #12
0
        private RpcAggregateHelper(IChannel channel, int maxConcurrentCalls,
                                   ConsumeMode mode, int?timeoutInMs)
            : base(channel, maxConcurrentCalls, mode, timeoutInMs)
        {
            _pendingAggregationState = new AggState[maxConcurrentCalls];

            for (int i = 0; i < maxConcurrentCalls; i++)
            {
                _pendingAggregationState[i] = new AggState();
            }
        }
コード例 #13
0
 private QueueConsumerRecovery(ConsumeMode mode,
                               string queue, string consumerTag2, bool withoutAcks, bool exclusive,
                               IDictionary <string, object> arguments)
 {
     _mode        = mode;
     _queue       = queue;
     _consumerTag = consumerTag2;
     _withoutAcks = withoutAcks;
     _exclusive   = exclusive;
     _arguments   = arguments;
 }
コード例 #14
0
        protected BaseRpcHelper(ConsumeMode mode, IChannel channel, int maxConcurrentCalls)
        {
            if (maxConcurrentCalls <= 0)
            {
                throw new ArgumentOutOfRangeException("maxConcurrentCalls");
            }

            _mode               = mode;
            _channel            = channel;
            _maxConcurrentCalls = maxConcurrentCalls;
            _semaphoreSlim      = new SemaphoreSlim(maxConcurrentCalls, maxConcurrentCalls);
        }
コード例 #15
0
        public async Task <RpcHelper> CreateRpcHelper(ConsumeMode mode, int maxConcurrentCalls = 500)
        {
            if (_confirmationKeeper != null)
            {
                throw new Exception("This channel is set up for confirmations");
            }

            var helper = new RpcHelper(this, maxConcurrentCalls, mode);
            await helper.Setup();

            return(helper);
        }
コード例 #16
0
        internal Task <string> __BasicConsume(ConsumeMode mode, string queue, string consumerTag, bool withoutAcks,
                                              bool exclusive, IDictionary <string, object> arguments, bool waitConfirmation, Action <string> confirmConsumerTag)
        {
            var tcs = new TaskCompletionSource <string>(            // TaskCreationOptions.AttachedToParent |
                mode == ConsumeMode.ParallelWithBufferCopy || mode == ConsumeMode.SerializedWithBufferCopy
                                ? TaskCreationOptions.RunContinuationsAsynchronously : TaskCreationOptions.None);

            var writer = AmqpChannelLevelFrameWriter.BasicConsume(
                queue, consumerTag, withoutAcks, exclusive, arguments, waitConfirmation);

            _connectionIo.SendCommand(_channelNum, 60, 20,
                                      writer,
                                      reply: (channel, classMethodId, error) =>
            {
                if (LogAdapter.ProtocolLevelLogEnabled)
                {
                    LogAdapter.LogDebug("ChannelIO", "< BasicConsumeOk for queue " + queue);
                }

                if (waitConfirmation && classMethodId == AmqpClassMethodChannelLevelConstants.BasicConsumeOk)
                {
                    _connectionIo._frameReader.Read_BasicConsumeOk((consumerTag2) =>
                    {
                        if (string.IsNullOrEmpty(consumerTag))
                        {
                            if (LogAdapter.ProtocolLevelLogEnabled)
                            {
                                LogAdapter.LogDebug("ChannelIO", "< BasicConsumeOk consumerTag " + consumerTag);
                            }

                            confirmConsumerTag(consumerTag2);
                        }

                        tcs.SetResult(consumerTag2);
                    });
                }
                else if (!waitConfirmation)
                {
                    tcs.SetResult(consumerTag);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }

                return(Task.CompletedTask);
            }, expectsReply: waitConfirmation);

            return(tcs.Task);
        }
コード例 #17
0
        public async Task <RpcHelper> CreateRpcHelper(ConsumeMode mode, int?timeoutInMs, int maxConcurrentCalls, bool captureContext)
        {
            ThrowIfRecoveryInProcess();

            if (this.IsConfirmationEnabled)
            {
                throw new Exception("This channel is set up for confirmations");
            }

            var helper = await _channel.CreateRpcHelper(this, mode, timeoutInMs, maxConcurrentCalls, captureContext).ConfigureAwait(false);

            lock (_rpcHelpers) _rpcHelpers.Add(helper);

            return(helper);
        }
コード例 #18
0
ファイル: Puller.cs プロジェクト: kevinten10/Ware-Qmq
        public Puller(string subject, string consumerGroup, bool isBroadcast, TagType tagType, string[] tags, ConsumeMode consumeMode, PullController controller, PullStrategy pullStrategy, LoadBalance loadBalance)
        {
            _subject       = subject;
            _originSubject = RetrySubjectUtils.RealSubject(_subject);
            _consumerGroup = consumerGroup;
            _isBroadcast   = isBroadcast;
            _consumeMode   = consumeMode;
            _tagType       = (_tags == null || _tags.Length == 0) ? (byte)0 : (byte)tagType;
            _tags          = tags;

            _controller = controller;

            _pullStrategy = pullStrategy;
            _loadBalance  = loadBalance;
        }
コード例 #19
0
ファイル: Channel.cs プロジェクト: kjnilsson/RabbitMqNext
        public Task <string> BasicConsume(ConsumeMode mode, IQueueConsumer consumer,
                                          string queue, string consumerTag, bool withoutAcks, bool exclusive,
                                          IDictionary <string, object> arguments, bool waitConfirmation)
        {
            EnsureOpen();

            if (consumer == null)
            {
                throw new ArgumentNullException("consumer");
            }
            if (!waitConfirmation && string.IsNullOrEmpty(consumerTag))
            {
                throw new ArgumentException("You must specify a consumer tag if waitConfirmation = false");
            }

            // TODO: refactor

            if (!string.IsNullOrEmpty(consumerTag))
            {
                var added = _consumerSubscriptions.TryAdd(consumerTag, new BasicConsumerSubscriptionInfo
                {
                    ConsumerTag = consumerTag,
                    Mode        = mode,
                    _consumer   = consumer
                });

                if (!added)
                {
                    throw new Exception("Consumer already exists for tag " + consumerTag);
                }
            }

            return(_io.__BasicConsume(mode, queue, consumerTag, withoutAcks, exclusive, arguments, waitConfirmation,
                                      consumerTag2 =>
            {
                var added = _consumerSubscriptions.TryAdd(consumerTag2, new BasicConsumerSubscriptionInfo
                {
                    ConsumerTag = consumerTag,
                    Mode = mode,
                    _consumer = consumer
                });

                if (!added)
                {
                    throw new Exception("Consumer already exists for tag " + consumerTag);
                }
            }));
        }
コード例 #20
0
        protected BaseRpcHelper(Channel channel, int maxConcurrentCalls, ConsumeMode mode, int?timeoutInMs)
            : base(mode, channel, maxConcurrentCalls)
        {
            _timeoutInMs = timeoutInMs;

            // the impl keeps a timer pool so this is light and efficient
            if (timeoutInMs.HasValue)
            {
                _timeoutInTicks = timeoutInMs * TimeSpan.TicksPerMillisecond;
                _timeoutTimer   = new System.Threading.Timer(OnTimeoutCheck, null, timeoutInMs.Value, timeoutInMs.Value);
            }

            _pendingCalls   = new TaskSlim <T> [maxConcurrentCalls];
            _taskResultPool = new ObjectPool <TaskSlim <T> >(() =>
                                                             new TaskSlim <T>((inst) => _taskResultPool.PutObject(inst)), maxConcurrentCalls, preInitialize: true);
        }
コード例 #21
0
		public RpcHelper(Channel channel, int maxConcurrentCalls, ConsumeMode mode, int timeoutInMs = 6000)
		{
			if (maxConcurrentCalls <= 0) throw new ArgumentOutOfRangeException("maxConcurrentCalls");

			_channel = channel;
			_maxConcurrentCalls = maxConcurrentCalls;
			_mode = mode;
			_timeoutInMs = timeoutInMs;
			_timeoutInTicks = timeoutInMs * TimeSpan.TicksPerMillisecond;

			_semaphoreSlim = new SemaphoreSlim(maxConcurrentCalls, maxConcurrentCalls);

			// the impl keeps a timer pool so this is light and efficient
			// _timeoutTimer = new System.Threading.Timer(OnTimeoutCheck, null, timeoutInMs, timeoutInMs);

			_pendingCalls = new TaskSlim<MessageDelivery>[maxConcurrentCalls];
			_taskResultPool = new ObjectPool<TaskSlim<MessageDelivery>>(() =>
				new TaskSlim<MessageDelivery>((inst) => _taskResultPool.PutObject(inst)), maxConcurrentCalls, true);
		}
コード例 #22
0
        protected BaseRpcHelper(IChannel channel, int maxConcurrentCalls, ConsumeMode mode, int?timeoutInMs)
            : base(mode, channel, maxConcurrentCalls)
        {
            _timeoutInMs = timeoutInMs;

            // the impl keeps a timer pool so this is light and efficient
            if (timeoutInMs.HasValue)
            {
                _timeoutInTicks = timeoutInMs * TimeSpan.TicksPerMillisecond;
                _timeoutTimer   = new System.Threading.Timer(OnTimeoutCheck, null, timeoutInMs.Value, timeoutInMs.Value);
            }

            _pendingCalls = new PendingCallState[maxConcurrentCalls];
            for (int i = 0; i < maxConcurrentCalls; i++)
            {
                _pendingCalls[i] = new PendingCallState {
                    pos = i
                };
            }
        }
コード例 #23
0
        public RpcHelper(Channel channel, int maxConcurrentCalls, ConsumeMode mode, int timeoutInMs = 6000)
        {
            if (maxConcurrentCalls <= 0)
            {
                throw new ArgumentOutOfRangeException("maxConcurrentCalls");
            }

            _channel            = channel;
            _maxConcurrentCalls = maxConcurrentCalls;
            _mode           = mode;
            _timeoutInMs    = timeoutInMs;
            _timeoutInTicks = timeoutInMs * TimeSpan.TicksPerMillisecond;

            _semaphoreSlim = new SemaphoreSlim(maxConcurrentCalls, maxConcurrentCalls);

            // the impl keeps a timer pool so this is light and efficient
            // _timeoutTimer = new System.Threading.Timer(OnTimeoutCheck, null, timeoutInMs, timeoutInMs);

            _pendingCalls   = new TaskSlim <MessageDelivery> [maxConcurrentCalls];
            _taskResultPool = new ObjectPool <TaskSlim <MessageDelivery> >(() =>
                                                                           new TaskSlim <MessageDelivery>((inst) => _taskResultPool.PutObject(inst)), maxConcurrentCalls, true);
        }
コード例 #24
0
        internal DefaultPullConsumer(string subject, string group, ConsumeMode mode, bool isBroadcast, string appCode, string metaServer, TagType tagType, string[] tags)
        {
            CheckArgument(subject, group, isBroadcast);

            if (isBroadcast)
            {
                group = ClientId.CurrentUniqeConsumerGroup;
            }

            Subject       = subject;
            ConsumerGroup = group;
            ConsumeMode   = mode;
            IsBroadcast   = isBroadcast;

            _localBuffer = new ConcurrentQueue <Message>();

            _controller = new PullController(appCode, metaServer);
            var retrySubject = RetrySubjectUtils.BuildRetrySubject(subject, group);

            _pullers[0] = new Puller(retrySubject, group, isBroadcast, tagType, tags, mode, _controller, new WeightPullStrategy(), new RoundRobinLoadBalance());
            _pullers[1] = new Puller(subject, group, isBroadcast, tagType, tags, mode, _controller, new AlwaysPullStrategy(), new WeightLoadBalance());
        }
コード例 #25
0
 /// <summary>
 /// 消费者
 /// </summary>
 /// <param name="settings"></param>
 /// <param name="autoConfig">自动建Exchange、Queue和Bind</param>
 public Consumer(ConsumerSettings settings, bool autoConfig)
 {
     if (settings == null)
     {
         throw new ArgumentNullException(nameof(settings));
     }
     if (settings.AmqpConnection == null && settings.AmqpUri == null)
     {
         throw new ArgumentNullException("AmqpConnection or AmqpUri must be set.");
     }
     _clientName = string.IsNullOrEmpty(settings.ClientName) ? "undefined consumer client" : settings.ClientName;
     _amqpUri    = settings.AmqpUri;
     if (settings.AmqpConnection != null)
     {
         _amqpConnection = settings.AmqpConnection;
         _clientName     = settings.AmqpConnection.ClientProvidedName;
     }
     _mode             = settings.Mode;
     _prefetchCount    = settings.PrefetchCount <= 0 ? (ushort)1 : (ushort)settings.PrefetchCount;
     _groupName        = string.IsNullOrEmpty(settings.GroupName) ? "default" : settings.GroupName;
     _consumerCount    = settings.ConsumerCount <= 0 ? 1 : settings.ConsumerCount;
     _consumerSequence = settings.ConsumerSequence <= 0 || settings.ConsumerSequence > _consumerCount ? 1 : settings.ConsumerSequence;
     _autoConfig       = autoConfig;
 }
コード例 #26
0
        public static async Task <RpcHelper> Create(Channel channel, int maxConcurrentCalls, ConsumeMode mode,
                                                    bool captureContext = false, int?timeoutInMs = null)
        {
            var instance = new RpcHelper(channel, maxConcurrentCalls, mode, timeoutInMs)
            {
                CaptureContext = captureContext
            };
            await instance.Setup().ConfigureAwait(captureContext);

            return(instance);
        }
コード例 #27
0
 public Task <RpcAggregateHelper> CreateRpcAggregateHelper(ConsumeMode mode, int?timeoutInMs, int maxConcurrentCalls, bool captureContext)
 {
     return(CreateRpcAggregateHelper(this, mode, timeoutInMs, maxConcurrentCalls, captureContext));
 }
コード例 #28
0
        public static async Task <RpcAggregateHelper> Create(Channel channel, int maxConcurrentCalls, ConsumeMode mode, int?timeoutInMs)
        {
            var instance = new RpcAggregateHelper(channel, maxConcurrentCalls, mode, timeoutInMs);
            await instance.Setup().ConfigureAwait(false);

            return(instance);
        }
コード例 #29
0
 public static IPullConsumer DefaultConsumer(string subject, string group, ConsumeMode mode, string appCode, string metaServer)
 {
     return(DefaultConsumer(subject, group, mode, appCode, metaServer, TagType.OR, null));
 }
コード例 #30
0
 private RpcHelper(Channel channel, int maxConcurrentCalls, ConsumeMode mode, int?timeoutInMs)
     : base(channel, maxConcurrentCalls, mode, timeoutInMs)
 {
 }
コード例 #31
0
		public Task<string> __BasicConsume(ConsumeMode mode, string queue, string consumerTag, bool withoutAcks, 
			bool exclusive, IDictionary<string, object> arguments, bool waitConfirmation, Action<string> confirmConsumerTag)
		{
			var tcs = new TaskCompletionSource<string>( // TaskCreationOptions.AttachedToParent | 
				mode == ConsumeMode.ParallelWithBufferCopy ? TaskCreationOptions.RunContinuationsAsynchronously : TaskCreationOptions.None);

			var writer = AmqpChannelLevelFrameWriter.BasicConsume(
				queue, consumerTag, withoutAcks, exclusive, arguments, waitConfirmation);

			_connectionIo.SendCommand(_channelNum, 60, 20, 
				writer,
				reply: (channel, classMethodId, error) =>
				{
					Console.WriteLine("< BasicConsumeOk  " + queue);

					if (waitConfirmation && classMethodId == AmqpClassMethodChannelLevelConstants.BasicConsumeOk)
					{
						_connectionIo._frameReader.Read_BasicConsumeOk((consumerTag2) =>
						{
							if (string.IsNullOrEmpty(consumerTag))
							{
								confirmConsumerTag(consumerTag2);
							}

							tcs.SetResult(consumerTag2);
						});
					}
					else if (!waitConfirmation)
					{
						tcs.SetResult(consumerTag);
					}
					else
					{
						AmqpIOBase.SetException(tcs, error, classMethodId);
					}

					return Task.CompletedTask;

				}, expectsReply: waitConfirmation);

			return tcs.Task;
		}