Пример #1
0
        public async Task PublishAsync_Should_Sent_To_Service_Exchange()
        {
            try
            {
                var networkInfos = RabbitNetworkInfos.GetConfigurationFor("CQELight", RabbitMQExchangeStrategy.Custom);
                networkInfos.ServiceExchangeDescriptions.Add(new RabbitExchangeDescription("pub1_exchange"));
                var config = new RabbitPublisherConfiguration
                {
                    ConnectionInfos = GetConnectionInfos(),
                    NetworkInfos    = networkInfos
                };
                RabbitCommonTools.DeclareExchangesAndQueueForPublisher(channel, config);
                channel.QueueDeclare("CQELight");
                channel.QueueBind("CQELight", "pub1_exchange", "");

                var publisher = new RabbitPublisher(
                    loggerFactory,
                    config);

                await publisher.PublishEventAsync(new TestEvent());

                var result = channel.BasicGet("CQELight", true);
                result.Should().NotBeNull();
                Encoding.UTF8.GetString(result.Body.ToArray()).FromJson <TestCommand>().Should().NotBeNull();
            }
            finally
            {
                DeleteData();
            }
        }
Пример #2
0
 public RabbitPublisher(
     ILoggerFactory loggerFactory,
     RabbitPublisherConfiguration configuration)
 {
     if (loggerFactory == null)
     {
         loggerFactory = new LoggerFactory();
         loggerFactory.AddProvider(new DebugLoggerProvider());
     }
     logger             = loggerFactory.CreateLogger <RabbitPublisher>();
     this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
     RabbitCommonTools.DeclareExchangesAndQueueForPublisher(GetChannel(GetConnection()), configuration);
 }
Пример #3
0
        /// <summary>
        /// Start the RabbitMQ in-app server.
        /// </summary>
        public void Start()
        {
            _consumers  = new List <EventingBasicConsumer>();
            _connection = GetConnection();
            _channel    = GetChannel(_connection);

            RabbitCommonTools.DeclareExchangesAndQueueForSubscriber(_channel, _config);
            foreach (var queueDescription in _config.NetworkInfos.ServiceQueueDescriptions)
            {
                var consumer = new EventingBasicConsumer(_channel);
                consumer.Received += OnEventReceived;
                _channel.BasicConsume(
                    queue: queueDescription.QueueName,
                    autoAck: false,
                    consumer: consumer);
                _consumers.Add(consumer);
            }
        }