Ejemplo n.º 1
0
        public static void Main2(int count)
        {
            TestUtilitiesClass.PrintThreadId("NewTask.Main2 (Producer)");
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            using (IConnection connection = factory.CreateConnection())
                using (IModel channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "task_queue",
                                         durable: true,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    byte[] message = TestUtilitiesClass.GetMessage("FairDispatchConsole");

                    IBasicProperties properties = channel.CreateBasicProperties();
                    properties.Persistent = true;

                    for (int i = 0; i < count; i++)
                    {
                        channel.BasicPublish(exchange: "",
                                             routingKey: "task_queue",
                                             basicProperties: properties,
                                             body: message);
                        Console.WriteLine("[FairDispatchConsole] Sent {0} ThreadId: {1}", message, Thread.CurrentThread.ManagedThreadId);
                        Thread.Sleep(TimeSpan.FromSeconds(0.5));
                    }
                }
        }
Ejemplo n.º 2
0
        public static void Main2()
        {
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            using (IConnection connection = factory.CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "hello",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
                    int counter = 1;
                    consumer.Received += (model, ea) =>
                    {
                        byte[] body    = ea.Body;
                        string message = Encoding.UTF8.GetString(body);
                        Console.WriteLine(" [x-con1] Received {0}, counter: {1}, threadId: {2}", message, counter, Thread.CurrentThread.ManagedThreadId);
                        counter++;
                    };

                    channel.BasicConsume(queue: "hello", autoAck: false, consumer: consumer);

                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public static void Main2()
        {
            TestUtilitiesClass.WriteLine("RpcServer Main2 started.");
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            IConnection connection = factory.CreateConnection();
            IModel      channel    = connection.CreateModel();

            TestUtilitiesClass.WriteLine("RpcServer Main2 QueueDeclare: rpc_queue");

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

            channel.BasicQos(0, 1, false);

            EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                TestUtilitiesClass.WriteLine("RpcServer Main2 Received message");
                string response = null;

                byte[]           body       = ea.Body;
                IBasicProperties props      = ea.BasicProperties;
                IBasicProperties replyProps = channel.CreateBasicProperties();
                replyProps.CorrelationId = props.CorrelationId;
                TestUtilitiesClass.WriteLine("RpcServer Main2 CorrelationId: " + props.CorrelationId);

                try
                {
                    string message = Encoding.UTF8.GetString(body);
                    int    n       = int.Parse(message);
                    Console.WriteLine("RpcServer Main2 fib({0})", message);
                    response = fib(n).ToString();
                }
                catch (Exception e)
                {
                    TestUtilitiesClass.WriteLine("RpcServer Main2 Exception " + e.Message);
                    response = "";
                }
                finally
                {
                    TestUtilitiesClass.WriteLine("RpcServer Main2 finally");
                    byte[] responseBytes = Encoding.UTF8.GetBytes(response);
                    TestUtilitiesClass.WriteLine("RpcServer Main2 finally BasicPublish routingKey: " + props.ReplyTo);
                    channel.BasicPublish(exchange: "", routingKey: props.ReplyTo,
                                         basicProperties: replyProps, body: responseBytes);
                    channel.BasicAck(deliveryTag: ea.DeliveryTag,
                                     multiple: false);
                }
            };
            TestUtilitiesClass.WriteLine("RpcServer Main2 BasicConsume queue: rpc_queue");
            channel.BasicConsume(queue: "rpc_queue",
                                 autoAck: false,
                                 consumer: consumer);
            Console.WriteLine("RpcServer Main2 Awaiting RPC requests");
        }
Ejemplo n.º 5
0
        public static void Main2()
        {
            string[] args    = { "a", "b", "c" };
            string   message = GetMessage(args);

            byte[]            body    = Encoding.UTF8.GetBytes(message);
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            using (IConnection connection = factory.CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    IBasicProperties properties = channel.CreateBasicProperties();
                    properties.Persistent = true;
                    channel.QueueDeclare(queue: "task_queue",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);
                    channel.BasicPublish(exchange: "",
                                         routingKey: "task_queue",
                                         basicProperties: properties,
                                         body: body);
                    Console.WriteLine(" [x] Worker-Sent {0}", body);
                }
            }
        }
Ejemplo n.º 6
0
        public RpcClient()
        {
            TestUtilitiesClass.WriteLine("RpcClient constructor started");
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            connection     = factory.CreateConnection();
            channel        = connection.CreateModel();
            replyQueueName = channel.QueueDeclare().QueueName;
            consumer       = new EventingBasicConsumer(channel);
            TestUtilitiesClass.WriteLine("RpcClient replyQueueName: " + replyQueueName);
            props = channel.CreateBasicProperties();
            string correlationId = Guid.NewGuid().ToString();

            TestUtilitiesClass.WriteLine("RpcClient correlationId: " + correlationId);
            props.CorrelationId = correlationId;
            props.ReplyTo       = replyQueueName;

            consumer.Received += (model, ea) =>
            {
                TestUtilitiesClass.WriteLine("RpcClient Received");
                byte[] body     = ea.Body;
                string response = Encoding.UTF8.GetString(body);
                if (ea.BasicProperties.CorrelationId == correlationId)
                {
                    respQueue.Add(response);
                }
            };
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        private static void SendMessage(int count)
        {
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            using (IConnection connection = factory.CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "hello",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);
                    for (int i = 0; i < count; i++)
                    {
                        string message = "Publisher Message " + i;
                        byte[] body    = Encoding.UTF8.GetBytes(message);

                        channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);

                        Console.WriteLine(" [x] Sent {0}", message);
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public static void Main2()
        {
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            using (IConnection connection = factory.CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "hello",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

                    consumer.Received += (model, ea) =>
                    {
                        byte[] body    = ea.Body;
                        string message = Encoding.UTF8.GetString(body);
                        Console.WriteLine(" [x] Received {0}", message);

                        int dots = message.Split('.').Length - 1;
                        Thread.Sleep(dots * 1000);

                        Console.WriteLine(" [x] Done");
                    };

                    channel.BasicConsume(queue: "task_queue", autoAck: true, consumer: consumer);

                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
            }
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
 static void Main(string[] args)
 {
     TestUtilitiesClass.WriteLine("Program.Main Started.");
     RunClient();
     RunServer();
     TestUtilitiesClass.WriteLine("main finished.");
     Console.ReadLine();
 }
Ejemplo n.º 13
0
        public static void Main2()
        {
            RpcClient rpcClient = new RpcClient();

            TestUtilitiesClass.WriteLine("Rpc Main2 Requesting fib(30)");
            string response = rpcClient.Call("5");

            Console.WriteLine("Rpc Main2 Got '{0}'", response);
            rpcClient.Close();
        }
Ejemplo n.º 14
0
        ///

        /// Assumes only valid positive integer input.
        /// Don't expect this one to work for big numbers, and it's
        /// probably the slowest recursive implementation possible.
        ///
        private static int fib(int n)
        {
            TestUtilitiesClass.WriteLine("RPCServer fib is called: " + n);
            TestUtilitiesClass.Sleep(0.5);
            if (n == 0 || n == 1)
            {
                return(n);
            }

            return(fib(n - 1) + fib(n - 2));
        }
Ejemplo n.º 15
0
 public string Call(string message)
 {
     TestUtilitiesClass.WriteLine("RpcClient Call message: " + message);
     byte[] messageBytes = Encoding.UTF8.GetBytes(message);
     TestUtilitiesClass.WriteLine("RpcClient BasicPublish routingKey: rpc_queue");
     channel.BasicPublish(
         exchange: "",
         routingKey: "rpc_queue",
         basicProperties: props,
         body: messageBytes);
     TestUtilitiesClass.WriteLine("RpcClient BasicConsume queue: " + replyQueueName);
     channel.BasicConsume(
         consumer: consumer,
         queue: replyQueueName,
         autoAck: true);
     // https://docs.microsoft.com/en-us/dotnet/standard/collections/thread-safe/blockingcollection-overview
     return(respQueue.Take());
 }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
0
        public static void Main2(string severity)
        {
            TestUtilitiesClass.PrintThreadId("EmitLogDirect");
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            using (IConnection connection = factory.CreateConnection())
                using (IModel channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(exchange: "direct_logs", type: ExchangeType.Direct);

                    byte[]          body            = TestUtilitiesClass.GetMessage("EmitLogDirect " + severity);
                    RabbitMqMessage rabbitMqMessage = TestUtilitiesClass.ParseMessage(body);
                    channel.BasicPublish(exchange: "direct_logs",
                                         routingKey: severity,
                                         basicProperties: null,
                                         body: body);
                    Console.WriteLine("[EmitLogDirect] Sent '{0}':'{1}'", severity, rabbitMqMessage);
                    TestUtilitiesClass.SleepProducer();
                }
        }
Ejemplo n.º 18
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();
                    }
                }
        }
Ejemplo n.º 19
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();
                    }
                }
        }
Ejemplo n.º 20
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());
            }
        }
Ejemplo n.º 21
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();
                    }
                }
        }
Ejemplo n.º 22
0
 static void Main(string[] args)
 {
     TempQueueCreator.Create(10);
     TestUtilitiesClass.PrintThreadId("Program.Main");
     Console.ReadLine();
 }
Ejemplo n.º 23
0
 static void Main(string[] args)
 {
     FanoutLogsProducer.Main2(10);
     TestUtilitiesClass.PrintThreadId("Program.Main");
     Console.ReadLine();
 }
Ejemplo n.º 24
0
 public void Close()
 {
     TestUtilitiesClass.WriteLine("RpcClient Close");
     connection.Close();
 }