Ejemplo n.º 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);
                }
            }
        }
Ejemplo n.º 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);
            }));
        }
 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;
 }
Ejemplo n.º 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
                };
            }));
        }
Ejemplo n.º 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);
                }
            }
        }
Ejemplo n.º 6
0
 public ExtraListenerConfig(ConsumeMode mode, int pullBatchSize, TimeSpan pullBatchTimeout, TagType tagType, string[] tags)
 {
     Mode             = mode;
     PullBatchSize    = pullBatchSize;
     PullBatchTimeout = pullBatchTimeout;
     TagType          = tagType;
     Tags             = tags;
 }
Ejemplo n.º 7
0
 private void RegisterConsumer(ConsumeMode mode, IQueueConsumer consumer, string consumerTag)
 {
     RegisterConsumer(consumerTag, new BasicConsumerSubscriptionInfo
     {
         ConsumerTag = consumerTag,
         Mode        = mode,
         _consumer   = consumer
     });
 }
Ejemplo n.º 8
0
 private void RegisterConsumer(ConsumeMode mode, Func <MessageDelivery, Task> consumer, string consumerTag)
 {
     RegisterConsumer(consumerTag, new BasicConsumerSubscriptionInfo
     {
         ConsumerTag = consumerTag,
         Mode        = mode,
         Callback    = consumer
     });
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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();
            }
        }
Ejemplo n.º 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;
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 18
0
        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;
        }
Ejemplo n.º 19
0
        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);
                }
            }));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
		}
Ejemplo n.º 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
                };
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 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;
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 27
0
 public Task <RpcAggregateHelper> CreateRpcAggregateHelper(ConsumeMode mode, int?timeoutInMs, int maxConcurrentCalls, bool captureContext)
 {
     return(CreateRpcAggregateHelper(this, mode, timeoutInMs, maxConcurrentCalls, captureContext));
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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));
 }
Ejemplo n.º 30
0
 private RpcHelper(Channel channel, int maxConcurrentCalls, ConsumeMode mode, int?timeoutInMs)
     : base(channel, maxConcurrentCalls, mode, timeoutInMs)
 {
 }
Ejemplo n.º 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;
		}