Ejemplo n.º 1
0
        /// <summary>
        ///     Adds the specified identifier.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="command">The command.</param>
        /// <param name="contextKey">An identifier for the context in which the command has been processed (for example, the name of the handler)</param>
        /// <param name="timeoutInMilliseconds">Timeout in milliseconds; -1 for default timeout</param>
        /// <returns>Task.</returns>
        public void Add <T>(T command, string contextKey, int timeoutInMilliseconds = -1) where T : class, IRequest
        {
            var parameters = InitAddDbParameters(command, contextKey);

            using (var connection = _connectionProvider.GetConnection())
            {
                connection.Open();
                var sqlcmd = InitAddDbCommand(connection, parameters, timeoutInMilliseconds);
                try
                {
                    sqlcmd.ExecuteNonQuery();
                }
                catch (SqlException sqlException)
                {
                    if (sqlException.Number == MsSqlDuplicateKeyError_UniqueIndexViolation || sqlException.Number == MsSqlDuplicateKeyError_UniqueConstraintViolation)
                    {
                        s_logger.LogWarning(
                            "MsSqlOutbox: A duplicate Command with the CommandId {Id} was inserted into the Outbox, ignoring and continuing",
                            command.Id);
                        return;
                    }

                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        private void CreateQueueTable()
        {
            _tableName           = $"queue_{_tableName}";
            using var connection = _connectionProvider.GetConnection();
            var createTableSql = string.Format(_queueDDL, _tableName, Guid.NewGuid().ToString());

            connection.Open();
            using (var command = connection.CreateCommand())
            {
                command.CommandText = createTableSql;
                command.ExecuteNonQuery();
            }
            connection.Close();
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Add the passed message to the Queue
        /// </summary>
        /// <param name="message">The Message</param>
        /// <param name="topic">The topic name</param>
        /// <param name="timeoutInMilliseconds">Timeout in milliseconds; -1 for default timeout</param>
        public void Send(T message, string topic, int timeoutInMilliseconds = -1)
        {
            if (s_logger.IsEnabled(LogLevel.Debug))
            {
                s_logger.LogDebug("Send<{CommandType}>(..., {Topic})", typeof(T).FullName, topic);
            }

            var parameters = InitAddDbParameters(topic, message);

            using (var connection = _connectionProvider.GetConnection())
            {
                connection.Open();
                var sqlCmd = InitAddDbCommand(timeoutInMilliseconds, connection, parameters);
                sqlCmd.ExecuteNonQuery();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Retrieves messages that have been sent within the window
        /// </summary>
        /// <param name="millisecondsDispatchedSince">How long ago would the message have been dispatched in milliseconds</param>
        /// <param name="pageSize">How many messages in a page</param>
        /// <param name="pageNumber">Which page of messages to get</param>
        /// <param name="outboxTimeout"></param>
        /// <param name="args">Additional parameters required for search, if any</param>
        /// <returns>A list of dispatched messages</returns>
        public IEnumerable <Message> DispatchedMessages(
            double millisecondsDispatchedSince,
            int pageSize      = 100,
            int pageNumber    = 1,
            int outboxTimeout = -1,
            Dictionary <string, object> args = null)
        {
            var connection = _connectionProvider.GetConnection();

            using (var command = connection.CreateCommand())
            {
                CreatePagedDispatchedCommand(command, millisecondsDispatchedSince, pageSize, pageNumber);

                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }

                var dbDataReader = command.ExecuteReader();

                var messages = new List <Message>();
                while (dbDataReader.Read())
                {
                    messages.Add(MapAMessage(dbDataReader));
                }
                dbDataReader.Close();

                if (!_connectionProvider.IsSharedConnection)
                {
                    connection.Dispose();
                }
                else if (!_connectionProvider.HasOpenTransaction)
                {
                    connection.Close();
                }

                return(messages);
            }
        }
Ejemplo n.º 5
0
 public void CreateDatabase()
 {
     using (var connection = _masterConnectionProvider.GetConnection())
     {
         connection.Open();
         using (var command = connection.CreateCommand())
         {
             command.CommandText = @"
                                 IF DB_ID('BrighterTests') IS NULL
                                 BEGIN
                                     CREATE DATABASE BrighterTests;
                                 END;";
             command.ExecuteNonQuery();
         }
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        ///     Adds the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="outBoxTimeout"></param>
        /// <returns>Task.</returns>
        public void Add(Message message, int outBoxTimeout = -1)
        {
            var parameters = InitAddDbParameters(message);

            var connection = _connectionProvider.GetConnection();

            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }
            using (var command = InitAddDbCommand(connection, parameters))
            {
                try
                {
                    if (_connectionProvider.HasOpenTransaction)
                    {
                        command.Transaction = _connectionProvider.GetTransaction();
                    }
                    command.ExecuteNonQuery();
                }
                catch (SqlException sqlException)
                {
                    if (sqlException.Number == MsSqlDuplicateKeyError_UniqueIndexViolation ||
                        sqlException.Number == MsSqlDuplicateKeyError_UniqueConstraintViolation)
                    {
                        s_logger.LogWarning(
                            "MsSqlOutbox: A duplicate Message with the MessageId {Id} was inserted into the Outbox, ignoring and continuing",
                            message.Id);
                        return;
                    }

                    throw;
                }
                finally
                {
                    if (!_connectionProvider.IsSharedConnection)
                    {
                        connection.Dispose();
                    }
                    else if (!_connectionProvider.HasOpenTransaction)
                    {
                        connection.Close();
                    }
                }
            }
        }