Esempio 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));
            }
        }
Esempio n. 2
0
        ReceiveResult TryReceiveWithDTCTransaction()
        {
            var result = new ReceiveResult();

            using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
            {
                var message = Receive();

                if (message == null)
                {
                    scope.Complete();
                    return(result);
                }

                result.Message = message;

                try
                {
                    if (tryProcessMessage(message))
                    {
                        scope.Complete();
                        scope.Dispose(); // We explicitly calling Dispose so that we force any exception to not bubble, eg Concurrency/Deadlock exception.
                    }
                }
                catch (Exception ex)
                {
                    result.Exception = ex;
                }

                return(result);
            }
        }
        public ReceiveResult TryReceiveFrom(TableBasedQueue queue)
        {
            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();
                using (pipelineExecutor.SetConnection(connectionString, connection))
                {
                    using (var transaction = connection.BeginTransaction(isolationLevel))
                    {
                        using (pipelineExecutor.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));
                            }
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        ReceiveResult TryReceiveWithNativeTransaction()
        {
            var result = new ReceiveResult();

            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();

                using (var transaction = connection.BeginTransaction(GetSqlIsolationLevel(settings.IsolationLevel)))
                {
                    TransportMessage message;
                    try
                    {
                        message = ReceiveWithNativeTransaction(connection, transaction);
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }

                    if (message == null)
                    {
                        transaction.Commit();
                        return(result);
                    }

                    result.Message = message;

                    try
                    {
                        if (MessageSender != null)
                        {
                            MessageSender.SetTransaction(transaction);
                        }

                        if (tryProcessMessage(message))
                        {
                            transaction.Commit();
                        }
                        else
                        {
                            transaction.Rollback();
                        }
                    }
                    catch (Exception ex)
                    {
                        result.Exception = ex;
                        transaction.Rollback();
                    }

                    return(result);
                }
            }
        }
Esempio n. 5
0
        public ReceiveResult TryReceiveFrom(TableBasedQueue queue)
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
            {
                using (var connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    using (pipelineExecutor.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));
                        }
                    }
                }
            }
        }
Esempio n. 6
0
        private void Action(object obj)
        {
            var cancellationToken = (CancellationToken)obj;
            var backOff           = new BackOff(1000);

            while (!cancellationToken.IsCancellationRequested)
            {
                var result = new ReceiveResult();

                try
                {
                    if (settings.IsTransactional)
                    {
                        if (settings.DontUseDistributedTransactions)
                        {
                            result = TryReceiveWithNativeTransaction();
                        }
                        else
                        {
                            result = TryReceiveWithDTCTransaction();
                        }
                    }
                    else
                    {
                        result = TryReceiveWithNoTransaction();
                    }
                }
                finally
                {
                    //since we're polling the message will be null when there was nothing in the queue
                    if (result.Message != null)
                    {
                        endProcessMessage(result.Message, result.Exception);
                    }
                }

                circuitBreaker.Success();
                backOff.Wait(() => result.Message == null);
            }
        }
Esempio n. 7
0
        ReceiveResult TryReceiveWithNoTransaction()
        {
            var result = new ReceiveResult();

            var message = Receive();

            if (message == null)
            {
                return(result);
            }

            result.Message = message;
            try
            {
                tryProcessMessage(message);
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }

            return(result);
        }
        ReceiveResult TryReceiveWithNoTransaction()
        {
            var result = new ReceiveResult();

            var message = Receive();

            if (message == null)
                return result;

            result.Message = message;
            try
            {
                tryProcessMessage(message);

            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }

            return result;
        }
        ReceiveResult TryReceiveWithNativeTransaction()
        {
            var result = new ReceiveResult();

            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();

                using (var transaction = connection.BeginTransaction(GetSqlIsolationLevel(settings.IsolationLevel)))
                {
                    TransportMessage message;
                    try
                    {
                        message = ReceiveWithNativeTransaction(connection, transaction);
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }

                    if (message == null)
                    {
                        transaction.Commit();
                        return result;
                    }

                    result.Message = message;

                    try
                    {
                        if (MessageSender != null)
                        {
                            MessageSender.SetTransaction(transaction);
                        }

                        if (tryProcessMessage(message))
                        {
                            transaction.Commit();
                        }
                        else
                        {
                            transaction.Rollback();
                        }

                    }
                    catch (Exception ex)
                    {
                        result.Exception = ex;
                        transaction.Rollback();
                    }

                    return result;
                }
            }
        }
        ReceiveResult TryReceiveWithDTCTransaction()
        {
            var result = new ReceiveResult();

            using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
            {
                var message = Receive();

                if (message == null)
                {
                    scope.Complete();
                    return result;
                }

                result.Message = message;

                try
                {
                    if (tryProcessMessage(message))
                    {
                        scope.Complete();
                        scope.Dispose(); // We explicitly calling Dispose so that we force any exception to not bubble, eg Concurrency/Deadlock exception.
                    }
                }
                catch (Exception ex)
                {
                    result.Exception = ex;
                }

                return result;
            }
        }
        private void Action(object obj)
        {
            var cancellationToken = (CancellationToken)obj;
            var backOff = new BackOff(1000);

            while (!cancellationToken.IsCancellationRequested)
            {
                var result = new ReceiveResult();

                try
                {
                    if (settings.IsTransactional)
                    {
                        if (settings.DontUseDistributedTransactions)
                        {
                            result = TryReceiveWithNativeTransaction();
                        }
                        else
                        {
                            result = TryReceiveWithDTCTransaction();
                        }
                    }
                    else
                    {
                        result = TryReceiveWithNoTransaction();
                    }
                }
                finally
                {
                    //since we're polling the message will be null when there was nothing in the queue
                    if (result.Message != null)
                        endProcessMessage(result.Message, result.Exception);
                }

                circuitBreaker.Success();
                backOff.Wait(() => result.Message == null);
            }
        }