Skip to main content

AMQP

AMQP (Advanced Message Queuing Protocol) is an open standard for messaging middleware that enables applications to communicate with each other by sending messages in a reliable and interoperable way. It is a protocol designed to provide a robust messaging infrastructure for distributed systems, enabling asynchronous communication between different components.

To work with AMQP protocol, NBomber provides NBomber.AMQP plugin that includes functionality for sending and receiving messages, including tracking of data transfer and status codes.

warning

This package is experimental and might be subject to breaking API changes in the future. While we intend to keep experimental packages as stable as possible, we may need to introduce breaking changes.

info

To install NBomber.AMQP package you should execute the following dotnet command:

build NuGet

dotnet add package NBomber.AMQP

AMQP API

AMQP plugin provides a wrapper over the popular library RabbitMQ.Client. The wrapper implements basic methods for publishing and receiving messages.

var scenario = Scenario.Create("amqp_scenario", async ctx =>
{
using var amqpClient = new AmqpClient(channel);

// Declares an AMQP exchange and queue, then binds the queue to the exchange using the specified routing key.
await amqpClient.DeclareQueue(exchange: "myExchange", exchangeType: ExchangeType.Direct, queue: queueName, routingKey: queueName);

// Subscribes to the specified AMQP queue by adding a consumer.
await amqpClient.Subscribe(queue: "queueName");

// Publishes a message to the specified AMQP exchange using the given routing key.
await amqpClient.Publish(exchange: "myExchange", routingKey: "routingKey", body: payload);

// Awaits and receives a message from the subscribed queue.
var response = await amqpClient.Receive();

// Disconnect the current client from the broker
await amqpClient.Disconnect();

// Gets the total number of messages received by the client.
var receivedCount = amqpClient.MsgReceivedCount

return Response.Ok();
});

Original RabbitMQ.Client

NBomber AMQP plugin is basically a wrapper over the popular library RabbitMQ.Client. If you need to work with the original API from RabbitMQ.Client library, you can use public AmqpChannel property. All native methods are available for usage.

using var client = new AmqpClient(channel);

var originalClient = client.AmqpChannel;

await originalClient.ExchangeDeclareAsync(exchange: "myExchange", type: ExchangeType.Direct);

Examples

Here you will find some useful examples of working with the AMQP protocol and NBomber to cover different workloads.

Ping Pong Example

This is a basic example meant to demonstrate the API usage. In this example, we create an AMQP client that:

  1. Connects to the broker
  2. Subscribes to its own queue (self topic) using the scenario instance ID (ctx.ScenarioInfo.InstanceId)
  3. Publishes a message to self topic
  4. Awaits and receives a message from the self topic
  5. Disconnects
var payload = Data.GenerateRandomBytes(200);
var factory = new ConnectionFactory { HostName = "localhost" };

var scenario = Scenario.Create("ping_pong_scenario", async ctx =>
{
var connect = await Step.Run("connect", ctx, async () =>
{
var connection = await factory.CreateConnectionAsync();
var channel = await connection.CreateChannelAsync();

var amqpClient = new AmqpClient(channel);
return Response.Ok(payload: amqpClient);
});

using var amqpClient = connect.Payload.Value;