Esempio n. 1
0
        public bool Handle(T message)
        {
            var lockKey      = string.Format("{2}-{1}-{0}", _handlerName, typeof(T).Name.ToLower(), message.UniqueKey());
            var lockResponse = _messageLock.TryAquireLock(lockKey, TimeSpan.FromSeconds(_timeOut));

            if (!lockResponse.DoIHaveExclusiveLock)
            {
                if (lockResponse.IsMessagePermanentlyLocked)
                {
                    return(RemoveTheMessageFromTheQueue);
                }

                return(LeaveItInTheQueue);
            }

            try
            {
                var successfullyHandled = _inner.Handle(message);
                if (successfullyHandled)
                {
                    _messageLock.TryAquireLockPermanently(lockKey);
                }
                return(successfullyHandled);
            }
            catch
            {
                _messageLock.ReleaseLock(lockKey);
                throw;
            }
        }
        public async Task <bool> Handle(T message)
        {
            var lockKey      = $"{message.UniqueKey()}-{typeof(T).Name.ToLower()}-{_handlerName}";
            var lockResponse = _messageLock.TryAquireLock(lockKey, TimeSpan.FromSeconds(_timeOut));

            if (!lockResponse.DoIHaveExclusiveLock)
            {
                if (lockResponse.IsMessagePermanentlyLocked)
                {
                    return(RemoveTheMessageFromTheQueue);
                }

                return(LeaveItInTheQueue);
            }

            try
            {
                var successfullyHandled = await _inner.Handle(message).ConfigureAwait(false);

                if (successfullyHandled)
                {
                    _messageLock.TryAquireLockPermanently(lockKey);
                }
                return(successfullyHandled);
            }
            catch
            {
                _messageLock.ReleaseLock(lockKey);
                throw;
            }
        }