Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using RabbitMQ;
  8. using RabbitMQ.Client;
  9. using RabbitMQ.Client.Events;
  10.  
  11. namespace Producent
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             var factory = new ConnectionFactory()
  18.             {
  19.                 UserName = "guest",
  20.                 Password = "guest",
  21.                 HostName = "localhost",
  22.                 VirtualHost = "103057"
  23.             };
  24.             var connection = factory.CreateConnection();
  25.             var channel = connection.CreateModel();
  26.            
  27.             string message = "my message";
  28.             var body = Encoding.UTF8.GetBytes(message);
  29.             IBasicProperties prop = channel.CreateBasicProperties();
  30.             prop.Persistent = true;
  31.             // ZAD 3
  32.  
  33.             // consume response from consumer
  34.             string replyQueueName = channel.QueueDeclare().QueueName;
  35.             EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
  36.             channel.BasicConsume(replyQueueName, true, consumer);
  37.  
  38.             // required for response
  39.             prop.ReplyTo = replyQueueName;
  40.             var corrId = Guid.NewGuid().ToString();
  41.             prop.CorrelationId = corrId;
  42.  
  43.             for (int i = 0; i < 50; i++)
  44.             {
  45.                 channel.BasicPublish("", "message_queue", prop, body);
  46.             }
  47.  
  48.  
  49.             Thread.Sleep(2000);
  50.  
  51.            
  52.  
  53.             consumer.Received += (model, ea) =>
  54.             {
  55.                 if (ea.BasicProperties.CorrelationId == corrId)
  56.                 {
  57.                     Console.Write("\n{0}\n", Encoding.UTF8.GetString(ea.Body), ConsoleColor.Blue);
  58.                 }
  59.             };
  60.  
  61.  
  62.             // ZAD 3
  63.  
  64.             Console.ReadKey();
  65.             channel.Close();
  66.             connection.Close();
  67.  
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement