Esempio n. 1
0
        public static void Main2(string[] args)
        {
            ConnectionFactory factory    = TestUtilitiesClass.GetConnectionFactory();
            IConnection       connection = factory.CreateConnection();
            IModel            channel    = connection.CreateModel();

            channel.ExchangeDeclare(exchange: "topic_logs", type: "topic");
            string queueName = channel.QueueDeclare().QueueName;

            foreach (string bindingKey in args)
            {
                channel.QueueBind(queue: queueName,
                                  exchange: "topic_logs",
                                  routingKey: bindingKey);
            }

            Console.WriteLine("[*] Waiting for messages. To exit press CTRL+C");

            EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                byte[]          body            = ea.Body;
                RabbitMqMessage rabbitMqMessage = TestUtilitiesClass.ParseMessage(body);
                string          routingKey      = ea.RoutingKey;
                Console.WriteLine("[x] Received '{0}':'{1}'",
                                  routingKey,
                                  rabbitMqMessage.ToJsonString());
                TestUtilitiesClass.SleepProducer();
            };
            channel.BasicConsume(queue: queueName,
                                 autoAck: true,
                                 consumer: consumer);
        }
Esempio n. 2
0
        public static void Start(int count)
        {
            TaskFactory taskFactory = new TaskFactory();
            Action      action      = () =>
            {
                ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();
                using (IConnection connection = factory.CreateConnection())
                {
                    using (IModel channel = connection.CreateModel())
                    {
                        IBasicProperties properties = channel.CreateBasicProperties();
                        properties.Persistent = true;
                        channel.QueueDeclare(queue: "MessageAcknowledgment",
                                             durable: false,
                                             exclusive: false,
                                             autoDelete: false,
                                             arguments: null);
                        for (int i = 0; i < count; i++)
                        {
                            byte[]          body = TestUtilitiesClass.GetMessage(i.ToString());
                            RabbitMqMessage item = TestUtilitiesClass.ParseMessage(body);
                            channel.BasicPublish(exchange: "",
                                                 routingKey: "MessageAcknowledgment",
                                                 basicProperties: properties,
                                                 body: body);
                            Console.WriteLine("Queued {0}", item.ToJsonString());
                            Console.WriteLine("");
                            Thread.Sleep(TimeSpan.FromSeconds(0.5));
                        }
                    }
                }
            };

            taskFactory.StartNew(action);
        }
Esempio n. 3
0
        public static void Main2()
        {
            TestUtilitiesClass.PrintThreadId("Worker.Main2 (Consumer)");
            ConnectionFactory factory    = TestUtilitiesClass.GetConnectionFactory();
            IConnection       connection = factory.CreateConnection();
            IModel            channel    = connection.CreateModel();

            channel.QueueDeclare(queue: "task_queue",
                                 durable: true,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

            Console.WriteLine("[Worker] Waiting for messages.");

            EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                byte[]          body            = ea.Body;
                RabbitMqMessage rabbitMqMessage = TestUtilitiesClass.ParseMessage(body);

                Console.WriteLine("[Worker] Received {0} ThreadId: {1}", rabbitMqMessage.ToJsonString(), Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(TimeSpan.FromSeconds(1));
                Console.WriteLine("[Worker] Done");
                channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
            };

            channel.BasicConsume(queue: "task_queue",
                                 autoAck: false,
                                 consumer: consumer);
        }
Esempio n. 4
0
        public static void Main2()
        {
            TestUtilitiesClass.PrintThreadId("ReceiveLogs");
            ConnectionFactory factory    = TestUtilitiesClass.GetConnectionFactory();
            IConnection       connection = factory.CreateConnection();
            IModel            channel    = connection.CreateModel();

            channel.ExchangeDeclare(exchange: "logs", type: "fanout");
            string queueName = channel.QueueDeclare().QueueName;

            channel.QueueBind(queue: queueName,
                              exchange: "logs",
                              routingKey: "");
            Console.WriteLine(" [*] Waiting for logs.");
            EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                byte[]          body            = ea.Body;
                RabbitMqMessage rabbitMqMessage = TestUtilitiesClass.ParseMessage(body);
                Console.WriteLine("[ReceiveLogs]: {0}", rabbitMqMessage.ToJsonString());
                TestUtilitiesClass.SleepConsumer();
            };
            channel.BasicConsume(queue: queueName,
                                 autoAck: true,
                                 consumer: consumer);
        }
Esempio n. 5
0
        // consumer.Received event not firing when channel.BasicConsume is called
        // https://groups.google.com/forum/#!topic/rabbitmq-users/l0GQ4w3sYEU
        public static void HandleMessage(string readerName)
        {
            ConnectionFactory     factory    = TestUtilitiesClass.GetConnectionFactory();
            IConnection           connection = factory.CreateConnection();
            IModel                channel    = connection.CreateModel();
            EventingBasicConsumer consumer   = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                byte[]          body    = ea.Body;
                RabbitMqMessage message = TestUtilitiesClass.ParseMessage(body);
                Console.WriteLine($"[{readerName}] Received {message.ToJsonString()}");
                Console.WriteLine("");
                Thread.Sleep(TimeSpan.FromSeconds(1));
            };
            Console.WriteLine($"BasicConsume: {readerName} ThreadId: " + Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine("");
            channel.BasicConsume(queue: "round_robin", autoAck: true, consumer: consumer);
        }
Esempio n. 6
0
        public static void Main2(int count)
        {
            TestUtilitiesClass.PrintThreadId("EmitLog");
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            using (IConnection connection = factory.CreateConnection())
                using (IModel channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(exchange: "logs", type: ExchangeType.Fanout);
                    byte[]          body            = TestUtilitiesClass.GetMessage("PuttingItAllTogetherConsole");
                    RabbitMqMessage rabbitMqMessage = TestUtilitiesClass.ParseMessage(body);
                    for (int i = 0; i < count; i++)
                    {
                        channel.BasicPublish(exchange: "logs",
                                             routingKey: "",
                                             basicProperties: null,
                                             body: body);
                        Console.WriteLine("Sent {0}", rabbitMqMessage.ToJsonString());
                        TestUtilitiesClass.SleepProducer();
                    }
                }
        }
Esempio n. 7
0
        public static void Main2(int counter)
        {
            TestUtilitiesClass.PrintThreadId("FanoutLogsProducer.Main2");
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            using (IConnection connection = factory.CreateConnection())
                using (IModel channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare("logs", ExchangeType.Fanout);
                    channel.QueueDeclare("logA",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false);
                    channel.QueueDeclare("logB",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false);
                    channel.QueueDeclare("logC",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false);
                    channel.QueueBind("logA", "logs", "");
                    channel.QueueBind("logB", "logs", "");
                    channel.QueueBind("logC", "logs", "");
                    byte[]          message         = TestUtilitiesClass.GetMessage("FanoutLogsProducer");
                    RabbitMqMessage rabbitMqMessage = TestUtilitiesClass.ParseMessage(message);

                    for (int i = 0; i < counter; i++)
                    {
                        channel.BasicPublish(exchange: "logs",
                                             routingKey: "",
                                             basicProperties: null,
                                             body: message);
                        Console.WriteLine("Published: " + rabbitMqMessage.ToJsonString());
                        TestUtilitiesClass.SleepProducer();
                    }
                }
        }
Esempio n. 8
0
        public static void Create(int counter)
        {
            TestUtilitiesClass.PrintThreadId("TempQueueCreator.Create");
            ConnectionFactory connectionFactory = TestUtilitiesClass.GetConnectionFactory();
            IConnection       connection        = connectionFactory.CreateConnection();
            IModel            channel           = connection.CreateModel();
            string            queueName         = channel.QueueDeclare().QueueName;
            string            queueName2        = channel.QueueDeclare().QueueName;
            string            queueName3        = channel.QueueDeclare().QueueName;

            Console.WriteLine("Temp Queue Created: " + queueName);
            channel.ExchangeDeclare("tempExc", ExchangeType.Direct);
            channel.QueueBind(queueName, "tempExc", "routingKey1");
            channel.QueueBind(queueName2, "tempExc", "routingKey2");
            channel.QueueBind(queueName3, "tempExc", "routingKey1");
            for (int i = 0; i < counter; i++)
            {
                byte[]          message         = TestUtilitiesClass.GetMessage("TempQueueCreator");
                RabbitMqMessage rabbitMqMessage = TestUtilitiesClass.ParseMessage(message);
                channel.BasicPublish(exchange: "tempExc", routingKey: "routingKey1", basicProperties: null, body: message);
                Console.WriteLine("Sent {0}", rabbitMqMessage.ToJsonString());
            }
        }
Esempio n. 9
0
        public static void Main2(string[] args, int count)
        {
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            using (IConnection connection = factory.CreateConnection())
                using (IModel channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(exchange: "topic_logs", type: ExchangeType.Topic);
                    string routingKey = (args.Length > 0) ? args[0] : "anonymous.info";
                    byte[] message    = (args.Length > 1)
                    ? TestUtilitiesClass.GetMessage(string.Join(" ", args.Skip(1).ToArray()))
                    : TestUtilitiesClass.GetMessage("Hello World!");
                    for (int i = 0; i < count; i++)
                    {
                        channel.BasicPublish(exchange: "topic_logs",
                                             routingKey: routingKey,
                                             basicProperties: null,
                                             body: message);
                        RabbitMqMessage rabbitMqMessage = TestUtilitiesClass.ParseMessage(message);
                        Console.WriteLine("[x] Sent '{0}':'{1}'", routingKey, rabbitMqMessage.ToJsonString());
                        TestUtilitiesClass.SleepProducer();
                    }
                }
        }