public async Task FuturePublishAsync <T>(DateTime futurePublishDate, T message, string topic, string cancellationKey = null) where T : class
        {
            Preconditions.CheckNotNull(message, "message");
            var scheduleMeType     = typeof(ScheduleMe);
            var scheduleMeExchange = await exchangeDeclareStrategy.DeclareExchangeAsync(scheduleMeType, ExchangeType.Topic).ConfigureAwait(false);

            var baseMessageType     = typeof(T);
            var concreteMessageType = message.GetType();
            var serializedMessage   = messageSerializationStrategy.SerializeMessage(new Message <T>(message)
            {
                Properties =
                {
                    DeliveryMode = messageDeliveryModeStrategy.GetDeliveryMode(concreteMessageType)
                }
            });
            var scheduleMe = new ScheduleMe
            {
                WakeTime          = futurePublishDate,
                CancellationKey   = cancellationKey,
                InnerMessage      = serializedMessage.Body,
                MessageProperties = serializedMessage.Properties,
                BindingKey        = typeNameSerializer.Serialize(typeof(T)),
                ExchangeType      = ExchangeType.Topic,
                Exchange          = conventions.ExchangeNamingConvention(baseMessageType),
                RoutingKey        = topic
            };
            var easyNetQMessage = new Message <ScheduleMe>(scheduleMe)
            {
                Properties =
                {
                    DeliveryMode = messageDeliveryModeStrategy.GetDeliveryMode(scheduleMeType)
                }
            };
            await advancedBus.PublishAsync(scheduleMeExchange, conventions.TopicNamingConvention(scheduleMeType), false, easyNetQMessage).ConfigureAwait(false);
        }
        /// <inheritdoc />
        public async Task FuturePublishAsync <T>(
            T message,
            TimeSpan delay,
            Action <IFuturePublishConfiguration> configure,
            CancellationToken cancellationToken = default
            )
        {
            Preconditions.CheckNotNull(message, "message");
            Preconditions.CheckNotNull(configure, "configure");

            using var cts = cancellationToken.WithTimeout(configuration.Timeout);

            var publishConfiguration = new FuturePublishConfiguration(conventions.TopicNamingConvention(typeof(T)));

            configure(publishConfiguration);

            var topic    = publishConfiguration.Topic;
            var exchange = await exchangeDeclareStrategy.DeclareExchangeAsync(
                conventions.ExchangeNamingConvention(typeof(T)),
                ExchangeType.Topic,
                cts.Token
                ).ConfigureAwait(false);

            var delayString    = delay.ToString(@"dd\_hh\_mm\_ss");
            var futureExchange = await exchangeDeclareStrategy.DeclareExchangeAsync(
                $"{conventions.ExchangeNamingConvention(typeof(T))}_{delayString}",
                ExchangeType.Topic,
                cts.Token
                ).ConfigureAwait(false);

            var futureQueue = await advancedBus.QueueDeclareAsync(
                conventions.QueueNamingConvention(typeof(T), delayString),
                c =>
            {
                c.WithMessageTtl(delay);
                c.WithDeadLetterExchange(exchange);
                if (setDeadLetterRoutingKey)
                {
                    c.WithDeadLetterRoutingKey(topic);
                }
            },
                cts.Token
                ).ConfigureAwait(false);

            await advancedBus.BindAsync(futureExchange, futureQueue, topic, cts.Token).ConfigureAwait(false);

            var properties = new MessageProperties();

            if (publishConfiguration.Priority != null)
            {
                properties.Priority = publishConfiguration.Priority.Value;
            }
            properties.DeliveryMode = messageDeliveryModeStrategy.GetDeliveryMode(typeof(T));

            var advancedMessage = new Message <T>(message, properties);
            await advancedBus.PublishAsync(
                futureExchange, topic, configuration.MandatoryPublish, advancedMessage, cts.Token
                ).ConfigureAwait(false);
        }
Beispiel #3
0
        /// <inheritdoc />
        public virtual async Task PublishAsync <T>(T message, Action <IPublishConfiguration> configure, CancellationToken cancellationToken)
        {
            Preconditions.CheckNotNull(message, "message");
            Preconditions.CheckNotNull(configure, "configure");

            using var cts = cancellationToken.WithTimeout(configuration.Timeout);

            var publishConfiguration = new PublishConfiguration(conventions.TopicNamingConvention(typeof(T)));

            configure(publishConfiguration);

            var messageType = typeof(T);
            var advancedMessageProperties = new MessageProperties();

            if (publishConfiguration.Priority != null)
            {
                advancedMessageProperties.Priority = publishConfiguration.Priority.Value;
            }
            if (publishConfiguration.Expires != null)
            {
                advancedMessageProperties.Expiration = publishConfiguration.Expires.ToString();
            }
            advancedMessageProperties.DeliveryMode = messageDeliveryModeStrategy.GetDeliveryMode(messageType);

            var advancedMessage = new Message <T>(message, advancedMessageProperties);
            var exchange        = await exchangeDeclareStrategy.DeclareExchangeAsync(
                messageType, ExchangeType.Topic, cts.Token
                ).ConfigureAwait(false);

            await advancedBus.PublishAsync(
                exchange, publishConfiguration.Topic, false, advancedMessage, cts.Token
                ).ConfigureAwait(false);
        }
Beispiel #4
0
        public virtual async Task PublishAsync <T>(T message, Action <IPublishConfiguration> configure) where T : class
        {
            Preconditions.CheckNotNull(message, "message");
            Preconditions.CheckNotNull(configure, "configure");

            var configuration = new PublishConfiguration(conventions.TopicNamingConvention(typeof(T)));

            configure(configuration);

            var messageType     = typeof(T);
            var easyNetQMessage = new Message <T>(message)
            {
                Properties =
                {
                    DeliveryMode = messageDeliveryModeStrategy.GetDeliveryMode(messageType)
                }
            };

            if (configuration.Priority != null)
            {
                easyNetQMessage.Properties.Priority = configuration.Priority.Value;
            }
            if (configuration.Expires != null)
            {
                easyNetQMessage.Properties.Expiration = configuration.Expires.ToString();
            }

            var exchange = await exchangeDeclareStrategy.DeclareExchangeAsync(messageType, ExchangeType.Topic).ConfigureAwait(false);

            await advancedBus.PublishAsync(exchange, configuration.Topic, false, easyNetQMessage).ConfigureAwait(false);
        }
        public static IExchange DeclareExchange(
            this IExchangeDeclareStrategy strategy,
            string exchangeName,
            string exchangeType,
            CancellationToken cancellationToken = default
            )
        {
            Preconditions.CheckNotNull(strategy, "strategy");

            return(strategy.DeclareExchangeAsync(exchangeName, exchangeType, cancellationToken)
                   .GetAwaiter()
                   .GetResult());
        }