Advertisement
Guest User

Sender rabbitMQ

a guest
Nov 8th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2. using RabbitMQ.Client;
  3. using System.Text;
  4.  
  5. namespace Send {
  6.  
  7.     public static class IModelExtensions {
  8.         public static void Publish(this IModel channel, string msg) {
  9.             byte [] body = Encoding.UTF8.GetBytes(msg);
  10.             channel.BasicPublish("", "hello", null, body);
  11.         }
  12.     }
  13.     class Send {
  14.         static ConnectionFactory CreateConnection() => new ConnectionFactory() {
  15.             HostName = "192.168.15.40",
  16.             UserName = "rfid",
  17.             Password = "rfid"
  18.         };
  19.  
  20.         static void Main(string[] args) {
  21.             Console.WriteLine("Press any key to start: ");
  22.             Console.ReadKey();
  23.  
  24.             var factory = CreateConnection();
  25.  
  26.             using(IConnection connection = factory.CreateConnection()) {
  27.                 using(IModel channel = connection.CreateModel()) {
  28.                     channel.QueueDeclare(
  29.                         queue: "hello",
  30.                         durable: false,
  31.                         exclusive: false,
  32.                         autoDelete: false,
  33.                         arguments: null
  34.                     );
  35.  
  36.                     for(int i = 0; i < 100; i++) {
  37.                         string msg = Guid.NewGuid().ToString();
  38.  
  39.                         channel.Publish(msg);
  40.                         Console.WriteLine($"[-] Sent {i+1} {msg}");
  41.                     }
  42.  
  43.                 }
  44.             }
  45.  
  46.             Console.WriteLine("Press [Enter] to exit.");
  47.             Console.ReadLine();
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement