Пример #1
0
        public RabbitService(
            Options options,
            ISerializationProvider serializationProvider,
            IEncryptionProvider encryptionProvider               = null,
            ICompressionProvider compressionProvider             = null,
            ILoggerFactory loggerFactory                         = null,
            Func <PublishReceipt, ValueTask> processReceiptAsync = null)
        {
            Guard.AgainstNull(options, nameof(options));
            Guard.AgainstNull(serializationProvider, nameof(serializationProvider));
            LogHelper.LoggerFactory = loggerFactory;

            Options     = options;
            ChannelPool = new ChannelPool(Options);

            SerializationProvider = serializationProvider;
            EncryptionProvider    = encryptionProvider;
            CompressionProvider   = compressionProvider;

            Publisher = new Publisher(ChannelPool, SerializationProvider, EncryptionProvider, CompressionProvider);
            Topologer = new Topologer(ChannelPool);

            Options.ApplyGlobalConsumerOptions();
            BuildConsumers();

            Publisher
            .StartAutoPublishAsync(processReceiptAsync)
            .GetAwaiter()
            .GetResult();

            BuildConsumerTopology()
            .GetAwaiter()
            .GetResult();
        }
Пример #2
0
        public async Task CreateBindAndUnbindExchangeAsync()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top   = new Topologer(options);
            var error = await top.CreateExchangeAsync("TestExchangeTest", "direct", false, false, null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.CreateExchangeAsync("TestExchange2Test", "direct", false, false, null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.BindExchangeToExchangeAsync("TestExchange2Test", "TestExchangeTest", "TestRoutingKeyTest", null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.UnbindExchangeFromExchangeAsync("TestExchange2Test", "TestExchangeTest", "TestRoutingKeyTest", null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.DeleteExchangeAsync("TestExchangeTest").ConfigureAwait(false);

            Assert.False(error);

            error = await top.DeleteExchangeAsync("TestExchange2Test").ConfigureAwait(false);

            Assert.False(error);
        }
Пример #3
0
        public async Task CreateAndBindExchangeAsync()
        {
            var config = new Config();

            config.FactorySettings.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top   = new Topologer(config);
            var error = await top.CreateExchangeAsync("TestExchangeTest", "direct", false, false, null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.CreateExchangeAsync("TestExchange2Test", "direct", false, false, null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.BindExchangeToExchangeAsync("TestExchange2Test", "TestExchangeTest", "TestRoutingKeyTest", null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.DeleteExchangeAsync("TestExchangeTest").ConfigureAwait(false);

            Assert.False(error);

            error = await top.DeleteExchangeAsync("TestExchange2Test").ConfigureAwait(false);

            Assert.False(error);
        }
Пример #4
0
        private async Task BuildConsumerTopology()
        {
            foreach (var consumer in Consumers)
            {
                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.QueueName))
                {
                    await Topologer.CreateQueueAsync(consumer.Value.ConsumerOptions.QueueName).ConfigureAwait(false);
                }

                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.TargetQueueName))
                {
                    await Topologer.CreateQueueAsync(consumer.Value.ConsumerOptions.TargetQueueName).ConfigureAwait(false);
                }

                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.ErrorSuffix) && !string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.ErrorQueueName))
                {
                    await Topologer.CreateQueueAsync(consumer.Value.ConsumerOptions.ErrorQueueName).ConfigureAwait(false);
                }

                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.AltSuffix) && !string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.AltQueueName))
                {
                    await Topologer.CreateQueueAsync(consumer.Value.ConsumerOptions.AltQueueName).ConfigureAwait(false);
                }
            }
        }
Пример #5
0
        public async Task CreateTopologyFromPartialFileAsync()
        {
            var config = await ConfigReader.ConfigFileReadAsync("TestConfig.json");

            var top = new Topologer(config);
            await top
            .CreateTopologyFromFileAsync("TestPartialTopologyConfig.json")
            .ConfigureAwait(false);
        }
Пример #6
0
        public ConsumerTests(ITestOutputHelper output)
        {
            this.output = output;
            config      = ConfigReader.ConfigFileReadAsync("TestConfig.json").GetAwaiter().GetResult();

            channelPool   = new ChannelPool(config);
            topologer     = new Topologer(config);
            rabbitService = new RabbitService("Config.json", null, null, null, null);
        }
Пример #7
0
        public async Task CreateTopologyFromPartialFileAsync()
        {
            var options = await JsonFileReader.ReadFileAsync <RabbitOptions>("TestConfig.json");

            var top = new Topologer(options);
            await top
            .CreateTopologyFromFileAsync("TestPartialTopologyConfig.json")
            .ConfigureAwait(false);
        }
Пример #8
0
        public ConsumerTests(ITestOutputHelper output)
        {
            this.output = output;
            options     = JsonFileReader.ReadFileAsync <Options>("TestConfig.json").GetAwaiter().GetResult();

            channelPool   = new ChannelPool(options);
            topologer     = new Topologer(options);
            rabbitService = new RabbitService("Config.json", null, null, null, null);
        }
Пример #9
0
        public void CreateTopologer()
        {
            var options = new Options();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top = new Topologer(options);

            Assert.NotNull(top);
        }
Пример #10
0
        public void CreateTopologerAndInitializeChannelPool()
        {
            var config = new Config();

            config.FactorySettings.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top = new Topologer(config);

            Assert.NotNull(top);
        }
Пример #11
0
        public void CreateTopologerAndInitializeChannelPool()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top = new Topologer(options);

            Assert.NotNull(top);
        }
Пример #12
0
        public async Task CreateQueueAsync()
        {
            var config = new Config();

            config.FactorySettings.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top   = new Topologer(config);
            var error = await top.CreateQueueAsync("TestQueueTest", false, false, false, null).ConfigureAwait(false);

            Assert.False(error);
        }
Пример #13
0
        public async Task CreateExchangeAsync()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top   = new Topologer(options);
            var error = await top.CreateExchangeAsync("TestExchangeTest", "direct", false, false, null).ConfigureAwait(false);

            Assert.False(error);
        }
Пример #14
0
        public void CreateTopologerWithChannelPool()
        {
            var options = new Options();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var chanPool = new ChannelPool(options);
            var top      = new Topologer(chanPool);

            Assert.NotNull(top);
        }
Пример #15
0
        /// <summary>
        /// Builds out a RabbitService with instantiated dependencies based on config settings.
        /// </summary>
        /// <param name="config"></param>
        /// <param name="loggerFactory"></param>
        public RabbitService(Config config, ILoggerFactory loggerFactory = null)
        {
            LogHelper.LoggerFactory = loggerFactory;

            Config        = config;
            ChannelPool   = new ChannelPool(Config);
            AutoPublisher = new AutoPublisher(ChannelPool);
            Topologer     = new Topologer(ChannelPool);

            BuildConsumers();
        }
Пример #16
0
        public PublisherConsumerTests(ITestOutputHelper output)
        {
            this.output = output;
            config      = ConfigReader.ConfigFileReadAsync("TestConfig.json").GetAwaiter().GetResult();

            var channelPool = new ChannelPool(config);

            topologer = new Topologer(channelPool);

            publisher = new Publisher(channelPool, new byte[] { });
            consumer  = new Consumer(channelPool, "TestAutoPublisherConsumerName");
        }
Пример #17
0
        public async Task CreateQueueWithoutInitializeAsync()
        {
            var config = new Config();

            config.FactorySettings.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top = new Topologer(config);

            await Assert
            .ThrowsAsync <InvalidOperationException>(() => top.CreateQueueAsync("TestQueue", false, false, false, null))
            .ConfigureAwait(false);
        }
Пример #18
0
        public AutoPublisherTests(ITestOutputHelper output)
        {
            this.output = output;
            options     = new Options();
            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");
            options.PublisherOptions   = new PublisherOptions();
            options.PublisherOptions.CreatePublishReceipts = true;

            channelPool = new ChannelPool(options);

            topologer = new Topologer(channelPool);
        }
Пример #19
0
        public PublisherConsumerTests(ITestOutputHelper output)
        {
            this.output = output;
            options     = JsonFileReader.ReadFileAsync <Options>("TestConfig.json").GetAwaiter().GetResult();

            var channelPool = new ChannelPool(options);

            topologer = new Topologer(channelPool);

            publisher = new Publisher(channelPool, new byte[] { });
            consumer  = new Consumer(channelPool, "TestAutoPublisherConsumerName");
        }
Пример #20
0
        public AutoPublisherTests(ITestOutputHelper output)
        {
            this.output = output;
            config      = new Config();
            config.FactorySettings.Uri = new Uri("amqp://*****:*****@localhost:5672/");
            config.PublisherSettings   = new PublisherOptions();
            config.PublisherSettings.CreatePublishReceipts = true;

            channelPool = new ChannelPool(config);

            topologer = new Topologer(channelPool);
        }
        public AutoPublisherConsumerTests(ITestOutputHelper output)
        {
            this.output = output;
            config      = ConfigReader.ConfigFileReadAsync("Config.json").GetAwaiter().GetResult();

            var channelPool = new ChannelPool(config);

            topologer = new Topologer(channelPool);
            topologer.InitializeAsync().GetAwaiter().GetResult();

            autoPublisher = new AutoPublisher(channelPool);
            consumer      = new Consumer(channelPool, "ConsumerFromConfig");
        }
Пример #22
0
        private static async Task SetupAsync()
        {
            var sw = Stopwatch.StartNew();

            config = await ConfigReader.ConfigFileReadAsync("Config.json");

            channelPool = new ChannelPool(config);

            topologer = new Topologer(channelPool);

            apub1 = new Publisher(channelPool, new byte[] { });
            apub2 = new Publisher(channelPool, new byte[] { });
            apub3 = new Publisher(channelPool, new byte[] { });
            apub4 = new Publisher(channelPool, new byte[] { });

            await Console.Out.WriteLineAsync("- Creating stress test queues!").ConfigureAwait(false);

            foreach (var kvp in config.ConsumerSettings)
            {
                await topologer
                .DeleteQueueAsync(kvp.Value.QueueName)
                .ConfigureAwait(false);
            }

            foreach (var kvp in config.ConsumerSettings)
            {
                await topologer
                .CreateQueueAsync(kvp.Value.QueueName, true)
                .ConfigureAwait(false);
            }

            await apub1.StartAutoPublishAsync().ConfigureAwait(false);

            await apub2.StartAutoPublishAsync().ConfigureAwait(false);

            await apub3.StartAutoPublishAsync().ConfigureAwait(false);

            await apub4.StartAutoPublishAsync().ConfigureAwait(false);

            con1 = new Consumer(channelPool, "Consumer1");
            con2 = new Consumer(channelPool, "Consumer2");
            con3 = new Consumer(channelPool, "Consumer3");
            con4 = new Consumer(channelPool, "Consumer4");
            sw.Stop();

            await Console
            .Out
            .WriteLineAsync($"- Setup has finished in {sw.ElapsedMilliseconds} ms.")
            .ConfigureAwait(false);
        }
Пример #23
0
        public async Task CreateAndDeleteQueueAsync()
        {
            var options = new Options();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top   = new Topologer(options);
            var error = await top.CreateQueueAsync("TestQueueTest", false, false, false, null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.DeleteQueueAsync("TestQueueTest", false, false).ConfigureAwait(false);

            Assert.False(error);
        }
Пример #24
0
        /// <summary>
        /// Reads config from a provided file name path. Builds out a RabbitService with instantiated dependencies based on config settings.
        /// </summary>
        /// <param name="fileNamePath"></param>
        /// <param name="loggerFactory"></param>
        public RabbitService(string fileNamePath, ILoggerFactory loggerFactory = null)
        {
            LogHelper.LoggerFactory = loggerFactory;

            Config = ConfigReader
                     .ConfigFileReadAsync(fileNamePath)
                     .GetAwaiter()
                     .GetResult();

            ChannelPool   = new ChannelPool(Config);
            AutoPublisher = new AutoPublisher(ChannelPool);
            Topologer     = new Topologer(ChannelPool);

            BuildConsumers();
        }
Пример #25
0
        public async Task CreateQueueWithArgsAsync()
        {
            var config = new Config();

            config.FactorySettings.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top = new Topologer(config);
            await top.InitializeAsync().ConfigureAwait(false);


            var args = new Dictionary <string, object>()
            {
                { "Test", "Value" }
            };
            var error = await top
                        .CreateQueueAsync("TestQueueTest", false, false, false, args)
                        .ConfigureAwait(false);

            Assert.False(error);
        }
Пример #26
0
        /// <summary>
        /// Builds out a RabbitService with instantiated dependencies based on config settings.
        /// </summary>
        /// <param name="config"></param>
        /// <param name="passphrase"></param>
        /// <param name="salt"></param>
        /// <param name="loggerFactory"></param>
        /// <param name="processReceiptAsync"></param>
        public RabbitService(Config config, string passphrase, string salt, ILoggerFactory loggerFactory = null, Func <PublishReceipt, ValueTask> processReceiptAsync = null)
        {
            LogHelper.LoggerFactory = loggerFactory;

            Config      = config;
            ChannelPool = new ChannelPool(Config);

            if (!string.IsNullOrWhiteSpace(passphrase))
            {
                _hashKey = ArgonHash
                           .GetHashKeyAsync(passphrase, salt, Utils.Constants.EncryptionKeySize)
                           .GetAwaiter().GetResult();
            }

            Publisher = new Publisher(ChannelPool, _hashKey);
            Topologer = new Topologer(ChannelPool);

            BuildConsumers();

            StartAsync(processReceiptAsync).GetAwaiter().GetResult();
        }
Пример #27
0
        private static async Task SetupAsync()
        {
            var sw = Stopwatch.StartNew();

            options = await JsonFileReader.ReadFileAsync <Options>("Config.json");

            channelPool = new ChannelPool(options);

            topologer = new Topologer(channelPool);

            apub1 = new Publisher(
                channelPool,
                _serializationProvider,
                _encryptionProvider,
                _compressionProvider);

            apub2 = new Publisher(
                channelPool,
                _serializationProvider,
                _encryptionProvider,
                _compressionProvider);

            apub3 = new Publisher(
                channelPool,
                _serializationProvider,
                _encryptionProvider,
                _compressionProvider);

            apub4 = new Publisher(
                channelPool,
                _serializationProvider,
                _encryptionProvider,
                _compressionProvider);

            await Console.Out.WriteLineAsync("- Creating stress test queues!").ConfigureAwait(false);

            foreach (var kvp in options.ConsumerOptions)
            {
                await topologer
                .DeleteQueueAsync(kvp.Value.QueueName)
                .ConfigureAwait(false);
            }

            foreach (var kvp in options.ConsumerOptions)
            {
                await topologer
                .CreateQueueAsync(kvp.Value.QueueName, true)
                .ConfigureAwait(false);
            }

            await apub1.StartAutoPublishAsync().ConfigureAwait(false);

            await apub2.StartAutoPublishAsync().ConfigureAwait(false);

            await apub3.StartAutoPublishAsync().ConfigureAwait(false);

            await apub4.StartAutoPublishAsync().ConfigureAwait(false);

            con1 = new Consumer(channelPool, "Consumer1");
            con2 = new Consumer(channelPool, "Consumer2");
            con3 = new Consumer(channelPool, "Consumer3");
            con4 = new Consumer(channelPool, "Consumer4");
            sw.Stop();

            await Console
            .Out
            .WriteLineAsync($"- Setup has finished in {sw.ElapsedMilliseconds} ms.")
            .ConfigureAwait(false);
        }
Пример #28
0
        private async Task FinishSettingUpConsumersAsync()
        {
            foreach (var consumer in Consumers)
            {
                consumer.Value.HashKey = _hashKey;
                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerSettings.QueueName))
                {
                    if (consumer.Value.ConsumerSettings.QueueArgs == null)
                    {
                        await Topologer
                        .CreateQueueAsync(consumer.Value.ConsumerSettings.QueueName)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        await Topologer
                        .CreateQueueAsync(
                            consumer.Value.ConsumerSettings.QueueName,
                            true,
                            false,
                            false,
                            consumer.Value.ConsumerSettings.QueueArgs)
                        .ConfigureAwait(false);
                    }
                }

                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerSettings.TargetQueueName))
                {
                    if (consumer.Value.ConsumerSettings.TargetQueueArgs == null)
                    {
                        await Topologer
                        .CreateQueueAsync(consumer.Value.ConsumerSettings.TargetQueueName)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        await Topologer
                        .CreateQueueAsync(
                            consumer.Value.ConsumerSettings.TargetQueueName,
                            true,
                            false,
                            false,
                            consumer.Value.ConsumerSettings.TargetQueueArgs)
                        .ConfigureAwait(false);
                    }
                }

                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerSettings.AltQueueName))
                {
                    if (consumer.Value.ConsumerSettings.AltQueueArgs == null)
                    {
                        await Topologer
                        .CreateQueueAsync(consumer.Value.ConsumerSettings.AltQueueName)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        await Topologer
                        .CreateQueueAsync(
                            consumer.Value.ConsumerSettings.AltQueueName,
                            true,
                            false,
                            false,
                            consumer.Value.ConsumerSettings.AltQueueArgs)
                        .ConfigureAwait(false);
                    }
                }

                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerSettings.ErrorSuffix) &&
                    !string.IsNullOrWhiteSpace(consumer.Value.ConsumerSettings.ErrorQueueName))
                {
                    if (consumer.Value.ConsumerSettings.ErrorQueueArgs == null)
                    {
                        await Topologer
                        .CreateQueueAsync(consumer.Value.ConsumerSettings.ErrorQueueName)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        await Topologer
                        .CreateQueueAsync(
                            consumer.Value.ConsumerSettings.ErrorQueueName,
                            true,
                            false,
                            false,
                            consumer.Value.ConsumerSettings.ErrorQueueArgs)
                        .ConfigureAwait(false);
                    }
                }
            }
        }
Пример #29
0
        private async Task BuildConsumerTopologyAsync()
        {
            foreach (var consumer in Consumers)
            {
                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.QueueName))
                {
                    if (consumer.Value.ConsumerOptions.QueueArgs == null ||
                        consumer.Value.ConsumerOptions.QueueArgs.Count == 0)
                    {
                        await Topologer
                        .CreateQueueAsync(consumer.Value.ConsumerOptions.QueueName)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        await Topologer
                        .CreateQueueAsync(
                            consumer.Value.ConsumerOptions.QueueName,
                            true,
                            false,
                            false,
                            consumer.Value.ConsumerOptions.QueueArgs)
                        .ConfigureAwait(false);
                    }
                }

                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.TargetQueueName))
                {
                    if (consumer.Value.ConsumerOptions.TargetQueueArgs == null ||
                        consumer.Value.ConsumerOptions.TargetQueueArgs.Count == 0)
                    {
                        await Topologer
                        .CreateQueueAsync(consumer.Value.ConsumerOptions.TargetQueueName)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        await Topologer
                        .CreateQueueAsync(
                            consumer.Value.ConsumerOptions.TargetQueueName,
                            true,
                            false,
                            false,
                            consumer.Value.ConsumerOptions.TargetQueueArgs)
                        .ConfigureAwait(false);
                    }
                }

                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.ErrorSuffix) &&
                    !string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.ErrorQueueName))
                {
                    if (consumer.Value.ConsumerOptions.ErrorQueueArgs == null ||
                        consumer.Value.ConsumerOptions.ErrorQueueArgs.Count == 0)
                    {
                        await Topologer
                        .CreateQueueAsync(consumer.Value.ConsumerOptions.ErrorQueueName)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        await Topologer
                        .CreateQueueAsync(
                            consumer.Value.ConsumerOptions.ErrorQueueName,
                            true,
                            false,
                            false,
                            consumer.Value.ConsumerOptions.ErrorQueueArgs)
                        .ConfigureAwait(false);
                    }
                }

                if (!string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.AltSuffix) &&
                    !string.IsNullOrWhiteSpace(consumer.Value.ConsumerOptions.AltQueueName))
                {
                    if (consumer.Value.ConsumerOptions.AltQueueArgs == null ||
                        consumer.Value.ConsumerOptions.AltQueueArgs.Count == 0)
                    {
                        await Topologer
                        .CreateQueueAsync(consumer.Value.ConsumerOptions.AltQueueName)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        await Topologer
                        .CreateQueueAsync(
                            consumer.Value.ConsumerOptions.AltQueueName,
                            true,
                            false,
                            false,
                            consumer.Value.ConsumerOptions.AltQueueArgs)
                        .ConfigureAwait(false);
                    }
                }
            }
        }