public bool MS_InsertMessage(DatabaseMessage message)
        {
            int recordsAffected = 0;

            using (SqlConnection connection = new SqlConnection(Settings.DatabaseSettings.ConnectionString))
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandType    = CommandType.Text;
                    command.CommandText    = IncomingQueueInsertScript;
                    command.CommandTimeout = 60; // seconds

                    command.Parameters.AddWithValue("Идентификатор", message.Uuid);
                    command.Parameters.AddWithValue("ДатаВремя", message.DateTimeStamp);
                    command.Parameters.AddWithValue("Отправитель", message.Sender);
                    command.Parameters.AddWithValue("ТипОперации", message.OperationType);
                    command.Parameters.AddWithValue("ТипСообщения", message.MessageType);
                    command.Parameters.AddWithValue("ТелоСообщения", message.MessageBody);
                    command.Parameters.AddWithValue("КоличествоОшибок", message.ErrorCount);
                    command.Parameters.AddWithValue("ОписаниеОшибки", message.ErrorDescription);

                    connection.Open();
                    recordsAffected = command.ExecuteNonQuery();
                }
            return(recordsAffected != 0);
        }
Пример #2
0
 public bool InsertMessage(DatabaseMessage message)
 {
     // TODO: use try-catch statement and handle exceptions thrown from using statements in this method !?
     if (Settings.DatabaseSettings.DatabaseProvider == DatabaseProviders.SQLServer)
     {
         return(SQLServer_InsertMessage(message));
     }
     return(PostgreSQL_InsertMessage(message));
 }
Пример #3
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.");
            }
        }
Пример #4
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);
        }
 public bool InsertMessage(DatabaseMessage message)
 {
     if (Settings.DatabaseSettings.DatabaseProvider == DatabaseProvider.SQLServer)
     {
         try
         {
             return(MS_InsertMessage(message));
         }
         catch (Exception error)
         {
             FileLogger.Log(LOG_TOKEN, ExceptionHelper.GetErrorText(error));
             return(false);
         }
     }
     return(PG_InsertMessage(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);
        }
        public bool PG_InsertMessage(DatabaseMessage message)
        {
            int recordsAffected = 0;

            {
                NpgsqlConnection connection = new NpgsqlConnection(Settings.DatabaseSettings.ConnectionString);
                NpgsqlCommand    command    = connection.CreateCommand();
                command.CommandType    = CommandType.Text;
                command.CommandText    = IncomingQueueInsertScript;
                command.CommandTimeout = 60; // seconds

                command.Parameters.AddWithValue("Идентификатор", message.Uuid.ToByteArray());
                command.Parameters.AddWithValue("ДатаВремя", message.DateTimeStamp);
                command.Parameters.AddWithValue("Отправитель", message.Sender);
                command.Parameters.AddWithValue("ТипОперации", message.OperationType);
                command.Parameters.AddWithValue("ТипСообщения", message.MessageType);
                command.Parameters.AddWithValue("ТелоСообщения", message.MessageBody);
                command.Parameters.AddWithValue("КоличествоОшибок", message.ErrorCount);
                command.Parameters.AddWithValue("ОписаниеОшибки", message.ErrorDescription);

                try
                {
                    connection.Open();
                    recordsAffected = command.ExecuteNonQuery();
                }
                catch (Exception error)
                {
                    // FIXME: replace with using statement and handle exceptions outside the method !?
                    FileLogger.Log(LOG_TOKEN, ExceptionHelper.GetErrorText(error));
                }
                finally
                {
                    DisposeDatabaseResources(connection, null, command);
                }
            }

            return(recordsAffected != 0);
        }
Пример #8
0
        public bool SQLServer_InsertMessage(DatabaseMessage message)
        {
            int recordsAffected = 0;

            {
                SqlConnection connection = new SqlConnection(Settings.DatabaseSettings.ConnectionString);
                SqlCommand    command    = connection.CreateCommand();
                command.CommandType    = CommandType.Text;
                command.CommandText    = SQLServer_InsertMessageScript();
                command.CommandTimeout = 60; // seconds

                command.Parameters.AddWithValue("p1", message.Code);
                command.Parameters.AddWithValue("p2", message.Uuid);
                command.Parameters.AddWithValue("p3", message.DateTimeStamp);
                command.Parameters.AddWithValue("p4", message.Sender);
                command.Parameters.AddWithValue("p5", message.OperationType);
                command.Parameters.AddWithValue("p6", message.MessageType);
                command.Parameters.AddWithValue("p7", message.MessageBody);
                command.Parameters.AddWithValue("p8", message.ErrorCount);
                command.Parameters.AddWithValue("p9", message.ErrorDescription);

                try
                {
                    connection.Open();
                    recordsAffected = command.ExecuteNonQuery();
                }
                catch (Exception error)
                {
                    FileLogger.Log(LOG_TOKEN, ExceptionHelper.GetErrorText(error));
                }
                finally
                {
                    // TODO: replace with using statement and handle exceptions outside the method !?
                    DisposeDatabaseResources(connection, null, command);
                }
            }
            return(recordsAffected != 0);
        }
Пример #9
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.");
            }
        }