CloudAMQP at AppHarbor Getting started

First encounter Publish messages

Begin to install the RabbitMQ client library, preferably via NuGet, then follow the code example to the right:

// First we need a ConnectionFactory
ConnectionFactory connFactory = new ConnectionFactory
{
    // AppSettings["CLOUDAMQP_URL"] contains the connection string
    // when you've added the CloudAMQP Addon
    Uri = ConfigurationManager.AppSettings["CLOUDAMQP_URL"]
};

// Open up a connection and a channel (a connection may have many channels)
using (var conn = connFactory.CreateConnection())
using (var channel = conn.CreateModel()) // Note, don't share channels between threads
{
    // The message we want to put on the queue
    var message = DateTime.Now.ToString("F");

    // the data put on the queue must be a byte array
    var data = Encoding.UTF8.GetBytes(message);

    // ensure that the queue exists before we publish to it
    channel.QueueDeclare("queue1", false, false, false, null);

    // publish to the "default exchange", with the queue name as the routing key
    channel.BasicPublish("", "queue1", null, data);
}

Put it to use Subscribe

Now lets take a look how to consume messages arriving to the queue.

using (var conn = connFactory.CreateConnection())
using (var channel = conn.CreateModel())
{
    // ensure that the queue exists before we access it
    channel.QueueDeclare("queue1", false, false, false, null);

    // subscribe to the queue
    var subscription = new Subscription(channel, "queue1");
    while (true)
    {
        // this will block until a messages has landed in the queue
        var message = subscription.Next();

        // deserialize the message body
        var text = Encoding.UTF8.GetString(message.Body);

        // do something useful with the data..
        Console.WriteLine(text);

        // ack the message, ie. confirm that we have processed it
        // otherwise it will be requeued a bit later
        subscription.Ack(message);
    }
}

The management interface

RabbitMQ has a nice managment interface with great interspection capabilites.

You can show the current message rate, which queues and exchanges you have, and the bindings between them.

The management interface