Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using RabbitMQ.Client;
- using RabbitMQ.Client.Events;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace prod
- {
- class Program
- {
- static void Main(string[] args)
- {
- var factory = new ConnectionFactory()
- {
- UserName = "pfolihar",
- Password = "NrX1IaunZAQxYQ9enonUZoyYDu2aEN8O",
- HostName = "bulldog.rmq.cloudamqp.com",
- VirtualHost = "pfolihar"
- };
- string queueName = "message_queue";
- using (var connection = factory.CreateConnection())
- using (var channel = connection.CreateModel())
- {
- channel.QueueDeclare(queueName, false, false, false, null);
- for (int i = 0; i < 10; ++i)
- {
- IBasicProperties properties = channel.CreateBasicProperties();
- properties.Headers = new Dictionary<string, object>();
- properties.Headers.Add("num", i);
- string message = "Mssg " + i.ToString();
- var body = Encoding.UTF8.GetBytes(message);
- string replyQueueName = channel.QueueDeclare().QueueName;
- EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
- channel.BasicConsume(replyQueueName, true, consumer);
- properties.ReplyTo = replyQueueName;
- var corrId = Guid.NewGuid().ToString();
- properties.CorrelationId = corrId;
- channel.BasicPublish("", queueName, properties, body);
- Console.WriteLine("Sending: " + message);
- consumer.Received += (model, ea) =>
- {
- if (ea.BasicProperties.CorrelationId == corrId)
- {
- Console.WriteLine("\n" + Encoding.UTF8.GetString(ea.Body));
- }
- };
- }
- Console.ReadKey();
- }
- }
- }
- }
- using RabbitMQ.Client;
- using RabbitMQ.Client.Events;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace odb_1
- {
- class Program
- {
- static void Main(string[] args)
- {
- var factory = new ConnectionFactory()
- {
- UserName = "pfolihar",
- Password = "NrX1IaunZAQxYQ9enonUZoyYDu2aEN8O",
- HostName = "bulldog.rmq.cloudamqp.com",
- VirtualHost = "pfolihar"
- };
- string queueName = "message_queue";
- using (var connection = factory.CreateConnection())
- using (var channel = connection.CreateModel())
- {
- channel.BasicQos(0, 1, true);
- var consumer = new EventingBasicConsumer(channel);
- consumer.Received += (model, ea) =>
- {
- ((EventingBasicConsumer)model).Model.BasicAck(ea.DeliveryTag, true);
- var body = ea.Body;
- var message = Encoding.UTF8.GetString(body);
- Console.WriteLine("Received: " + message);
- int num = (int)ea.BasicProperties.Headers["num"];
- Console.WriteLine("13+" + num.ToString());
- System.Threading.Thread.Sleep(2000);
- var responseBytes = Encoding.UTF8.GetBytes("13+" + num.ToString() + " = " + (13 + num));
- var replyProps = channel.CreateBasicProperties();
- replyProps.CorrelationId = ea.BasicProperties.CorrelationId;
- channel.BasicPublish("", ea.BasicProperties.ReplyTo, true, replyProps, responseBytes);
- };
- while (true)
- {
- channel.BasicConsume(queueName, false, consumer);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement