Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void WorkWithRMQ(bool isReceiver)
- {
- string Exchange = "my.special";
- string Queue = "my.special.outq2";
- string RoutingKey = "my.special.outq2";
- var factory = new ConnectionFactory()
- {
- HostName = "RabbitMQ.server",
- Port = 15672,
- UserName = "userName",
- Password = "p@ssWorD",
- VirtualHost = "someVhost",
- Protocol = Protocols.AMQP_0_9_1,
- };
- using (var connection = factory.CreateConnection())
- using (var channel = connection.CreateModel())
- {
- channel.ExchangeDeclare(Exchange, ExchangeType.Topic, true);
- channel.QueueDeclare(Queue, true, false, false, new Dictionary<string, object> { { "x-ha-policy", "all" } });
- channel.QueueBind(Queue, Exchange, RoutingKey);
- //channel.BasicReturn += channel_BasicReturn;
- if (isReceiver)
- {
- Console.WriteLine("Receiver started: \r\n");
- var consumer = new EventingBasicConsumer(channel);
- consumer.Received += (model, ea) =>
- {
- var body = ea.Body;
- var message = Encoding.UTF8.GetString(body);
- Console.WriteLine(" [x] Received {0}", message);
- };
- channel.BasicConsume(queue: Queue, noAck: true, consumer: consumer);
- }
- else
- {
- Console.WriteLine("Type your message to Send: \r\n");
- string message = Console.ReadLine().Trim();
- var body = Encoding.UTF8.GetBytes(message);
- channel.BasicPublish(exchange: Exchange,
- routingKey: RoutingKey,
- basicProperties: null,
- body: body);
- }
- Console.WriteLine(" Press [enter] to exit.");
- Console.ReadLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement