public IEnumerable <string> GetSubscribedUris(object message)
        {
            Guard.AgainstNull(message, "message");

            var messageType = message.GetType().FullName;

            if (!subscribers.ContainsKey(messageType))
            {
                lock (padlock)
                {
                    if (!subscribers.ContainsKey(messageType))
                    {
                        DataTable table;

                        using (databaseConnectionFactory.Create(SubscriptionDataSource))
                        {
                            table = databaseGateway.GetDataTableFor(
                                SubscriptionDataSource,
                                RawQuery.CreateFrom(
                                    scriptProvider.GetScript(
                                        Script.SubscriptionManagerInboxWorkQueueUris))
                                .AddParameterValue(SubscriptionManagerColumns.MessageType, messageType));
                        }

                        subscribers.Add(messageType, (from DataRow row in table.Rows
                                                      select SubscriptionManagerColumns.InboxWorkQueueUri.MapFrom(row))
                                        .ToList());
                    }
                }
            }

            return(subscribers[messageType]);
        }
Exemple #2
0
        public IEnumerable <Stream> Read(int top)
        {
            try
            {
                var result = new List <Stream>();

                using (databaseConnectionFactory.Create(dataSource))
                {
                    using (var reader = databaseGateway.GetReaderUsing(
                               dataSource,
                               RawQuery.CreateFrom(scriptProvider.GetScript(Script.QueueRead,
                                                                            top.ToString(),
                                                                            tableName))))
                    {
                        while (reader.Read())
                        {
                            result.Add(new MemoryStream((byte[])reader["MessageBody"]));
                        }
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                log.Debug(string.Format(
                              "Could not read top {0} messages from queue '{1}'.  Exception: {2}.  Query: {3}",
                              top, Uri, ex.Message, createQuery));

                throw;
            }
        }
Exemple #3
0
 public void Remove(TransportMessage transportMessage)
 {
     using (databaseConnectionFactory.Create(IdempotenceDataSource))
     {
         databaseGateway.ExecuteUsing(
             IdempotenceDataSource,
             RawQuery.CreateFrom(
                 scriptProvider.GetScript(Script.IdempotenceTrackerRemove))
             .AddParameterValue(IdempotenceTrackerColumns.MessageId, transportMessage.MessageId));
     }
 }
Exemple #4
0
        public SqlQueue(Uri uri,
                        IScriptProvider scriptProvider,
                        IDatabaseConnectionFactory databaseConnectionFactory,
                        IDatabaseGateway databaseGateway)
        {
            Guard.AgainstNull(uri, "uri");
            Guard.AgainstNull(scriptProvider, "scriptProvider");
            Guard.AgainstNull(databaseConnectionFactory, "databaseConnectionFactory");
            Guard.AgainstNull(databaseGateway, "databaseGateway");

            this.scriptProvider            = scriptProvider;
            this.databaseConnectionFactory = databaseConnectionFactory;
            this.databaseGateway           = databaseGateway;

            if (!uri.Scheme.Equals(SCHEME, StringComparison.InvariantCultureIgnoreCase))
            {
                throw new InvalidSchemeException(SCHEME, uri.ToString());
            }

            var builder = new UriBuilder(uri);

            if (uri.LocalPath == "/" || uri.Segments.Length != 2)
            {
                throw new UriFormatException(string.Format(ESBResources.UriFormatException,
                                                           "sql://{{connection-name}}/{{table-name}}",
                                                           uri));
            }

            log = Log.For(this);

            Uri = builder.Uri;

            dataSource = new DataSource(Uri.Host,
                                        new SqlServerDbDataParameterFactory(),
                                        new SqlServerContainsQueryFactory(),
                                        new SqlServerInsertQueryFactory(),
                                        new SqlServerUpdateQueryFactory(),
                                        new SqlServerDeleteQueryFactory(),
                                        new SqlServerSelectQueryFactory());

            using (databaseConnectionFactory.Create(dataSource))
            {
                var host = databaseGateway.GetScalarUsing <string>(dataSource,
                                                                   RawQuery.CreateFrom("select host_name()"));

                IsLocal = (host ?? string.Empty) == Environment.MachineName;
            }

            tableName = Uri.Segments[1];

            IsTransactional = true;

            BuildQueries();
        }
Exemple #5
0
 public bool Contains(TransportMessage transportMessage)
 {
     using (databaseConnectionFactory.Create(IdempotenceDataSource))
     {
         return(databaseGateway.GetScalarUsing <int>(
                    IdempotenceDataSource,
                    RawQuery.CreateFrom(
                        scriptProvider.GetScript(
                            Script.IdempotenceTrackerContains))
                    .AddParameterValue(IdempotenceTrackerColumns.MessageId, transportMessage.MessageId)) == 1);
     }
 }
Exemple #6
0
 private void BuildQueries()
 {
     existsQuery             = RawQuery.CreateFrom(scriptProvider.GetScript(Script.QueueExists, tableName));
     createQuery             = RawQuery.CreateFrom(scriptProvider.GetScript(Script.QueueCreate, tableName));
     dropQuery               = RawQuery.CreateFrom(scriptProvider.GetScript(Script.QueueDrop, tableName));
     purgeQuery              = RawQuery.CreateFrom(scriptProvider.GetScript(Script.QueuePurge, tableName));
     dequeueQuery            = RawQuery.CreateFrom(scriptProvider.GetScript(Script.QueueDequeue, tableName));
     countQuery              = RawQuery.CreateFrom(scriptProvider.GetScript(Script.QueueCount, tableName));
     enqueueQueryStatement   = scriptProvider.GetScript(Script.QueueEnqueue, tableName);
     removeQueryStatement    = scriptProvider.GetScript(Script.QueueRemove, tableName);
     dequeueIdQueryStatement = scriptProvider.GetScript(Script.QueueDequeueId, tableName);
 }
Exemple #7
0
 public void Initialize(IServiceBus bus)
 {
     using (databaseConnectionFactory.Create(IdempotenceDataSource))
     {
         if (databaseGateway.GetScalarUsing <int>(
                 IdempotenceDataSource,
                 RawQuery.CreateFrom(
                     scriptProvider.GetScript(
                         Script.IdempotenceTrackerExists))) != 1)
         {
             throw new IdempotenceTrackerException(SqlResources.IdempotenceTrackerDatabaseNotConfigured);
         }
     }
 }
        public void Initialize(IServiceBus bus)
        {
            serviceBusConfiguration = bus.Configuration;

            using (databaseConnectionFactory.Create(SubscriptionDataSource))
            {
                if (databaseGateway.GetScalarUsing <int>(
                        SubscriptionDataSource,
                        RawQuery.CreateFrom(
                            scriptProvider.GetScript(
                                Script.SubscriptionManagerExists))) != 1)
                {
                    throw new SubscriptionManagerException(SqlResources.SubscriptionManagerDatabaseNotConfigured);
                }
            }

            if (HasDeferredSubscriptions)
            {
                Subscribe(deferredSubscriptions);
            }
        }
Exemple #9
0
        public bool Remove(Guid messageId)
        {
            ResetUnderlyingMessageData();

            try
            {
                using (databaseConnectionFactory.Create(dataSource))
                {
                    return(databaseGateway.ExecuteUsing(
                               dataSource,
                               RawQuery.CreateFrom(removeQueryStatement)
                               .AddParameterValue(QueueColumns.MessageId, messageId)) > 0);
                }
            }
            catch (Exception ex)
            {
                log.Debug(string.Format("Could not remove message from queue '{0}'.  Exception: {1}.  Query: {2}",
                                        Uri, ex.Message, createQuery));

                throw;
            }
        }
Exemple #10
0
        public Stream Dequeue(Guid messageId)
        {
            ResetUnderlyingMessageData();

            lock (padlock)
            {
                try
                {
                    using (databaseConnectionFactory.Create(dataSource))
                    {
                        var row = databaseGateway.GetSingleRowUsing(
                            dataSource,
                            RawQuery.CreateFrom(dequeueIdQueryStatement).AddParameterValue(QueueColumns.MessageId,
                                                                                           messageId));

                        if (row == null)
                        {
                            return(null);
                        }

                        underlyingMessageData = row;

                        using (var stream = new MemoryStream((byte[])row["MessageBody"]))
                        {
                            underlyingMessageData = new MemoryStream(stream.ToBytes());

                            return((Stream)underlyingMessageData);
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Debug(string.Format("Could not dequeue message from queue '{0}'.  Exception: {1}.  Query: {2}",
                                            Uri, ex.Message, createQuery));

                    throw;
                }
            }
        }
Exemple #11
0
        public void Enqueue(Guid messageId, Stream stream)
        {
            try
            {
                using (databaseConnectionFactory.Create(dataSource))
                {
                    databaseGateway.ExecuteUsing(
                        dataSource,
                        RawQuery.CreateFrom(enqueueQueryStatement)
                        .AddParameterValue(QueueColumns.MessageId, messageId)
                        .AddParameterValue(QueueColumns.MessageBody, stream.ToBytes()));
                }
            }
            catch (Exception ex)
            {
                log.Debug(
                    string.Format(
                        "Could not enqueue message id '{0}' on queue '{1}'.  Exception: {2}", messageId, Uri, ex.Message));

                throw;
            }
        }
        public void Subscribe(IEnumerable <string> messageTypes)
        {
            if (!Started)
            {
                deferredSubscriptions.AddRange(messageTypes);

                return;
            }

            using (databaseConnectionFactory.Create(SubscriptionDataSource))
            {
                foreach (var messageType in messageTypes)
                {
                    databaseGateway.ExecuteUsing(
                        SubscriptionDataSource,
                        RawQuery.CreateFrom(
                            scriptProvider.GetScript(Script.SubscriptionManagerSubscribe))
                        .AddParameterValue(SubscriptionManagerColumns.InboxWorkQueueUri,
                                           serviceBusConfiguration.Inbox.WorkQueue.Uri.ToString())
                        .AddParameterValue(SubscriptionManagerColumns.MessageType, messageType));
                }
            }
        }
Exemple #13
0
        public void Enqueue(object data)
        {
            Guard.AgainstNull(data, "data");

            var row = data as DataRow;

            if (row == null)
            {
                throw new EnqueueMessageDataTypeMismatchException(data.GetType().FullName,
                                                                  Uri.ToString(),
                                                                  typeof(DataRow).FullName);
            }

            var messageId = QueueColumns.MessageId.MapFrom(row);

            try
            {
                using (databaseConnectionFactory.Create(dataSource))
                {
                    databaseGateway.ExecuteUsing(
                        dataSource,
                        RawQuery.CreateFrom(enqueueQueryStatement)
                        .AddParameterValue(QueueColumns.MessageId, messageId)
                        .AddParameterValue(QueueColumns.MessageBody, QueueColumns.MessageBody.MapFrom(row)));
                }
            }
            catch (Exception ex)
            {
                log.Debug(
                    string.Format(
                        "Could not enqueue message data with message id '{0}' on queue '{1}'.  Exception: {2}",
                        messageId, Uri, ex.Message));

                throw;
            }
        }