Exemple #1
0
        public void TestKeyLocker()
        {
            // Arrange.
            var locker = new KeyLocker <int, object>();

            // Act.
            var lock_1_1 = locker.GetLock(1);
            var lock_1_2 = locker.GetLock(1);
            var lock_2_1 = locker.GetLock(2);

            locker.FreeLock(1);
            locker.FreeLock(1);

            // Assert.
            Assert.True(lock_1_1 == lock_1_2);
            Assert.True(lock_1_1 != lock_2_1);
            Assert.True(lock_1_1 != locker.GetLock(1));
        }
Exemple #2
0
        /// <summary>
        /// Принять сообщение с именем группы.
        /// </summary>
        /// <param name="message">Принимаемое сообщение.</param>
        /// <param name="groupName">Имя группы.</param>
        public override void AcceptMessage(ServiceBusMessage message, string groupName)
        {
            _logger.LogIncomingMessage(message);
            try
            {
                if (!_objectRepository.GetRestrictionsForClient(message.ClientID).Any(x => x.MessageType.ID == message.MessageTypeID))
                {
                    _logger.LogInformation("Отправка запрещена.", $"Клиент {message.ClientID} не имеет прав на отправку сообщения типа {message.MessageTypeID}.");
                    return;
                }

                IEnumerable <Subscription> subscriptions = _subscriptionsManager.GetSubscriptionsForMsgType(message.MessageTypeID, message.ClientID);

                if (!subscriptions.Any())
                {
                    _logger.LogInformation("Для сообщения нет ни одной подписки.", $"Было получено сообщение, для которого нет ни одной активной подписки (ID типа сообщения: {message.MessageTypeID}).");
                    return;
                }

                string signature = $"{nameof(DefaultReceivingManager)}.{nameof(AcceptMessage)}({nameof(ServiceBusMessage)} {nameof(message)}, string {nameof(groupName)})";
                foreach (var subscription in subscriptions)
                {
                    Tuple <Guid, string> lockKey = Tuple.Create(((KeyGuid)subscription.__PrimaryKey).Guid, groupName);
                    object lockObject            = locker.GetLock(lockKey);
                    try
                    {
                        lock (lockObject)
                        {
                            LoadingCustomizationStruct lcs = MessageBS.GetMessagesWithGroupLCS(subscription.Client, subscription.MessageType, groupName);
                            lcs.ColumnsSort = new[] { new ColumnsSortDef(Information.ExtractPropertyPath <Message>(m => m.SendingTime), SortOrder.Desc) };

                            Stopwatch    stopwatch        = Stopwatch.StartNew();
                            DataObject[] existingMessages = _dataService.LoadObjects(lcs);
                            stopwatch.Stop();

                            _statisticsService.NotifyAvgTimeSql(subscription, (int)stopwatch.ElapsedMilliseconds, $"{signature} - find existing message.");

                            var messageWithGroup = new Message();
                            if (existingMessages.Length > 0)
                            {
                                messageWithGroup.SetExistObjectPrimaryKey(existingMessages[0].__PrimaryKey);

                                stopwatch = Stopwatch.StartNew();
                                _dataService.LoadObject(Message.Views.MessageEditView, messageWithGroup);
                                stopwatch.Stop();

                                _statisticsService.NotifyAvgTimeSql(subscription, (int)stopwatch.ElapsedMilliseconds, $"{signature} - load existing message.");
                            }

                            ServiceHelper.SetMessageWithGroupValues(message, subscription, messageWithGroup, groupName, _dataService, _logger, _statisticsService);
                            messageWithGroup.SendingTime = DateTime.Now;

                            stopwatch = Stopwatch.StartNew();
                            _dataService.UpdateObject(messageWithGroup);
                            stopwatch.Stop();

                            _statisticsService.NotifyAvgTimeSql(subscription, (int)stopwatch.ElapsedMilliseconds, $"{signature} - update message.");
                        }
                    }
                    finally
                    {
                        locker.FreeLock(lockKey);
                    }

                    _statisticsService.NotifyMessageReceived(subscription);
                }
            }
            catch (Exception e)
            {
                _logger.LogUnhandledException(e);
                throw;
            }
        }