Management API Introduction - EasyNetQ/EasyNetQ GitHub Wiki
EasyNetQ.Client.Management is a thin C# client for the RabbitMQ Management HTTP API. Before you can use it, you have to enable the RabbitMQ Management Plugin.
Using the management API it's possible to automate various management, monitoring and set-up tasks including:
- Declare, list and delete exchanges, queues, bindings, users, virtual hosts and permissions.
- Monitor queue length, message rates globally and per channel, data rates per connection, etc.
- Force close connections, purge queues.
To install EasyNetQ.Client.Management use NuGet:
PM> Install-Package EasyNetQ.Management.Client
To give an overview of the sort of things you can do with EasyNetQ.Client.Management, have a look at this code which walks through a scenario creating a new virtual host, user, some permissions and then creating an exchange, a queue, and binding them together. Finally it publishes a test message to the exchange and checks that it has arrived on the queue:
async Task Main()
{
var initial = new ManagementClient(new Uri("http://localhost:15672"), "guest", "guest");
var vhostName = "my_virtual_host";
// first create a new virtual host
await initial.CreateVhostAsync(vhostName);
var vhost = await initial.GetVhostAsync(vhostName);
// next create a user for that virtual host
var username = "mike";
var password = "topSecret";
await initial.CreateUserAsync(UserInfo.ByPassword(username, password).AddTag("administrator"));
// give the new user all permissions on the virtual host
await initial.CreatePermissionAsync(vhost, new PermissionInfo(username));
// now log in again as the new user
var management = new ManagementClient(new Uri("http://localhost:15672"), username, password);
// test that everything's OK
var isAlive = await management.IsAliveAsync(vhost);
Console.WriteLine(isAlive ? "I'm alive!" : "R.I.P.");
// create an exchange
var exchangeName = "my_exchange";
await management.CreateExchangeAsync(new ExchangeInfo(exchangeName, "direct"), vhost);
var exchange = await management.GetExchangeAsync(vhost, exchangeName);
// create a queue
var queueName = "my_queue";
await management.CreateQueueAsync(new QueueInfo(queueName), vhost);
var queue = await management.GetQueueAsync(vhost, queueName);
// bind the exchange to the queue
var routingKey = "my_routing_key";
await management.CreateQueueBindingAsync(exchange, queue, new BindingInfo(routingKey));
// publish a test message
await management.PublishAsync(exchange, new PublishInfo(routingKey, "Hello World!"));
// get any messages on the queue
var messages = await management.GetMessagesFromQueueAsync(queue, new GetMessagesFromQueueInfo(1, AckMode.AckRequeueFalse));
foreach (var message in messages)
{
Console.WriteLine("message.payload = {0}", message.Payload);
}
}