Exemple #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);
        }
Exemple #2
0
        public Task PublishAsync <TBody>(ILinkPublishMessage <TBody> message, CancellationToken?cancellation = null)
            where TBody : class
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var props = message.Properties.Clone();

            byte[] body;

            try
            {
                if (_configuration.Serializer == null)
                {
                    throw new InvalidOperationException("Serializer not set for producer");
                }

                body = _configuration.Serializer.Serialize(message.Body, props);
            }
            catch (Exception ex)
            {
                throw new LinkSerializationException(ex);
            }

            var typeName = _configuration.TypeNameMapping.Map(typeof(TBody));

            if (typeName != null)
            {
                props.Type = typeName;
            }
            else if (!_configuration.TypeNameMapping.IsEmpty)
            {
                throw new LinkProducerTypeNameMappingException(typeof(TBody));
            }

            return(PublishAsync(new LinkPublishMessage <byte[]>(body, props, message.PublishProperties), cancellation));
        }