Esempio n. 1
0
        /// <summary>
        /// A template method responsible for processing a received message
        /// </summary>
        /// <param name="message">received message to be processed</param>
        /// <param name="physicalOrLogicalChannel"> name of a channel from that a message is received and processed</param>
        protected virtual void HandleMessageImpl(Message message, string physicalOrLogicalChannel)
        {
            var validationResult = _messageValidator.Validate(message);

            switch (validationResult)
            {
            case SuccessValidationResult _:
                OnExpectedMessage(message, physicalOrLogicalChannel);
                break;

            case ValidationResultForMissedMessages missedMessagesResult:
                var lastProcessedMessageId = missedMessagesResult.LastProcessedMessageId;
                OnMissedMessages(message, lastProcessedMessageId, physicalOrLogicalChannel);
                OnExpectedMessage(message, physicalOrLogicalChannel);
                break;

            case AlreadyProcessedValidationResult _:
                OnDuplicatedMessage(message, physicalOrLogicalChannel);
                break;

            default:
                OnOtherValidationResult(message, validationResult, physicalOrLogicalChannel);
                break;
            }
        }
Esempio n. 2
0
        private void MessageReceived(object sender, BinaryMessage message)
        {
            if (!_validator.Validate(message))
            {
                // If you send us crap we shut you off!
                Reconnect();
                return;
            }

            try
            {
                // ReSharper disable once PossibleNullReferenceException
                Received(this, message);
            }
            catch (Exception ex)
            {
                Logger.LogException(LogLevel.Fatal, ex, "Received event ran into an exception!");
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Добавляет еще одного обработчика сообщений.
        /// </summary>
        /// <param name="label">
        /// Метка сообщения, которое может быть обработано.
        /// </param>
        /// <param name="consumer">
        /// Обработчик сообщения.
        /// </param>
        /// <param name="validator">
        /// Механизм проверки входящего сообщения.
        /// </param>
        /// <typeparam name="T">
        /// Тип входящего сообщения.
        /// </typeparam>
        public void RegisterConsumer <T>(MessageLabel label, IConsumerOf <T> consumer, IMessageValidator validator) where T : class
        {
            ConsumingAction consumingAction = delivery =>
            {
                IConsumingContext <T> context = delivery.BuildConsumingContext <T>(label);

                if (validator != null)
                {
                    validator.Validate(context.Message).ThrowIfBroken();
                }
                else
                {
                    this.validatorRegistry.Validate(context.Message);
                }

                consumer.Handle(context);
            };

            this.consumers[label] = consumingAction;
        }
Esempio n. 4
0
        /// <summary>
        /// Добавляет еще одного обработчика сообщений.
        /// </summary>
        /// <param name="label">
        /// Метка сообщения, которое может быть обработано.
        /// </param>
        /// <param name="consumer">
        /// Обработчик сообщения.
        /// </param>
        /// <param name="validator">
        /// Механизм проверки входящего сообщения.
        /// </param>
        /// <typeparam name="T">
        /// Тип входящего сообщения.
        /// </typeparam>
        public void RegisterConsumer <T>(MessageLabel label, IConsumerOf <T> consumer, IMessageValidator validator) where T : class
        {
            ConsumingAction consumingAction = delivery =>
            {
                IConsumingContext <T> context = delivery.BuildConsumingContext <T>(label);

                if (validator != null)
                {
                    validator.Validate(context.Message).ThrowIfBroken();
                }
                else
                {
                    this.validatorRegistry.Validate(context.Message);
                }

                var sw = Stopwatch.StartNew();

                consumer.Handle(context);

                this.MetricsCollector?.Histogram("contour.rmq.consuming.duration", sw.ElapsedMilliseconds, 1D, new[] { "endpoint:" + this.busContext.Endpoint.Address, "label:" + label.ToString() });
            };

            this.consumers[label] = consumingAction;
        }
Esempio n. 5
0
        public async Task <bool> TryAddAsync(EncryptedMessage message)
        {
            var id = message.Id;

            if (!_messageValidator.Validate(message))
            {
                return(false);
            }
            if (GetMessageById(message.Id) != null)
            {
                return(false);
            }

            try
            {
                var messageDb = new EncryptedMessageStore(message);
                _applicationDatabase.Messages.Add(messageDb);
                return(await _applicationDatabase.SaveChangesAsync() > 0);
            }
            catch (DbException)
            {
                return(false);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Добавляет еще одного обработчика сообщений.
        /// </summary>
        /// <param name="label">
        /// Метка сообщения, которое может быть обработано.
        /// </param>
        /// <param name="consumer">
        /// Обработчик сообщения.
        /// </param>
        /// <param name="validator">
        /// Механизм проверки входящего сообщения.
        /// </param>
        /// <typeparam name="T">
        /// Тип входящего сообщения.
        /// </typeparam>
        public void RegisterConsumer <T>(MessageLabel label, IConsumerOf <T> consumer, IMessageValidator validator) where T : class
        {
            ConsumingAction consumingAction = delivery =>
            {
                IConsumingContext <T> context = delivery.BuildConsumingContext <T>(label);

                if (validator != null)
                {
                    validator.Validate(context.Message).ThrowIfBroken();
                }
                else
                {
                    this.validatorRegistry.Validate(context.Message);
                }

                this.logger.Trace(
                    m => m($"{typeof(T)}: получил сообщение " +
                           $"[{JsonConvert.SerializeObject(context.Message.Payload)}] на обработку"));

                consumer.Handle(context);
            };

            this.consumers[label] = consumingAction;
        }
        //public static IDependencyResolver DependencyResolver;

        /// <summary>
        ///     The validate.
        /// </summary>
        /// <param name="toValidationInstance">
        ///     The to validation instance.
        /// </param>
        /// <returns>
        ///     The <see cref="ValidationNotification" />.
        /// </returns>
        public ValidationNotification Validate(object toValidationInstance)
        {
            return(MessageValidator.Validate(toValidationInstance));
        }