Example #1
0
        public Task PublishAsync(
            ILinkPublishMessage <byte[]> message,
            CancellationToken?cancellation = null
            )
        {
            if (State == LinkProducerState.Disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (message.PublishProperties.Mandatory == true && !ConfirmsMode)
            {
                throw new LinkNotSupportedException("Mandatory without ConfirmsMode not supported");
            }

            if (cancellation == null)
            {
                if (_configuration.PublishTimeout != TimeSpan.Zero &&
                    _configuration.PublishTimeout != Timeout.InfiniteTimeSpan)
                {
                    cancellation = new CancellationTokenSource(_configuration.PublishTimeout).Token;
                }
                else
                {
                    cancellation = CancellationToken.None;
                }
            }

            var msgProperties = _configuration.MessageProperties.Extend(message.Properties);

            msgProperties.AppId = _appId;

            var publishProperties = _configuration.PublishProperties.Extend(message.PublishProperties);

            var body = message.Body;

            _configuration.MessageIdGenerator.SetMessageId(
                body,
                msgProperties,
                publishProperties.Clone()
                );

            var msg = new LinkProducerMessage(body, msgProperties, publishProperties, cancellation.Value);

            try
            {
                _messageQueue.Put(msg);
            }
            catch
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            return(msg.Completion);
        }
Example #2
0
        private void ConsumerOnReceived(object sender, BasicDeliverEventArgs e)
        {
            try
            {
                var props = new LinkMessageProperties();
                props.Extend(e.BasicProperties);

                var recieveProps = new LinkRecieveProperties(e.Redelivered, e.Exchange, e.RoutingKey, _queue.Name,
                                                             props.AppId == _appId);

                var token = _consumerCancellationTokenSource.Token;

                var msg = new LinkConsumedMessage <byte[]>(e.Body, props, recieveProps, token);

                HandleMessageAsync(msg, e.DeliveryTag);
            }
            catch (Exception ex)
            {
                _logger.Error($"Recieve message error, NACKing: {ex}");

                try
                {
                    _actionQueue.Put(new LinkConsumerMessageAction(
                                         e.DeliveryTag,
                                         LinkConsumerAckStrategy.Nack,
                                         _consumerCancellationTokenSource.Token)
                                     );
                }
                catch
                {
                    // No-op
                }
            }
        }