Пример #1
0
        public QueueConsumer(string queueName, int maximumThreads)
        {
            MaximumThreads = maximumThreads;
            HostId         = ConfigSource.GetAppSetting("DQueue.HostId");
            QueueName      = queueName ?? QueueNameGenerator.GetQueueName <TMessage>();
            Timeout        = ConfigSource.FirstAppSetting("DQueue.ConsumerTimeout", "ConsumerTimeout").AsNullableTimeSpan();

            if (string.IsNullOrWhiteSpace(HostId))
            {
                throw new ArgumentNullException("HostId");
            }

            if (string.IsNullOrWhiteSpace(QueueName))
            {
                throw new ArgumentNullException("queueName");
            }

            if (MaximumThreads < 1)
            {
                throw new ArgumentOutOfRangeException("maximumThreads");
            }

            _provider = Constants.DefaultProvider;
            _cts      = new CancellationTokenSource();

            _messageHandlers  = new List <Action <DispatchContext <TMessage> > >();
            _timeoutHandlers  = new List <Action <DispatchContext <TMessage> > >();
            _completeHandlers = new List <Action <DispatchContext <TMessage> > >();
        }
Пример #2
0
        public void Enqueue(string queueName, object message)
        {
            if (string.IsNullOrWhiteSpace(queueName) || message == null)
            {
                return;
            }

            var json  = message.Serialize();
            var queue = GetQueue(queueName);

            string           hash    = null;
            HashSet <string> hashSet = null;

            if (!IgnoreHash)
            {
                hash    = json.GetMD5();
                hashSet = GetHashSet(queueName);
                if (hashSet.Contains(hash))
                {
                    return;
                }
            }

            var hostId        = ConfigSource.GetAppSetting("DQueue.HostId");
            var dequeueLocker = ReceptionAssistant.GetLocker(queueName + string.Format(Constants.DequeueLockerFlag, hostId));

            lock (dequeueLocker)
            {
                queue.Add(json.AddEnqueueTime());

                if (!IgnoreHash)
                {
                    hashSet.Add(hash);
                }

                Monitor.Pulse(dequeueLocker);
            }
        }