Skip to content

Instantly share code, notes, and snippets.

@mplacona
Created July 28, 2012 21:34
Show Gist options
  • Save mplacona/3194895 to your computer and use it in GitHub Desktop.
Save mplacona/3194895 to your computer and use it in GitHub Desktop.
RabbitMQ C# "Add to queue example"
//Declare queue name. This can be anything you like
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 (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel())
{
// mark all messages as persistent
const bool durable = true;
channel.QueueDeclare(QueueName, durable, false, false, null);
// Set delivery mode (1 = non Persistent | 2 = Persistent)
IBasicProperties props = channel.CreateBasicProperties();
props.DeliveryMode = 2;
string msg = args[0];
byte[] body = System.Text.Encoding.UTF8.GetBytes(msg);
channel.BasicPublish("", QueueName, props, body);
Console.WriteLine(" [x] Sent {0}", msg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment