RonakThakkar
7/4/2018 - 1:41 PM

RabbitMQ

All code snippet related to RabbitMQ
Console.CancelKeyPress += new ConsoleCancelEventHandler((object obj, ConsoleCancelEventArgs args) => { Environment.Exit(0); });
Console.Write("Press CTRL + C to close.\n");

ConnectionFactory factory = new ConnectionFactory() { HostName = "localhost", UserName = "admin", Password = "admin", VirtualHost = "/" };
//factory.AutomaticRecoveryEnabled = true;

using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel())
{
    string exchangeName = "demo";
    channel.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Headers, durable: true);

    do
    {
        Console.WriteLine("Enter a message to publish");
        string message = Console.ReadLine();


        byte[] body = Encoding.UTF8.GetBytes(message);

        BasicProperties properties = new BasicProperties
        {
            Headers = new Dictionary<string, object>()
        };
        properties.DeliveryMode = 2;  // 1 = Transient, 2 = Persistent

        channel.BasicPublish(exchange: exchangeName,
            routingKey: "",
            basicProperties: properties,
            body: body);
    }
    while (true);
}