Ejemplo n.º 1
0
        public ReceiveResult TryReceiveFrom(TableBasedQueue queue)
        {
            MessageReadResult readResult;

            using (var connection = sqlConnectionFactory.OpenNewConnection(connectionString))
            {
                readResult = queue.TryReceive(connection);
                if (readResult.IsPoison)
                {
                    errorQueue.Send(readResult.DataRecord, connection);
                    return(ReceiveResult.NoMessage());
                }
            }

            if (!readResult.Successful)
            {
                return(ReceiveResult.NoMessage());
            }

            var result = ReceiveResult.Received(readResult.Message);

            try
            {
                tryProcessMessageCallback(readResult.Message);
                return(result);
            }
            catch (Exception ex)
            {
                return(result.FailedProcessing(ex));
            }
        }
        public ReceiveResult TryReceiveFrom(TableBasedQueue queue)
        {
            using (var connection = sqlConnectionFactory.OpenNewConnection(connectionString))
            {
                using (connectionStore.SetConnection(connectionString, connection))
                {
                    using (var transaction = connection.BeginTransaction(isolationLevel))
                    {
                        using (connectionStore.SetTransaction(connectionString, transaction))
                        {
                            MessageReadResult readResult;
                            try
                            {
                                readResult = queue.TryReceive(connection, transaction);
                            }
                            catch (Exception)
                            {
                                transaction.Rollback();
                                throw;
                            }

                            if (readResult.IsPoison)
                            {
                                errorQueue.Send(readResult.DataRecord, connection, transaction);
                                transaction.Commit();
                                return(ReceiveResult.NoMessage());
                            }

                            if (!readResult.Successful)
                            {
                                transaction.Commit();
                                return(ReceiveResult.NoMessage());
                            }

                            var result = ReceiveResult.Received(readResult.Message);
                            try
                            {
                                if (tryProcessMessageCallback(result.Message))
                                {
                                    transaction.Commit();
                                }
                                else
                                {
                                    transaction.Rollback();
                                }
                                return(result);
                            }
                            catch (Exception ex)
                            {
                                transaction.Rollback();
                                return(result.FailedProcessing(ex));
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        void Purge(IEnumerable <string> tableNames)
        {
            using (var connection = sqlConnectionFactory.OpenNewConnection(localConnectionParams.ConnectionString))
            {
                foreach (var tableName in tableNames)
                {
                    using (var command = new SqlCommand(string.Format(SqlPurge, localConnectionParams.Schema, tableName), connection)
                    {
                        CommandType = CommandType.Text
                    })
                    {
                        var numberOfPurgedRows = command.ExecuteNonQuery();

                        Logger.InfoFormat("{0} messages was purged from table {1}", numberOfPurgedRows, tableName);
                    }
                }
            }
        }
        public ReceiveResult TryReceiveFrom(TableBasedQueue queue)
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
            {
                using (var connection = sqlConnectionFactory.OpenNewConnection(connectionString))
                {
                    using (connectionStore.SetConnection(connectionString, connection))
                    {
                        var readResult = queue.TryReceive(connection);
                        if (readResult.IsPoison)
                        {
                            errorQueue.Send(readResult.DataRecord, connection);
                            scope.Complete();
                            return(ReceiveResult.NoMessage());
                        }

                        if (!readResult.Successful)
                        {
                            scope.Complete();
                            return(ReceiveResult.NoMessage());
                        }

                        var result = ReceiveResult.Received(readResult.Message);

                        try
                        {
                            if (tryProcessMessageCallback(readResult.Message))
                            {
                                scope.Complete();
                                scope.Dispose(); // We explicitly calling Dispose so that we force any exception to not bubble, eg Concurrency/Deadlock exception.
                            }
                            return(result);
                        }
                        catch (Exception ex)
                        {
                            return(result.FailedProcessing(ex));
                        }
                    }
                }
            }
        }
        public void Send(TransportMessage message, SendOptions sendOptions)
        {
            Address destination = null;

            try
            {
                destination = DetermineDestination(sendOptions);

                var connectionInfo = connectionStringProvider.GetForDestination(sendOptions.Destination);
                var queue          = new TableBasedQueue(destination, connectionInfo.Schema);
                if (sendOptions.EnlistInReceiveTransaction)
                {
                    SqlTransaction currentTransaction;
                    if (connectionStore.TryGetTransaction(connectionInfo.ConnectionString, out currentTransaction))
                    {
                        queue.Send(message, sendOptions, currentTransaction.Connection, currentTransaction);
                    }
                    else
                    {
                        SqlConnection currentConnection;
                        if (connectionStore.TryGetConnection(connectionInfo.ConnectionString, out currentConnection))
                        {
                            queue.Send(message, sendOptions, currentConnection);
                        }
                        else
                        {
                            using (var connection = sqlConnectionFactory.OpenNewConnection(connectionInfo.ConnectionString))
                            {
                                queue.Send(message, sendOptions, connection);
                            }
                        }
                    }
                }
                else
                {
                    // Suppress so that even if DTC is on, we won't escalate
                    using (var tx = new TransactionScope(TransactionScopeOption.Suppress))
                    {
                        using (var connection = sqlConnectionFactory.OpenNewConnection(connectionInfo.ConnectionString))
                        {
                            queue.Send(message, sendOptions, connection);
                        }

                        tx.Complete();
                    }
                }
            }
            catch (SqlException ex)
            {
                if (ex.Number == 208)
                {
                    ThrowQueueNotFoundException(destination, ex);
                }

                ThrowFailedToSendException(destination, ex);
            }
            catch (Exception ex)
            {
                ThrowFailedToSendException(destination, ex);
            }
        }