Exemplo n.º 1
0
        private void SerializeDatabaseMessage(DatabaseMessage message)
        {
            JsonDataTransferMessage dtm = ProduceDataTransferMessage(message);
            string json = JsonSerializer.Serialize(dtm);

            message.MessageBytes = Encoding.UTF8.GetBytes(json);
        }
Exemplo n.º 2
0
        private void ProcessMessage(object sender, BasicDeliverEventArgs args)
        {
            if (!(sender is EventingBasicConsumer consumer))
            {
                return;
            }

            if (!ConsumerTags.TryGetValue(args.ConsumerTag, out string exchange))
            {
                UnsubscribeConsumer(consumer, args.ConsumerTag);
                return;
            }

            JsonDataTransferMessage dataTransferMessage = null;

            try
            {
                byte[] body        = args.Body.ToArray();
                string messageBody = Encoding.UTF8.GetString(body);
                dataTransferMessage = JsonSerializer.Deserialize <JsonDataTransferMessage>(messageBody);
            }
            catch (Exception error)
            {
                FileLogger.Log(LOG_TOKEN, ExceptionHelper.GetErrorText(error));
            }

            if (dataTransferMessage == null)
            {
                RemovePoisonMessage(exchange, consumer, args.DeliveryTag);
                return;
            }

            bool success = true;
            IDatabaseMessageProducer producer = Services.GetService <IDatabaseMessageProducer>();

            try
            {
                DatabaseMessage message = producer.ProduceMessage(dataTransferMessage);
                success = producer.InsertMessage(message);
                if (success)
                {
                    consumer.Model.BasicAck(args.DeliveryTag, false);
                }
            }
            catch (Exception error)
            {
                success = false;
                FileLogger.Log(LOG_TOKEN, ExceptionHelper.GetErrorText(error));
            }

            if (!success)
            {
                ResetConsumer(args.ConsumerTag); // return unacked messages back to queue in the same order (!)
                FileLogger.Log(LOG_TOKEN, "Failed to process message. Consumer for exchange \"" + exchange + "\" has been reset.");
            }
        }
Exemplo n.º 3
0
        public DatabaseMessage ProduceMessage(JsonDataTransferMessage message)
        {
            DatabaseMessage dbm = new DatabaseMessage();

            dbm.Sender = message.Sender;
            if (message.Objects.Count > 0)
            {
                dbm.MessageType   = message.Objects[0].Type;
                dbm.MessageBody   = message.Objects[0].Body;
                dbm.OperationType = message.Objects[0].Operation;
            }
            return(dbm);
        }
Exemplo n.º 4
0
        private JsonDataTransferMessage ProduceDataTransferMessage(DatabaseMessage data)
        {
            JsonDataTransferMessage message = new JsonDataTransferMessage()
            {
                Sender = data.Sender
            };

            message.Objects.Add(new JsonDataTransferObject()
            {
                Type      = data.MessageType,
                Body      = data.MessageBody,
                Operation = data.OperationType
            });
            return(message);
        }
        public DatabaseMessage ProduceMessage(JsonDataTransferMessage message)
        {
            DatabaseMessage dbm = new DatabaseMessage()
            {
                Sender        = message.Sender,
                DateTimeStamp = DateTime.Now.AddYears(YearOffset)
            };

            if (message.Objects.Count > 0)
            {
                dbm.MessageType   = message.Objects[0].Type;
                dbm.MessageBody   = message.Objects[0].Body;
                dbm.OperationType = message.Objects[0].Operation;
            }

            return(dbm);
        }
Exemplo n.º 6
0
        private JsonDataTransferMessage GetJsonDataTransferMessage(BasicDeliverEventArgs args)
        {
            string messageBody = Encoding.UTF8.GetString(args.Body.Span);

            JsonDataTransferMessage dataTransferMessage = null;

            if (string.IsNullOrWhiteSpace(args.BasicProperties.Type))
            {
                try
                {
                    dataTransferMessage = JsonSerializer.Deserialize <JsonDataTransferMessage>(messageBody);
                }
                catch (Exception error)
                {
                    FileLogger.Log(LOG_TOKEN, ExceptionHelper.GetErrorText(error));
                }
            }
            else
            {
                dataTransferMessage = new JsonDataTransferMessage()
                {
                    Sender = (args.BasicProperties.AppId == null ? string.Empty : args.BasicProperties.AppId)
                };
                dataTransferMessage.Objects.Add(new JsonDataTransferObject()
                {
                    Type      = (args.BasicProperties.Type == null ? string.Empty : args.BasicProperties.Type),
                    Body      = messageBody,
                    Operation = string.Empty
                });

                if (args.BasicProperties.Headers != null)
                {
                    if (args.BasicProperties.Headers.TryGetValue("OperationType", out object value))
                    {
                        if (value is byte[] operationType)
                        {
                            dataTransferMessage.Objects[0].Operation = Encoding.UTF8.GetString(operationType);
                        }
                    }
                }
            }

            return(dataTransferMessage);
        }
Exemplo n.º 7
0
        private void ProcessMessage(object sender, BasicDeliverEventArgs args)
        {
            if (!(sender is EventingBasicConsumer consumer))
            {
                return;
            }

            if (!ConsumerTags.TryGetValue(args.ConsumerTag, out string exchange))
            {
                //TODO: review commented code
                //UnsubscribeConsumer(consumer, args.ConsumerTag);
                //return;

                if (!string.IsNullOrWhiteSpace(args.Exchange))
                {
                    exchange = args.Exchange;
                }
                else if (!string.IsNullOrWhiteSpace(args.RoutingKey))
                {
                    exchange = args.RoutingKey;
                }
                else
                {
                    exchange = "unknown";
                }
            }

            JsonDataTransferMessage dataTransferMessage = GetJsonDataTransferMessage(args);

            if (dataTransferMessage == null)
            {
                RemovePoisonMessage(exchange, consumer, args.DeliveryTag);
                return;
            }

            bool success = true;
            IDatabaseMessageProducer producer = Services.GetService <IDatabaseMessageProducer>();

            try
            {
                DatabaseMessage message = producer.ProduceMessage(dataTransferMessage);
                success = producer.InsertMessage(message);
                if (success)
                {
                    consumer.Model.BasicAck(args.DeliveryTag, false);
                }
            }
            catch (Exception error)
            {
                success = false;
                FileLogger.Log(LOG_TOKEN, ExceptionHelper.GetErrorText(error));
            }

            if (!success)
            {
                // return unacked messages back to queue in the same order (!)
                ResetConsumer(args.ConsumerTag);

                FileLogger.Log(LOG_TOKEN,
                               "Failed to process message. Consumer (tag = " + args.ConsumerTag.ToString()
                               + ") for exchange \"" + exchange + "\" has been reset.");
            }
        }