示例#1
0
        public void Publish <T>(T message, Action <IPublishConfiguration <T> > configure)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (configure == null)
            {
                throw new ArgumentNullException("configure");
            }

            var configuration = new PublishConfiguration <T>();

            configure(configuration);

            var exchangeName    = bus.Conventions.ExchangeNamingConvention(typeof(T));
            var exchange        = Exchange.DeclareTopic(exchangeName);
            var easyNetQMessage = new Message <T>(message);

            // by default publish persistent messages
            easyNetQMessage.Properties.DeliveryMode = 2;

            var topic = configuration.Topics.Any()
                ? configuration.Topics[0]
                : bus.Conventions.TopicNamingConvention(typeof(T));

            advancedPublishChannel.Publish(exchange, topic, easyNetQMessage);
        }
示例#2
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);
        }
示例#3
0
        public virtual void Publish <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 = publishExchangeDeclareStrategy.DeclareExchange(advancedBus, messageType, ExchangeType.Topic);

            advancedBus.Publish(exchange, configuration.Topic, false, easyNetQMessage);
        }
示例#4
0
 private Action <IAdvancedPublishConfiguration> MapConfiguration <T>(PublishConfiguration <T> configuration)
 {
     return(x =>
     {
         if (configuration.SuccessCallback != null)
         {
             x.OnSuccess(configuration.SuccessCallback);
         }
         if (configuration.FailureCallback != null)
         {
             x.OnFailure(configuration.FailureCallback);
         }
     });
 }
示例#5
0
        public virtual void Publish <T>(T message, Action <IPublishConfiguration <T> > configure)
        {
            Preconditions.CheckNotNull(message, "message");
            Preconditions.CheckNotNull(configure, "configure");

            var configuration = new PublishConfiguration <T>();

            configure(configuration);

            var exchangeName    = bus.Conventions.ExchangeNamingConvention(typeof(T));
            var exchange        = Exchange.DeclareTopic(exchangeName);
            var easyNetQMessage = new Message <T>(message);

            // by default publish persistent messages
            easyNetQMessage.Properties.DeliveryMode = 2;

            var topic = configuration.Topics.Any()
                ? configuration.Topics[0]
                : bus.Conventions.TopicNamingConvention(typeof(T));

            advancedPublishChannel.Publish(exchange, topic, easyNetQMessage, MapConfiguration(configuration));
        }