Пример #1
0
        protected override async Task <TransportMessage> ReadMessage(bool isPrimaryReader, long taskId, CancellationToken token, ITransactionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context), "ITransactionContext context == null");
            }
            TransportMessage msg;

            try
            {
                msg = await _transport.Receive(context, token);
            }
            catch (OperationCanceledException)
            {
                // it's fine - just a sign that we are shutting down
                throw;
            }
            catch (Exception exception)
            {
                Log.Warn("An error occurred when attempting to receive the next message: {exception}", exception);
                if (IsPrimaryReader)
                {
                    await _backoffStrategy.WaitErrorAsync(token);
                }
                return(null);
            }
            token.ThrowIfCancellationRequested();

            return(msg);
        }
Пример #2
0
        private async Task ProcessOutboxMessages()
        {
            _log.Debug("Starting outbox messages processor");

            while (!_busDisposalCancellationToken.IsCancellationRequested)
            {
                try
                {
                    bool waitForMessages;
                    using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                    {
                        var messages = await _outboxStorage.Retrieve(_busDisposalCancellationToken, _topMessagesToRetrieve);

                        if (messages.Length > 0)
                        {
                            using (var rebusTransactionScope = new RebusTransactionScope())
                            {
                                foreach (var message in messages)
                                {
                                    var destinationAddress = message.Headers[OutboxHeaders.Recipient];
                                    message.Headers.Remove(OutboxHeaders.Recipient);
                                    await _transport.Send(destinationAddress, message,
                                                          rebusTransactionScope.TransactionContext);
                                }
                                await rebusTransactionScope.CompleteAsync();
                            }
                            waitForMessages = false;
                        }
                        else
                        {
                            waitForMessages = true;
                        }

                        transactionScope.Complete();
                    }

                    if (waitForMessages)
                    {
                        await _backoffStrategy.WaitNoMessageAsync(_busDisposalCancellationToken);
                    }
                    else
                    {
                        _backoffStrategy.Reset();
                    }
                }
                catch (OperationCanceledException) when(_busDisposalCancellationToken.IsCancellationRequested)
                {
                    // we're shutting down
                }
                catch (Exception exception)
                {
                    _log.Error(exception, "Unhandled exception in outbox messages processor");
                    await _backoffStrategy.WaitErrorAsync(_busDisposalCancellationToken);
                }
            }

            _log.Debug("Outbox messages processor stopped");
        }
Пример #3
0
        private async Task _processOutboxMessages(CancellationToken ctk)
        {
            _log.Debug("Starting outbox messages processor");

            while (!ctk.IsCancellationRequested)
            {
                try
                {
                    bool waitForMessages = true;
                    using (var ctx = _outboxContextFactory.Create())
                    {
                        var messages = await ctx.PeekLockMessagesAsync(_topMessagesToRetrieve, ctk);

                        if (messages.Any())
                        {
                            using (var rebusTransactionScope = new RebusTransactionScope())
                            {
                                foreach (var message in messages)
                                {
                                    var destinationAddress = message.Headers[OutboxTransportDecorator._outboxRecepientHeader];
                                    message.Headers.Remove(OutboxTransportDecorator._outboxRecepientHeader);
                                    await _transport.Send(destinationAddress, new TransportMessage(message.Headers, message.Body),
                                                          rebusTransactionScope.TransactionContext).WithCancellation(ctk);
                                }
                                await rebusTransactionScope.CompleteAsync().WithCancellation(ctk);
                            }
                            waitForMessages = false;
                            _backoffStrategy.Reset();
                        }
                        ctx.Commit();
                    }

                    if (waitForMessages)
                    {
                        await _backoffStrategy.WaitNoMessageAsync(ctk);
                    }
                }
                catch (OperationCanceledException) when(ctk.IsCancellationRequested)
                {
                    // we're shutting down
                }
                catch (Exception exception)
                {
                    _log.Error(exception, "Unhandled exception in outbox messages processor");
                    await _backoffStrategy.WaitErrorAsync(ctk);
                }
            }

            _log.Debug("Outbox messages processor stopped");
        }
Пример #4
0
        async Task <TransportMessage> ReceiveTransportMessage(CancellationToken token, ITransactionContext context)
        {
            try
            {
                return(await _transport.Receive(context, token));
            }
            catch (OperationCanceledException) when(token.IsCancellationRequested || _busDisposalCancellationToken.IsCancellationRequested)
            {
                // it's fine - just a sign that we are shutting down
                return(null);
            }
            catch (Exception exception)
            {
                _log.Warn("An error occurred when attempting to receive the next message: {exception}", exception);

                await _backoffStrategy.WaitErrorAsync(token);

                return(null);
            }
        }