Пример #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("VowelConsRater");
            const string exchange       = "vowel-cons-counter";
            const string outputExchange = "text-rank-calc";
            const string key            = "vowel-cons-task";
            var          factory        = new ConnectionFactory()
            {
                HostName = "localhost"
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(exchange: exchange,
                                            type: "direct");
                    var queueName = "rank-task";
                    channel.QueueDeclare(queue: queueName,
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    channel.QueueBind(queue: queueName,
                                      exchange: exchange,
                                      routingKey: key);

                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var body    = ea.Body;
                        var message = Encoding.UTF8.GetString(body);

                        message = message.Replace("text_", "");

                        var msgArgs = Regex.Split(message, ":");
                        if (msgArgs.Length == 4 && msgArgs[0] == "VowelConsCounted")
                        {
                            VowelConsCounted data = new VowelConsCounted(msgArgs[1], msgArgs[2], msgArgs[3]);
                            float            rank = CalculateRank(data.Vowels, data.Cons);
                            SetRankInDbById(data.Id, rank);
                            SendRankToExchange(data.Id, rank, outputExchange, channel);
                        }
                    };
                    channel.BasicConsume(queue: queueName,
                                         autoAck: true,
                                         consumer: consumer);
                    Console.ReadLine();
                }
        }
Пример #2
0
        private static void RabbitListener()
        {
            var factory = new ConnectionFactory()
            {
                HostName = HOST_NAME
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(exchange: EXCHANGE_NAME,
                                            type: "direct");
                    channel.QueueDeclare(queue: QUEUE_NAME,
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);


                    channel.QueueBind(queue: QUEUE_NAME,
                                      exchange: EXCHANGE_NAME,
                                      routingKey: ROUTING_KEY);
                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var body    = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        var msgArgs = Regex.Split(message, ":");
                        if (msgArgs.Length == 4 && msgArgs[0] == "VowelConsCounted")
                        {
                            VowelConsCounted data = new VowelConsCounted(msgArgs[1], msgArgs[2], msgArgs[3]);
                            float            rank = CalculateRank(data.Vowels, data.Cons);
                            SetRankInDbById(data.Id, rank);
                            Console.WriteLine("ID: " + data.Id + " Rank: " + rank.ToString());
                            AddRankToExchange(data.Id, rank, channel);
                        }
                    };

                    channel.BasicConsume(queue: QUEUE_NAME,
                                         autoAck: true,
                                         consumer: consumer);
                    Console.ReadLine();
                }
        }