/// <summary>
        /// Initializes a new instance of the <see cref="ReceiveMessageQueryHandler" /> class.
        /// </summary>
        /// <param name="optionsFactory">The options factory.</param>
        /// <param name="tableNameHelper">The table name helper.</param>
        /// <param name="receivedMessageFactory">The received message factory.</param>
        /// <param name="commandCache">The command cache.</param>
        /// <param name="messageFactory">The message factory.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="serialization">The serialization.</param>
        /// <param name="getTimeFactory">The get time factory.</param>
        public ReceiveMessageQueryHandler(IPostgreSqlMessageQueueTransportOptionsFactory optionsFactory,
                                          ITableNameHelper tableNameHelper,
                                          IReceivedMessageFactory receivedMessageFactory,
                                          PostgreSqlCommandStringCache commandCache,
                                          IMessageFactory messageFactory,
                                          IHeaders headers,
                                          ICompositeSerialization serialization,
                                          IGetTimeFactory getTimeFactory)
        {
            Guard.NotNull(() => optionsFactory, optionsFactory);
            Guard.NotNull(() => tableNameHelper, tableNameHelper);
            Guard.NotNull(() => receivedMessageFactory, receivedMessageFactory);
            Guard.NotNull(() => commandCache, commandCache);
            Guard.NotNull(() => messageFactory, messageFactory);
            Guard.NotNull(() => serialization, serialization);
            Guard.NotNull(() => headers, headers);
            Guard.NotNull(() => getTimeFactory, getTimeFactory);

            _options                = new Lazy <PostgreSqlMessageQueueTransportOptions>(optionsFactory.Create);
            _tableNameHelper        = tableNameHelper;
            _receivedMessageFactory = receivedMessageFactory;
            _commandCache           = commandCache;
            _messageFactory         = messageFactory;
            _headers                = headers;
            _serialization          = serialization;
            _getTime                = getTimeFactory.Create();
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SendMessageCommandHandler" /> class.
        /// </summary>
        /// <param name="tableNameHelper">The table name helper.</param>
        /// <param name="serializer">The serializer.</param>
        /// <param name="optionsFactory">The options factory.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="commandCache">The command cache.</param>
        /// <param name="configurationSend">The configuration send.</param>
        /// <param name="sendJobStatus">The send job status.</param>
        /// <param name="jobExistsHandler">The job exists handler.</param>
        /// <param name="jobSchedulerMetaData">The job scheduler meta data.</param>
        /// <param name="getTimeFactory">The get time factory.</param>
        public SendMessageCommandHandlerAsync(ITableNameHelper tableNameHelper,
                                              ICompositeSerialization serializer,
                                              IPostgreSqlMessageQueueTransportOptionsFactory optionsFactory,
                                              IHeaders headers,
                                              PostgreSqlCommandStringCache commandCache,
                                              TransportConfigurationSend configurationSend,
                                              ICommandHandler <SetJobLastKnownEventCommand <NpgsqlConnection, NpgsqlTransaction> > sendJobStatus, IQueryHandler <DoesJobExistQuery <NpgsqlConnection, NpgsqlTransaction>, QueueStatuses> jobExistsHandler,
                                              IJobSchedulerMetaData jobSchedulerMetaData,
                                              IGetTimeFactory getTimeFactory)
        {
            Guard.NotNull(() => tableNameHelper, tableNameHelper);
            Guard.NotNull(() => serializer, serializer);
            Guard.NotNull(() => optionsFactory, optionsFactory);
            Guard.NotNull(() => headers, headers);
            Guard.NotNull(() => commandCache, commandCache);
            Guard.NotNull(() => configurationSend, configurationSend);
            Guard.NotNull(() => sendJobStatus, sendJobStatus);
            Guard.NotNull(() => jobExistsHandler, jobExistsHandler);
            Guard.NotNull(() => jobSchedulerMetaData, jobSchedulerMetaData);
            Guard.NotNull(() => getTimeFactory, getTimeFactory);

            _tableNameHelper      = tableNameHelper;
            _serializer           = serializer;
            _options              = new Lazy <PostgreSqlMessageQueueTransportOptions>(optionsFactory.Create);
            _headers              = headers;
            _commandCache         = commandCache;
            _configurationSend    = configurationSend;
            _sendJobStatus        = sendJobStatus;
            _jobExistsHandler     = jobExistsHandler;
            _jobSchedulerMetaData = jobSchedulerMetaData;
            _getTime              = getTimeFactory.Create();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SetJobLastKnownEventCommandHandler" /> class.
        /// </summary>
        /// <param name="commandCache">The command cache.</param>
        /// <param name="connectionInformation">The connection information.</param>
        public SetJobLastKnownEventCommandHandler(PostgreSqlCommandStringCache commandCache,
                                                  IConnectionInformation connectionInformation)
        {
            Guard.NotNull(() => commandCache, commandCache);
            Guard.NotNull(() => connectionInformation, connectionInformation);

            _commandCache          = commandCache;
            _connectionInformation = connectionInformation;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="RollbackMessageCommandHandler" /> class.
 /// </summary>
 /// <param name="getUtcDateQuery">The get UTC date query.</param>
 /// <param name="options">The options.</param>
 /// <param name="tableNameHelper">The table name helper.</param>
 /// <param name="connectionInformation">The connection information.</param>
 /// <param name="commandCache">The command cache.</param>
 public RollbackMessageCommandHandler(IGetTimeFactory getUtcDateQuery,
                                      IPostgreSqlMessageQueueTransportOptionsFactory options,
                                      TableNameHelper tableNameHelper,
                                      IConnectionInformation connectionInformation,
                                      PostgreSqlCommandStringCache commandCache)
 {
     _getUtcDateQuery       = getUtcDateQuery;
     _options               = new Lazy <PostgreSqlMessageQueueTransportOptions>(options.Create);
     _tableNameHelper       = tableNameHelper;
     _connectionInformation = connectionInformation;
     _commandCache          = commandCache;
     _rollbackDictionary    = new ConcurrentDictionary <string, string>();
 }
Пример #5
0
        /// <summary>
        /// Gets the de queue command.
        /// </summary>
        /// <param name="commandCache">The command cache.</param>
        /// <param name="tableNameHelper">The table name helper.</param>
        /// <param name="options">The options.</param>
        /// <param name="forRpc">if set to <c>true</c> [for RPC].</param>
        /// <param name="routes">The routes.</param>
        /// <returns></returns>
        public static string GetDeQueueCommand(PostgreSqlCommandStringCache commandCache, TableNameHelper tableNameHelper, PostgreSqlMessageQueueTransportOptions options, bool forRpc, List<string> routes )
        {
            if (routes == null || routes.Count == 0)
            {
                if (forRpc && commandCache.Contains(RpcdequeueKey))
                {
                    return commandCache.Get(RpcdequeueKey);
                }
                if (commandCache.Contains(DequeueKey))
                {
                    return commandCache.Get(DequeueKey);
                }
            }

            var sb = new StringBuilder();
            var needWhere = true;
            if (options.EnableStatus)
            {
                sb.AppendLine($"update {tableNameHelper.MetaDataName} q");
                sb.AppendLine($"set status = {Convert.ToInt16(QueueStatuses.Processing)}");
                if (options.EnableHeartBeat)
                {
                    sb.AppendLine(", HeartBeat = @CurrentDate");
                }
                sb.AppendLine($"from {tableNameHelper.QueueName} qm");
            }
            else
            {
                sb.AppendLine($"delete from {tableNameHelper.MetaDataName} q ");
                sb.AppendLine($"using {tableNameHelper.QueueName} qm ");
            }

            sb.AppendLine(" where q.QueueID in (");
            sb.AppendLine($"select q.QueueID from {tableNameHelper.MetaDataName} q");

            //calculate where clause...
            if (options.EnableStatus && options.EnableDelayedProcessing)
            {
                sb.AppendFormat(" WHERE q.Status = {0} ", Convert.ToInt16(QueueStatuses.Waiting));
                sb.AppendLine("and q.QueueProcessTime < @CurrentDate ");
                needWhere = false;
            }
            else if (options.EnableStatus)
            {
                sb.AppendFormat("WHERE q.Status = {0} ", Convert.ToInt16(QueueStatuses.Waiting));
                needWhere = false;
            }
            else if (options.EnableDelayedProcessing)
            {
                sb.AppendLine("WHERE (q.QueueProcessTime < @CurrentDate) ");
                needWhere = false;
            }

            if (forRpc)
            {
                if (needWhere)
                {
                    sb.AppendLine("where q.SourceQueueID = @QueueID");
                    needWhere = false;
                }
                else
                {
                    sb.AppendLine("AND q.SourceQueueID = @QueueID");
                }
            }

            if (options.EnableMessageExpiration || options.QueueType == QueueTypes.RpcReceive || options.QueueType == QueueTypes.RpcSend)
            {
                if (needWhere)
                {
                    sb.AppendLine("Where q.ExpirationTime > @CurrentDate ");
                    needWhere = false;
                }
                else
                {
                    sb.AppendLine("AND q.ExpirationTime > @CurrentDate ");
                }
                needWhere = false;
            }

            if (options.EnableRoute && routes != null && routes.Count > 0)
            {
                if (needWhere)
                {
                    sb.AppendLine("where Route IN ( ");
                    needWhere = false;
                }
                else
                {
                    sb.AppendLine("AND Route IN ( ");
                }

                var routeCounter = 1;
                foreach (var route in routes)
                {
                    sb.Append("@Route" + routeCounter.ToString());
                    routeCounter++;
                    if (routeCounter != routes.Count + 1)
                    {
                        sb.Append(", ");
                    }
                }

                sb.Append(") ");
            }

            //determine order by looking at the options
            var bNeedComma = false;
            sb.Append(" Order by ");
            if (options.EnableStatus)
            {
                sb.Append(" q.status asc ");
                bNeedComma = true;
            }
            if (options.EnablePriority)
            {
                if (bNeedComma)
                {
                    sb.Append(", ");
                }
                sb.Append(" q.priority asc ");
                bNeedComma = true;
            }
            if (options.EnableDelayedProcessing)
            {
                if (bNeedComma)
                {
                    sb.Append(", ");
                }
                sb.AppendLine(" q.QueueProcessTime asc ");
                bNeedComma = true;
            }
            if (options.EnableMessageExpiration)
            {
                if (bNeedComma)
                {
                    sb.Append(", ");
                }
                sb.AppendLine(" q.ExpirationTime asc ");
                bNeedComma = true;
            }

            if (bNeedComma)
            {
                sb.Append(", ");
            }
            sb.AppendLine(" q.QueueID asc limit 1 FOR UPDATE SKIP LOCKED) ");
            sb.AppendLine(" AND q.QueueID = qm.QueueID");
            sb.AppendLine("returning q.queueid, qm.body, qm.Headers, q.CorrelationID");

            if (routes != null && routes.Count > 0)
            { //TODO - cache based on route
                return sb.ToString();
            }
            else
            {
                return commandCache.Add(forRpc ? RpcdequeueKey : DequeueKey, sb.ToString());
            }
        }
Пример #6
0
        /// <summary>
        /// Gets the de queue command.
        /// </summary>
        /// <param name="commandCache">The command cache.</param>
        /// <param name="tableNameHelper">The table name helper.</param>
        /// <param name="options">The options.</param>
        /// <param name="forRpc">if set to <c>true</c> [for RPC].</param>
        /// <param name="routes">The routes.</param>
        /// <returns></returns>
        public static string GetDeQueueCommand(PostgreSqlCommandStringCache commandCache, TableNameHelper tableNameHelper, PostgreSqlMessageQueueTransportOptions options, bool forRpc, List <string> routes)
        {
            if (routes == null || routes.Count == 0)
            {
                if (forRpc && commandCache.Contains(RpcdequeueKey))
                {
                    return(commandCache.Get(RpcdequeueKey).CommandText);
                }
                if (commandCache.Contains(DequeueKey))
                {
                    return(commandCache.Get(DequeueKey).CommandText);
                }
            }

            var sb        = new StringBuilder();
            var needWhere = true;

            if (options.EnableStatus)
            {
                sb.AppendLine($"update {tableNameHelper.MetaDataName} q");
                sb.AppendLine($"set status = {Convert.ToInt16(QueueStatuses.Processing)}");
                if (options.EnableHeartBeat)
                {
                    sb.AppendLine(", HeartBeat = @CurrentDate");
                }
                sb.AppendLine($"from {tableNameHelper.QueueName} qm");
            }
            else
            {
                sb.AppendLine($"delete from {tableNameHelper.MetaDataName} q ");
                sb.AppendLine($"using {tableNameHelper.QueueName} qm ");
            }

            sb.AppendLine(" where q.QueueID in (");
            sb.AppendLine($"select q.QueueID from {tableNameHelper.MetaDataName} q");

            //calculate where clause...
            if (options.EnableStatus && options.EnableDelayedProcessing)
            {
                sb.AppendFormat(" WHERE q.Status = {0} ", Convert.ToInt16(QueueStatuses.Waiting));
                sb.AppendLine("and q.QueueProcessTime < @CurrentDate ");
                needWhere = false;
            }
            else if (options.EnableStatus)
            {
                sb.AppendFormat("WHERE q.Status = {0} ", Convert.ToInt16(QueueStatuses.Waiting));
                needWhere = false;
            }
            else if (options.EnableDelayedProcessing)
            {
                sb.AppendLine("WHERE (q.QueueProcessTime < @CurrentDate) ");
                needWhere = false;
            }

            if (forRpc)
            {
                if (needWhere)
                {
                    sb.AppendLine("where q.SourceQueueID = @QueueID");
                    needWhere = false;
                }
                else
                {
                    sb.AppendLine("AND q.SourceQueueID = @QueueID");
                }
            }

            if (options.EnableMessageExpiration || options.QueueType == QueueTypes.RpcReceive || options.QueueType == QueueTypes.RpcSend)
            {
                if (needWhere)
                {
                    sb.AppendLine("Where q.ExpirationTime > @CurrentDate ");
                    needWhere = false;
                }
                else
                {
                    sb.AppendLine("AND q.ExpirationTime > @CurrentDate ");
                }
            }

            if (options.EnableRoute && routes != null && routes.Count > 0)
            {
                sb.AppendLine(needWhere ? "where Route IN ( " : "AND Route IN ( ");

                for (var i = 1; i - 1 < routes.Count; i++)
                {
                    sb.Append("@Route" + i);
                    if (i != routes.Count)
                    {
                        sb.Append(", ");
                    }
                }
                sb.Append(") ");
            }

            //determine order by looking at the options
            var bNeedComma = false;

            sb.Append(" Order by ");
            if (options.EnableStatus)
            {
                sb.Append(" q.status asc ");
                bNeedComma = true;
            }
            if (options.EnablePriority)
            {
                if (bNeedComma)
                {
                    sb.Append(", ");
                }
                sb.Append(" q.priority asc ");
                bNeedComma = true;
            }
            if (options.EnableDelayedProcessing)
            {
                if (bNeedComma)
                {
                    sb.Append(", ");
                }
                sb.AppendLine(" q.QueueProcessTime asc ");
                bNeedComma = true;
            }
            if (options.EnableMessageExpiration)
            {
                if (bNeedComma)
                {
                    sb.Append(", ");
                }
                sb.AppendLine(" q.ExpirationTime asc ");
                bNeedComma = true;
            }

            if (bNeedComma)
            {
                sb.Append(", ");
            }
            sb.AppendLine(" q.QueueID asc limit 1 FOR UPDATE SKIP LOCKED) ");
            sb.AppendLine(" AND q.QueueID = qm.QueueID");
            sb.AppendLine("returning q.queueid, qm.body, qm.Headers, q.CorrelationID");

            if (routes != null && routes.Count > 0)
            { //TODO - cache based on route
                return(sb.ToString());
            }
            return(commandCache.Add(forRpc ? RpcdequeueKey : DequeueKey, sb.ToString()));
        }
Пример #7
0
        /// <summary>Gets the de queue command.</summary>
        /// <param name="commandCache">The command cache.</param>
        /// <param name="tableNameHelper">The table name helper.</param>
        /// <param name="options">The options.</param>
        /// <param name="configuration">Queue Configuration</param>
        /// <param name="routes">The routes.</param>
        /// <param name="userParams">An optional collection of user params to pass to the query</param>
        /// <returns>
        ///   <br />
        /// </returns>
        public static string GetDeQueueCommand(PostgreSqlCommandStringCache commandCache, ITableNameHelper tableNameHelper, PostgreSqlMessageQueueTransportOptions options, QueueConsumerConfiguration configuration, List <string> routes, out List <Npgsql.NpgsqlParameter> userParams)
        {
            userParams = null;
            var userQuery = configuration.GetUserClause();

            if ((routes == null || routes.Count == 0) && string.IsNullOrEmpty(userQuery))
            {
                if (commandCache.Contains(DequeueKey))
                {
                    return(commandCache.Get(DequeueKey).CommandText);
                }
            }

            var sb        = new StringBuilder();
            var needWhere = true;

            if (options.EnableStatus)
            {
                sb.AppendLine($"update {tableNameHelper.MetaDataName} q");
                sb.AppendLine($"set status = {Convert.ToInt16(QueueStatuses.Processing)}");
                if (options.EnableHeartBeat)
                {
                    sb.AppendLine(", HeartBeat = @CurrentDate");
                }
                sb.AppendLine($"from {tableNameHelper.QueueName} qm");
            }
            else
            {
                sb.AppendLine($"delete from {tableNameHelper.MetaDataName} q ");
                sb.AppendLine($"using {tableNameHelper.QueueName} qm ");
            }

            sb.AppendLine(" where q.QueueID in (");
            sb.AppendLine($"select q.QueueID from {tableNameHelper.MetaDataName} q");

            //calculate where clause...
            if (options.EnableStatus && options.EnableDelayedProcessing)
            {
                sb.AppendFormat(" WHERE q.Status = {0} ", Convert.ToInt16(QueueStatuses.Waiting));
                sb.AppendLine("and q.QueueProcessTime < @CurrentDate ");
                needWhere = false;
            }
            else if (options.EnableStatus)
            {
                sb.AppendFormat("WHERE q.Status = {0} ", Convert.ToInt16(QueueStatuses.Waiting));
                needWhere = false;
            }
            else if (options.EnableDelayedProcessing)
            {
                sb.AppendLine("WHERE (q.QueueProcessTime < @CurrentDate) ");
                needWhere = false;
            }

            if (options.EnableMessageExpiration)
            {
                if (needWhere)
                {
                    sb.AppendLine("Where q.ExpirationTime > @CurrentDate ");
                    needWhere = false;
                }
                else
                {
                    sb.AppendLine("AND q.ExpirationTime > @CurrentDate ");
                }
            }

            if (options.EnableRoute && routes != null && routes.Count > 0)
            {
                sb.AppendLine(needWhere ? "where Route IN ( " : "AND Route IN ( ");

                for (var i = 1; i - 1 < routes.Count; i++)
                {
                    sb.Append("@Route" + i);
                    if (i != routes.Count)
                    {
                        sb.Append(", ");
                    }
                }
                sb.Append(") ");
            }

            //if true, the query can be added to via user settings
            if (options.AdditionalColumnsOnMetaData && !string.IsNullOrEmpty(userQuery))
            {
                userParams = configuration.GetUserParameters(); //NOTE - could be null
                sb.AppendLine(needWhere
                    ? $"where {userQuery} "
                    : $"AND {userQuery} ");
            }

            //determine order by looking at the options
            var bNeedComma = false;

            sb.Append(" Order by ");
            if (options.EnableStatus)
            {
                sb.Append(" q.status asc ");
                bNeedComma = true;
            }
            if (options.EnablePriority)
            {
                if (bNeedComma)
                {
                    sb.Append(", ");
                }
                sb.Append(" q.priority asc ");
                bNeedComma = true;
            }
            if (options.EnableDelayedProcessing)
            {
                if (bNeedComma)
                {
                    sb.Append(", ");
                }
                sb.AppendLine(" q.QueueProcessTime asc ");
                bNeedComma = true;
            }
            if (options.EnableMessageExpiration)
            {
                if (bNeedComma)
                {
                    sb.Append(", ");
                }
                sb.AppendLine(" q.ExpirationTime asc ");
                bNeedComma = true;
            }

            if (bNeedComma)
            {
                sb.Append(", ");
            }
            sb.AppendLine(" q.QueueID asc limit 1 FOR UPDATE SKIP LOCKED) ");
            sb.AppendLine(" AND q.QueueID = qm.QueueID");
            sb.AppendLine("returning q.queueid, qm.body, qm.Headers, q.CorrelationID");

            if ((routes != null && routes.Count > 0) || !string.IsNullOrEmpty(userQuery))
            { //TODO - cache based on route
                return(sb.ToString());
            }
            return(commandCache.Add(DequeueKey, sb.ToString()));
        }