Esempio n. 1
0
        public Message <T> DeSerialize <T>(byte[] bytes, IBasicProperties messageProperties)
        {
            try
            {
                Encoding encoding     = Encoding.GetEncoding(messageProperties.ContentEncoding);
                string   instanceJson = encoding?.GetString(bytes);
                if (string.IsNullOrEmpty(instanceJson))
                {
                    throw new ArgumentOutOfRangeException("IBasicProperties.ContentEncoding");
                }

                if (!messageProperties.IsContentTypePresent())
                {
                    throw new ArgumentNullException("IBasicProperties.ContentType");
                }

                if (!messageProperties.ContentType.Equals(this.ContentType))
                {
                    throw new ArgumentException("Message ContentType do not match with selected Serialization ContentType", "IBasicProperties.ContentType");
                }

                var instance = JsonConvert.DeserializeObject <Message <T> >(instanceJson);

                return(instance);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Esempio n. 2
0
        public void CopyFrom(IBasicProperties basicProperties)
        {
            Preconditions.CheckNotNull(basicProperties, "basicProperties");

            if (basicProperties.IsContentTypePresent())         ContentType         = basicProperties.ContentType;
            if (basicProperties.IsContentEncodingPresent())     ContentEncoding     = basicProperties.ContentEncoding;
            if (basicProperties.IsDeliveryModePresent())        DeliveryMode        = basicProperties.DeliveryMode;
            if (basicProperties.IsPriorityPresent())            Priority            = basicProperties.Priority;
            if (basicProperties.IsCorrelationIdPresent())       CorrelationId       = basicProperties.CorrelationId;
            if (basicProperties.IsReplyToPresent())             ReplyTo             = basicProperties.ReplyTo;
            if (basicProperties.IsExpirationPresent())          Expiration          = basicProperties.Expiration;
            if (basicProperties.IsMessageIdPresent())           MessageId           = basicProperties.MessageId;
            if (basicProperties.IsTimestampPresent())           Timestamp           = basicProperties.Timestamp.UnixTime;
            if (basicProperties.IsTypePresent())                Type                = basicProperties.Type;
            if (basicProperties.IsUserIdPresent())              UserId              = basicProperties.UserId;
            if (basicProperties.IsAppIdPresent())               AppId               = basicProperties.AppId;
            if (basicProperties.IsClusterIdPresent())           ClusterId           = basicProperties.ClusterId;

            if (basicProperties.IsHeadersPresent())
            {
                foreach (var header in basicProperties.Headers)
                {
                    Headers.Add(header.Key, header.Value);
                }
            }
        }
Esempio n. 3
0
        public override async Task HandleBasicDeliver(
            string consumerTag,
            ulong deliveryTag,
            bool redelivered,
            string exchange,
            string routingKey,
            IBasicProperties properties,
            ReadOnlyMemory <byte> body)
        {
            // Sanity checks.
            if (!properties.IsTypePresent() || !properties.IsContentTypePresent())
            {
                this.logger.LogWarning(
                    "Found an unknow message {DeliveryTag} from {Exchange}.",
                    deliveryTag,
                    exchange);
                this.Model.BasicReject(deliveryTag, false);
                return;
            }

            var eventType   = properties.Type;
            var contentType = properties.ContentType;

            if (!string.Equals(contentType, "application/x-protobuf", StringComparison.OrdinalIgnoreCase))
            {
                // We want to requeue because the other instance with newer version may supports this new content type.
                this.Model.BasicReject(deliveryTag, true);
                return;
            }

            // Find a consumer to handler.
            if (!this.consumers.TryGetValue(eventType, out var consumer))
            {
                // We need to requeue if other instance can handle this event. This can happen if the newer version of
                // the service is deployed alongside old version.
                var requeue = await this.coordinator.IsEventSupportedAsync(eventType);

                this.Model.BasicReject(deliveryTag, requeue);
                return;
            }

            // Invoke consumer.
            var @event = consumer.EventParser.ParseFrom(new ReadOnlySequence <byte>(body));

            try
            {
                await consumer.ConsumeExecutor(@event);
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, "Unhandled exception occurred while consuming event {EventType}.", eventType);

                // Requeue to let the other instance handle this event instead.
                this.Model.BasicReject(deliveryTag, true);
                return;
            }

            this.Model.BasicAck(deliveryTag, false);
        }
        internal static ReceivedMessage CreateFromEvent(BasicDeliverEventArgs eventArgs)
        {
            if (eventArgs == null)
            {
                return(null);
            }

            IBasicProperties properties = eventArgs.BasicProperties;

            ReceivedMessage message = null;

            switch (properties?.ContentType?.ToLower())
            {
            case ContentTypes.JSON:
                message = new JSONReceivedMessage();
                break;

            case ContentTypes.Binary:
                message = new BinaryReceivedMessage();
                break;

            default:
                message = new ReceivedMessage();
                break;
            }

            message.DeliveryTag     = eventArgs.DeliveryTag;
            message.Exchange        = eventArgs.Exchange;
            message.RoutingKey      = eventArgs.RoutingKey;
            message.Redelivered     = eventArgs.Redelivered;
            message.ContentType     = (properties?.IsContentTypePresent() ?? false) ? properties.ContentType : null;
            message.ContentEncoding = (properties?.IsContentEncodingPresent() ?? false) ? properties.ContentEncoding : null;

            message.ExpandBody(eventArgs.Body);

            if (properties == null)
            {
                return(message);
            }

            message.Type             = properties.IsTypePresent() ? properties.Type : null;
            message.Persistent       = properties.IsDeliveryModePresent() && properties.Persistent;
            message.ApplicationName  = properties.IsAppIdPresent() ? properties.AppId : null;
            message.ReplyToQueueName = properties.IsReplyToPresent() ? properties.ReplyTo : null;

            if (properties.IsMessageIdPresent() && Guid.TryParse(properties.MessageId, out Guid messageId))
            {
                message.MessageID = messageId;
            }

            if (properties.IsCorrelationIdPresent() && Guid.TryParse(properties.CorrelationId, out Guid correlationId))
            {
                message.CorrelationID = correlationId;
            }

            return(message);
        }
        public MessageBasicProperties(IBasicProperties basicProperties)
            : this()
        {
            ContentTypePresent = basicProperties.IsContentTypePresent();
            ContentEncodingPresent = basicProperties.IsContentEncodingPresent();
            HeadersPresent = basicProperties.IsHeadersPresent();
            DeliveryModePresent = basicProperties.IsDeliveryModePresent();
            PriorityPresent = basicProperties.IsPriorityPresent();
            CorrelationIdPresent = basicProperties.IsCorrelationIdPresent();
            ReplyToPresent = basicProperties.IsReplyToPresent();
            ExpirationPresent = basicProperties.IsExpirationPresent();
            MessageIdPresent = basicProperties.IsMessageIdPresent();
            TimestampPresent = basicProperties.IsTimestampPresent();
            TypePresent = basicProperties.IsTypePresent();
            UserIdPresent = basicProperties.IsUserIdPresent();
            AppIdPresent = basicProperties.IsAppIdPresent();
            ClusterIdPresent = basicProperties.IsClusterIdPresent();

            ContentType = basicProperties.ContentType;
            ContentEncoding = basicProperties.ContentEncoding;
            DeliveryMode = basicProperties.DeliveryMode;
            Priority = basicProperties.Priority;
            CorrelationId = basicProperties.CorrelationId;
            ReplyTo = basicProperties.ReplyTo;
            Expiration = basicProperties.Expiration;
            MessageId = basicProperties.MessageId;
            Timestamp = basicProperties.Timestamp.UnixTime;
            Type = basicProperties.Type;
            UserId = basicProperties.UserId;
            AppId = basicProperties.AppId;
            ClusterId = basicProperties.ClusterId;

            if(basicProperties.IsHeadersPresent())
            {
                foreach (DictionaryEntry header in basicProperties.Headers)
                {
                    Headers.Add(header.Key, header.Value);
                }
            }
        }
        public void CopyFrom(IBasicProperties basicProperties)
        {
            ContentTypePresent     = basicProperties.IsContentTypePresent();
            ContentEncodingPresent = basicProperties.IsContentEncodingPresent();
            HeadersPresent         = basicProperties.IsHeadersPresent();
            DeliveryModePresent    = basicProperties.IsDeliveryModePresent();
            PriorityPresent        = basicProperties.IsPriorityPresent();
            CorrelationIdPresent   = basicProperties.IsCorrelationIdPresent();
            ReplyToPresent         = basicProperties.IsReplyToPresent();
            ExpirationPresent      = basicProperties.IsExpirationPresent();
            MessageIdPresent       = basicProperties.IsMessageIdPresent();
            TimestampPresent       = basicProperties.IsTimestampPresent();
            TypePresent            = basicProperties.IsTypePresent();
            UserIdPresent          = basicProperties.IsUserIdPresent();
            AppIdPresent           = basicProperties.IsAppIdPresent();
            ClusterIdPresent       = basicProperties.IsClusterIdPresent();

            ContentType     = basicProperties.ContentType;
            ContentEncoding = basicProperties.ContentEncoding;
            DeliveryMode    = basicProperties.DeliveryMode;
            Priority        = basicProperties.Priority;
            CorrelationId   = basicProperties.CorrelationId;
            ReplyTo         = basicProperties.ReplyTo;
            Expiration      = basicProperties.Expiration;
            MessageId       = basicProperties.MessageId;
            Timestamp       = basicProperties.Timestamp.UnixTime;
            Type            = basicProperties.Type;
            UserId          = basicProperties.UserId;
            AppId           = basicProperties.AppId;
            ClusterId       = basicProperties.ClusterId;

            if (basicProperties.IsHeadersPresent())
            {
                foreach (DictionaryEntry header in basicProperties.Headers)
                {
                    Headers.Add(header.Key, header.Value);
                }
            }
        }
Esempio n. 7
0
        public static void CopyTo(this IBasicProperties basicProperties, MessageProperties messageProperties)
        {
            if (basicProperties.IsContentTypePresent())
            {
                messageProperties.ContentType = basicProperties.ContentType;
            }
            if (basicProperties.IsContentEncodingPresent())
            {
                messageProperties.ContentEncoding = basicProperties.ContentEncoding;
            }
            if (basicProperties.IsDeliveryModePresent())
            {
                messageProperties.DeliveryMode = basicProperties.DeliveryMode;
            }
            if (basicProperties.IsPriorityPresent())
            {
                messageProperties.Priority = basicProperties.Priority;
            }
            if (basicProperties.IsCorrelationIdPresent())
            {
                messageProperties.CorrelationId = basicProperties.CorrelationId;
            }
            if (basicProperties.IsReplyToPresent())
            {
                messageProperties.ReplyTo = basicProperties.ReplyTo;
            }
            if (basicProperties.IsExpirationPresent())
            {
                messageProperties.Expiration = basicProperties.Expiration;
            }
            if (basicProperties.IsMessageIdPresent())
            {
                messageProperties.MessageId = basicProperties.MessageId;
            }
            if (basicProperties.IsTimestampPresent())
            {
                messageProperties.Timestamp = basicProperties.Timestamp.UnixTime;
            }
            if (basicProperties.IsTypePresent())
            {
                messageProperties.Type = basicProperties.Type;
            }
            if (basicProperties.IsUserIdPresent())
            {
                messageProperties.UserId = basicProperties.UserId;
            }
            if (basicProperties.IsAppIdPresent())
            {
                messageProperties.AppId = basicProperties.AppId;
            }
            if (basicProperties.IsClusterIdPresent())
            {
                messageProperties.ClusterId = basicProperties.ClusterId;
            }

            if (basicProperties.IsHeadersPresent())
            {
                foreach (DictionaryEntry header in basicProperties.Headers)
                {
                    messageProperties.Headers.Add(header.Key, header.Value);
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        ///     Copies IBasicProperties to MessageProperties
        /// </summary>
        public void CopyFrom(IBasicProperties basicProperties)
        {
            Preconditions.CheckNotNull(basicProperties, "basicProperties");

            if (basicProperties.IsContentTypePresent())
            {
                ContentType = basicProperties.ContentType;
            }
            if (basicProperties.IsContentEncodingPresent())
            {
                ContentEncoding = basicProperties.ContentEncoding;
            }
            if (basicProperties.IsDeliveryModePresent())
            {
                DeliveryMode = basicProperties.DeliveryMode;
            }
            if (basicProperties.IsPriorityPresent())
            {
                Priority = basicProperties.Priority;
            }
            if (basicProperties.IsCorrelationIdPresent())
            {
                CorrelationId = basicProperties.CorrelationId;
            }
            if (basicProperties.IsReplyToPresent())
            {
                ReplyTo = basicProperties.ReplyTo;
            }
            if (basicProperties.IsExpirationPresent())
            {
                Expiration = basicProperties.Expiration;
            }
            if (basicProperties.IsMessageIdPresent())
            {
                MessageId = basicProperties.MessageId;
            }
            if (basicProperties.IsTimestampPresent())
            {
                Timestamp = basicProperties.Timestamp.UnixTime;
            }
            if (basicProperties.IsTypePresent())
            {
                Type = basicProperties.Type;
            }
            if (basicProperties.IsUserIdPresent())
            {
                UserId = basicProperties.UserId;
            }
            if (basicProperties.IsAppIdPresent())
            {
                AppId = basicProperties.AppId;
            }
            if (basicProperties.IsClusterIdPresent())
            {
                ClusterId = basicProperties.ClusterId;
            }

            if (basicProperties.IsHeadersPresent())
            {
                Headers = basicProperties.Headers == null || basicProperties.Headers.Count == 0
                    ? null
                    : new Dictionary <string, object>(basicProperties.Headers);
            }
        }
Esempio n. 9
0
        public void CopyFrom(IBasicProperties basicProperties)
        {
            GuardHelper.ArgumentNotNull(() => basicProperties);

            if (basicProperties.IsContentTypePresent())
            {
                ContentType = basicProperties.ContentType;
            }
            if (basicProperties.IsContentEncodingPresent())
            {
                ContentEncoding = basicProperties.ContentEncoding;
            }
            if (basicProperties.IsDeliveryModePresent())
            {
                DeliveryMode = basicProperties.DeliveryMode;
            }
            if (basicProperties.IsPriorityPresent())
            {
                Priority = basicProperties.Priority;
            }
            if (basicProperties.IsCorrelationIdPresent())
            {
                CorrelationId = basicProperties.CorrelationId;
            }
            if (basicProperties.IsReplyToPresent())
            {
                ReplyTo = basicProperties.ReplyTo;
            }
            if (basicProperties.IsExpirationPresent())
            {
                Expiration = basicProperties.Expiration;
            }
            if (basicProperties.IsMessageIdPresent())
            {
                MessageId = basicProperties.MessageId;
            }
            if (basicProperties.IsTimestampPresent())
            {
                Timestamp = basicProperties.Timestamp.UnixTime;
            }
            if (basicProperties.IsTypePresent())
            {
                Type = basicProperties.Type;
            }
            if (basicProperties.IsUserIdPresent())
            {
                UserId = basicProperties.UserId;
            }
            if (basicProperties.IsAppIdPresent())
            {
                AppId = basicProperties.AppId;
            }
            if (basicProperties.IsClusterIdPresent())
            {
                ClusterId = basicProperties.ClusterId;
            }

            if (basicProperties.IsHeadersPresent())
            {
                foreach (var header in basicProperties.Headers)
                {
                    Headers.Add(header.Key, header.Value);
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        ///     Converts IBasicProperties to a string
        /// </summary>
        /// <param name="extendedObject">objected being extended</param>
        /// <returns>formatted string of IBasicProperties</returns>
        public static string ToFormatString(this IBasicProperties extendedObject)
        {
            Arguments.NotNull(extendedObject, nameof(extendedObject));

            var sb = new StringBuilder();

            if (extendedObject.IsAppIdPresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "AppID:{0} ", extendedObject.AppId);
            }

            if (extendedObject.IsClusterIdPresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "ClusterId:{0} ", extendedObject.ClusterId);
            }

            if (extendedObject.IsContentEncodingPresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "ContentEncoding:{0} ", extendedObject.ContentEncoding);
            }

            if (extendedObject.IsContentTypePresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "ContentType:{0} ", extendedObject.ContentType);
            }

            if (extendedObject.IsCorrelationIdPresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "CorrelationId:{0} ", extendedObject.CorrelationId);
            }

            if (extendedObject.IsDeliveryModePresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "DeliveryMode:{0} ", extendedObject.DeliveryMode);
            }

            if (extendedObject.IsExpirationPresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "Expiration:{0} ", extendedObject.Expiration);
            }

            if (extendedObject.IsHeadersPresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "Headers:{0} ", extendedObject.Headers.StringFormat());
            }

            if (extendedObject.IsMessageIdPresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "MessageId:{0} ", extendedObject.MessageId);
            }

            if (extendedObject.IsPriorityPresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "Priority:{0} ", extendedObject.Priority);
            }

            if (extendedObject.IsReplyToPresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "ReplyTo:{0} ", extendedObject.ReplyTo);
            }

            if (extendedObject.IsTimestampPresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "Timestamp:{0} ", extendedObject.Timestamp);
            }

            if (extendedObject.IsTypePresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "Type:{0} ", extendedObject.Type);
            }

            if (extendedObject.IsUserIdPresent())
            {
                sb.AppendFormat(CultureInfo.InvariantCulture, "UserId:{0} ", extendedObject.UserId);
            }

            return(sb.ToString());
        }
Esempio n. 11
0
        public override async Task HandleBasicDeliver(
            string consumerTag,
            ulong deliveryTag,
            bool redelivered,
            string exchange,
            string routingKey,
            IBasicProperties properties,
            ReadOnlyMemory <byte> body)
        {
            // FIXME: This is a workaround until this PR is released:
            // https://github.com/rabbitmq/rabbitmq-dotnet-client/pull/946
            await Task.Yield();

            // Sanity checks.
            if (!properties.IsTypePresent() || !properties.IsContentTypePresent())
            {
                this.logger.LogWarning(
                    "Found an unknow message {DeliveryTag} from {Exchange}.",
                    deliveryTag,
                    exchange);
                this.Model.BasicReject(deliveryTag, false);
                return;
            }

            var eventType   = properties.Type;
            var contentType = properties.ContentType;

            if (!string.Equals(contentType, "application/x-protobuf", StringComparison.OrdinalIgnoreCase))
            {
                // We want to requeue because the other instance with newer version may supports this new content type.
                this.logger.LogInformation(
                    "Unknown content type {ContentType} for message {Message}.",
                    contentType,
                    eventType);
                this.Model.BasicReject(deliveryTag, true);
                return;
            }

            // Find a consumer to handler.
            if (!this.consumers.TryGetValue(eventType, out var consumer))
            {
                // We need to requeue if other instance can handle this event. This can happen if the newer version of
                // the service is deployed alongside old version.
                var requeue = await this.coordinator.IsEventSupportedAsync(eventType);

                if (requeue)
                {
                    this.logger.LogInformation(
                        "Returning {EventType} to the queue due to we cannot handle it but the other instance can.",
                        eventType);
                    this.Model.BasicReject(deliveryTag, true);
                }
                else
                {
                    this.logger.LogInformation("Discarding {EventType} due to we cannot handle it.", eventType);
                }

                return;
            }

            // Deserialize event.
            var @event = consumer.EventParser.ParseFrom(new ReadOnlySequence <byte>(body));

            this.logger.LogInformation("Consuming {EventType}: {EventData}", eventType, @event);

            // Invoke consumer.
            bool?success = null;

            try
            {
                success = await consumer.ConsumeExecutor(@event);
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, "Unhandled exception occurred while consuming {EventType}.", eventType);
            }

            if (success == null || !success.Value)
            {
                // Requeue to let the other instance handle this event instead.
                this.logger.LogInformation(
                    "Unsuccessful consuming {EventType}, returning event to the queue to let other instance handle it.",
                    eventType);
                this.Model.BasicReject(deliveryTag, true);
                return;
            }

            this.logger.LogInformation("Successful consuming {EventType}.", eventType);
            this.Model.BasicAck(deliveryTag, false);
        }
        internal void CopyFrom(IBasicProperties from)
        {
            if (from == null)
                throw new ArgumentNullException(nameof(from));

            if (from.IsAppIdPresent()) AppId = from.AppId;
            if (from.IsClusterIdPresent()) ClusterId = from.ClusterId;
            if (from.IsContentEncodingPresent()) ContentEncoding = from.ContentEncoding;
            if (from.IsContentTypePresent()) ContentType = from.ContentType;
            if (from.IsCorrelationIdPresent()) CorrelationId = from.CorrelationId;
            if (from.IsDeliveryModePresent()) DeliveryMode = (LinkMessageDeliveryMode) from.DeliveryMode;
            if (from.IsReplyToPresent()) ReplyTo = from.ReplyTo;
            if (from.IsExpirationPresent()) Expiration = TimeSpan.FromMilliseconds(int.Parse(from.Expiration));
            if (from.IsMessageIdPresent()) MessageId = from.MessageId;
            if (from.IsTimestampPresent()) TimeStamp = from.Timestamp.UnixTime;
            if (from.IsTypePresent()) Type = from.Type;
            if (from.IsUserIdPresent()) UserId = from.UserId;
            if (from.IsPriorityPresent()) Priority = from.Priority;

            if (from.IsHeadersPresent())
            {
                foreach (var header in from.Headers)
                {
                    Headers[header.Key] = header.Value;
                }
            }
        }
Esempio n. 13
0
            public EasyNetQMessageProperties(IBasicProperties basicProperties) : this()
            {
                if (basicProperties.IsContentTypePresent())
                {
                    ContentType = basicProperties.ContentType;
                }
                if (basicProperties.IsContentEncodingPresent())
                {
                    ContentEncoding = basicProperties.ContentEncoding;
                }
                if (basicProperties.IsDeliveryModePresent())
                {
                    DeliveryMode = basicProperties.DeliveryMode;
                }
                if (basicProperties.IsPriorityPresent())
                {
                    Priority = basicProperties.Priority;
                }
                if (basicProperties.IsCorrelationIdPresent())
                {
                    CorrelationId = basicProperties.CorrelationId;
                }
                if (basicProperties.IsReplyToPresent())
                {
                    ReplyTo = basicProperties.ReplyTo;
                }
                if (basicProperties.IsExpirationPresent())
                {
                    Expiration = basicProperties.Expiration;
                }
                if (basicProperties.IsMessageIdPresent())
                {
                    MessageId = basicProperties.MessageId;
                }
                if (basicProperties.IsTimestampPresent())
                {
                    Timestamp = basicProperties.Timestamp.UnixTime;
                }
                if (basicProperties.IsTypePresent())
                {
                    Type = basicProperties.Type;
                }
                if (basicProperties.IsUserIdPresent())
                {
                    UserId = basicProperties.UserId;
                }
                if (basicProperties.IsAppIdPresent())
                {
                    AppId = basicProperties.AppId;
                }
                if (basicProperties.IsClusterIdPresent())
                {
                    ClusterId = basicProperties.ClusterId;
                }

                if (!basicProperties.IsHeadersPresent())
                {
                    return;
                }

                foreach (var(key, value) in basicProperties.Headers)
                {
                    Headers.Add(key, (byte[])value);
                }
            }