Skip to content

Instantly share code, notes, and snippets.

@mplacona
Created July 29, 2012 12:43
Show Gist options
  • Save mplacona/3198479 to your computer and use it in GitHub Desktop.
Save mplacona/3198479 to your computer and use it in GitHub Desktop.
RabbitMQ C# "Read from queue example"
// Declare the queue name
string QueueName = "QTransactions";
// Create a new connection factory for the queue
var factory = new ConnectionFactory();
// Because Rabbit is installed locally, we can run it on localhost
factory.HostName = "127.0.0.1";
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
// When reading from a persistent queue, you need to tell that to your consumer
const bool durable = true;
channel.QueueDeclare(QueueName, durable, false, false, null);
var consumer = new QueueingBasicConsumer(channel);
// turn auto acknowledge off so we can do it manually. This is so we don't remove items from the queue until we're perfectly happy
const bool autoAck = false;
channel.BasicConsume(QueueName, autoAck, consumer);
System.Console.WriteLine(" [*] Waiting for messages." +
"To exit press CTRL+C");
while (true)
{
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
byte[] body = ea.Body;
string message = System.Text.Encoding.UTF8.GetString(body);
System.Console.WriteLine(" [x] Processing {0}", message);
// Acknowledge message received and processed
System.Console.WriteLine(" Processed ", message);
channel.BasicAck(ea.DeliveryTag, false);
}
}
@sgaikwad
Copy link

What does true means here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment