Exemplo n.º 1
0
        public MessageQueueFactory CreateMessageQueueInstance()
        {
            var factory = new MessageQueueFactory(
                new MessageQueueFactory.HostOptions
            {
                HostName    = configuration["MessageQueue:HostName"],
                VirtualHost = configuration["MessageQueue:VirtualHost"],
                Port        = int.Parse(configuration["MessageQueue:Port"]),
                UserName    = configuration["MessageQueue:UserName"],
                Password    = configuration["MessageQueue:Password"]
            });

            var cnt = -1;

            while (configuration.GetSection($"MessageQueue:Producers:{++cnt}").Exists())
            {
                factory.CreateProducer(new MessageQueueFactory.ProducerOptions
                {
                    Queue      = configuration[$"MessageQueue:Producers:{cnt}:Queue"],
                    Durable    = bool.Parse(configuration[$"MessageQueue:Producers:{cnt}:Durable"]),
                    AutoDelete = bool.Parse(configuration[$"MessageQueue:Producers:{cnt}:AutoDelete"]),
                    Exclusive  = bool.Parse(configuration[$"MessageQueue:Producers:{cnt}:Exclusive"]),
                    Exchange   = configuration[$"MessageQueue:Producers:{cnt}:Exchange"],
                    RoutingKey = configuration[$"MessageQueue:Producers:{cnt}:RoutingKey"]
                });
            }

            cnt = -1;
            while (configuration.GetSection($"MessageQueue:Consumers:{++cnt}").Exists())
            {
                var consumer = new MessageQueueFactory.ConsumerOptions
                {
                    Queue      = configuration[$"MessageQueue:Consumers:{cnt}:Queue"],
                    Durable    = bool.Parse(configuration[$"MessageQueue:Consumers:{cnt}:Durable"]),
                    AutoAck    = bool.Parse(configuration[$"MessageQueue:Consumers:{cnt}:AutoAck"]),
                    Exclusive  = bool.Parse(configuration[$"MessageQueue:Consumers:{cnt}:Exclusive"]),
                    Exchange   = configuration[$"MessageQueue:Consumers:{cnt}:Exchange"],
                    RoutingKey = configuration[$"MessageQueue:Consumers:{cnt}:RoutingKey"]
                };
                switch (configuration[$"MessageQueue:Consumers:{cnt}:Queue"])
                {
                case "JudgeReport":
                    consumer.OnReceived = JudgeReport.JudgeReport_Received;
                    Task.Run(() => JudgeReport.QueueExecutor(cancellationTokenSource.Token));
                    break;
                }
                factory.CreateConsumer(consumer);
            }

            return(factory);
        }