public RabbitMQMessagePublisher(IRabbitMQConnectionFactory connectionFactory,
                                 IOptions <RabbitMQPublisherConfig <T> > config, ICloudEventFormatter cloudEventFormatter)
 {
     _connectionFactory   = connectionFactory;
     _cloudEventFormatter = cloudEventFormatter;
     _config = config.Value;
 }
        public static void Update <T>(this IBasicProperties self, MotorCloudEvent <byte[]> cloudEvent,
                                      RabbitMQPublisherConfig <T> config, ICloudEventFormatter cloudEventFormatter)
        {
            var messagePriority = cloudEvent.Extension <RabbitMQPriorityExtension>()?.Priority ??
                                  config.DefaultPriority;

            if (messagePriority.HasValue)
            {
                self.Priority = messagePriority.Value;
            }
            var dictionary = new Dictionary <string, object>();

            foreach (var attr in cloudEvent.GetAttributes())
            {
                if (string.Equals(attr.Key, CloudEventAttributes.DataAttributeName(cloudEvent.SpecVersion)) ||
                    string.Equals(attr.Key,
                                  CloudEventAttributes.DataContentTypeAttributeName(cloudEvent.SpecVersion)) ||
                    string.Equals(attr.Key, RabbitMQPriorityExtension.PriorityAttributeName) ||
                    string.Equals(attr.Key, RabbitMQBindingConfigExtension.ExchangeAttributeName) ||
                    string.Equals(attr.Key, RabbitMQBindingConfigExtension.RoutingKeyAttributeName))
                {
                    continue;
                }
                dictionary.Add($"{CloudEventPrefix}{attr.Key}",
                               cloudEventFormatter.EncodeAttribute(cloudEvent.SpecVersion, attr.Key, attr.Value,
                                                                   cloudEvent.GetExtensions().Values));
            }

            self.Headers = dictionary;
        }
        public void BindPublisherConfig_ConfigWithTarget_ContainsPublishingTarget()
        {
            var config          = GetJsonConfig("publisher-with-target");
            var publisherConfig = new RabbitMQPublisherConfig <string>();

            config.Bind(publisherConfig);

            Assert.NotNull(publisherConfig.PublishingTarget);
            Assert.Equal("exchange", publisherConfig.PublishingTarget.Exchange);
            Assert.Equal("routing", publisherConfig.PublishingTarget.RoutingKey);
            Assert.Equal("teststring", publisherConfig.PublishingTarget.Arguments["string"]);
        }
        private ITypedMessagePublisher <byte[]> GetPublisher(
            IRabbitMQConnectionFactory connectionFactory = null,
            RabbitMQPublisherConfig <string> config      = null)
        {
            connectionFactory ??= GetDefaultConnectionFactoryMock().Object;
            config ??= GetConfig();

            var configMock = new Mock <IOptions <RabbitMQPublisherConfig <string> > >();

            configMock.Setup(x => x.Value).Returns(config);
            return(new RabbitMQMessagePublisher <string>(connectionFactory, configMock.Object, new JsonEventFormatter()));
        }
        public void BindPublisherConfig_ConfigWithoutQueue_ContainsAllValues()
        {
            var config          = GetJsonConfig("publisher-no-target");
            var publisherConfig = new RabbitMQPublisherConfig <string>();

            config.Bind(publisherConfig);

            Assert.Equal("hostname", publisherConfig.Host);
            Assert.Equal(10000, publisherConfig.Port);
            Assert.Equal("username", publisherConfig.User);
            Assert.Equal("password", publisherConfig.Password);
            Assert.Equal("vhost", publisherConfig.VirtualHost);
        }
Ejemplo n.º 6
0
        public void FromPublisherConfig_NullPublishingTarget_Throws()
        {
            var factory = new RabbitMQConnectionFactory();
            var cfg     = new RabbitMQPublisherConfig <string>
            {
                Host             = "host",
                User             = "******",
                Password         = "******",
                VirtualHost      = "vHost",
                PublishingTarget = null
            };

            Assert.Throws <ArgumentException>(() => factory.From(cfg));
        }
Ejemplo n.º 7
0
 private void ThrowIfConfigInvalid <T>(RabbitMQPublisherConfig <T> config)
 {
     ThrowIfInvalid(config);
     ThrowIfInvalid(config.PublishingTarget);
 }
Ejemplo n.º 8
0
 public IConnectionFactory From <T>(RabbitMQPublisherConfig <T> publisherConfig)
 {
     ThrowIfConfigInvalid(publisherConfig);
     return(FromConfig(publisherConfig));
 }