예제 #1
0
        /// <summary>
        /// Imitates sending message throw SB.
        /// </summary>
        /// <param name="senderId">Sender of message.</param>
        /// <param name="receiverId">Receiver of message.</param>
        /// <param name="messageType">Type of message.</param>
        private void ImitateServiceBusMessage(string senderId, string receiverId, string messageType)
        {
            var subscriptions = _subscriptionsManager.GetSubscriptionsForMsgType(messageType, senderId);

            if (subscriptions == null || !subscriptions.Any())
            {
                throw new Exception(string.Format("Не найдена подписка для типа сообщения {0}.", messageType));
            }
            var subscription = subscriptions.First();

            var dummyMessageForEsb = new ServiceBusMessage()
            {
                ClientID      = senderId,
                MessageTypeID = messageType,
            };

            _logger.LogIncomingMessage(dummyMessageForEsb);
            _statisticsService.NotifyMessageSent(subscriptions.First());

            var dummyMessage = new Message()
            {
                Recipient = new Client()
                {
                    ID = receiverId
                },
                MessageType = new MessageType()
                {
                    ID = messageType
                },
            };

            _logger.LogOutgoingMessage(dummyMessage);
            _statisticsService.NotifyMessageReceived(subscriptions.First());
        }
예제 #2
0
        /// <summary>
        /// Notify statistics component that message has been received.
        /// </summary>
        /// <param name="client">Client, recipient of message.</param>
        /// <param name="messageType">Type of message.</param>
        public void NotifyMessageReceived(Client client, MessageType messageType)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            if (messageType == null)
            {
                throw new ArgumentNullException(nameof(messageType));
            }

            var subscriptions = _subscriptions.GetSubscriptionsForMsgType(messageType.ID, client.ID);

            if (subscriptions != null && subscriptions.Count() != 0)
            {
                NotifyMessageReceived(subscriptions.First());
            }
            else
            {
                _logger.LogError("Can't find subscription for received message", $"Client: {client.Name} (Id: {client.ID}), Message type: {messageType.Name} (Id: {messageType.ID})");
            }
        }
예제 #3
0
        /// <summary>
        /// Принять сообщение.
        /// </summary>
        /// <param name="message">Принимаемое сообщение.</param>
        public override void AcceptMessage(ServiceBusMessage message)
        {
            _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}).");
                }

                // Формируем для найденных подписчиков сообщения.
                var messages = new List <Message>();
                foreach (var subscription in subscriptions)
                {
                    var msg = new Message()
                    {
                        ReceivingTime = DateTime.Now,
                        Recipient     = subscription.Client,
                        Priority      = 0,
                        IsSending     = false,
                    };

                    ServiceHelper.AddSenderToMessage(message, msg, null, _dataService, _logger, _statisticsService);
                    ServiceHelper.SaveTag(message, msg);

                    msg.MessageType      = subscription.MessageType;
                    msg.Body             = message.Body;
                    msg.BinaryAttachment = message.Attachment;
                    msg.Priority         = message.Priority;
                    msg.SendingTime      = DateTime.Now;

                    messages.Add(msg);

                    if (subscription.IsCallback)
                    {
                        _sendingManager.QueueForSending(msg);
                    }

                    _statisticsService.NotifyMessageReceived(subscription);
                }

                var dobjs = messages.Cast <DataObject>().ToArray();

                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                _dataService.UpdateObjects(ref dobjs);

                stopwatch.Stop();
                long time = stopwatch.ElapsedMilliseconds;
                _statisticsService.NotifyAvgTimeSql(null, (int)time, "DefaultReceivingManager.AcceptMessage(MessageForESB message) update Сообщения.");
            }
            catch (Exception e)
            {
                _logger.LogUnhandledException(e);
                throw;
            }
        }