Connect FlowMQ with AMQP .NET Client
FlowMQ's native support for AMQP 0.9.1 makes it an excellent choice for .NET applications that require reliable, message-based communication. This guide will walk you through using the official RabbitMQ .NET Client to connect to FlowMQ, declare queues, publish messages, and consume them.
Prerequisites
Before you start, ensure you have the following:
- A running instance of FlowMQ.
- The FlowMQ AMQP listener endpoint (e.g., your-flowmq-host:5672).
- .NET 6 or later installed.
- A .NET development environment like Visual Studio or the .NET CLI.
Installation
Add the RabbitMQ .NET Client to your project using the NuGet package manager.
# Using .NET CLI
dotnet add package RabbitMQ.Client
# Or using Package Manager Console
Install-Package RabbitMQ.ClientConnecting to FlowMQ
First, you need to establish a connection to your FlowMQ broker and open a channel. All AMQP operations are performed on a channel.
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
using System.Threading.Tasks;
class AmqpDotnetClient
{
public static async Task Main(string[] args)
{
var factory = new ConnectionFactory() { HostName = "your-flowmq-host" };
await using var connection = await factory.CreateConnectionAsync();
await using var channel = await connection.CreateChannelAsync();
{
// ... declare, publish, and consume logic here ...
}
}
}Declaring a Queue
For simplicity, we'll declare a queue and publish messages directly to it using the default exchange. The default exchange will route messages to the queue whose name matches the message's routing key.
// Inside the 'using' block
await channel.QueueDeclareAsync(
queue: "hello-dotnet",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);Publishing a Message
Now you can publish a message. The routing key must match the queue name to be delivered correctly.
string message = "Hello FlowMQ from .NET!";
var body = Encoding.UTF8.GetBytes(message);
await channel.BasicPublishAsync(
exchange: "",
routingKey: "hello-dotnet",
mandatory: false,
basicProperties: new BasicProperties(),
body: body);
Console.WriteLine(" [x] Sent {0}", message);
Console.ReadLine();Consuming Messages
To consume messages, you can set up an EventingBasicConsumer, which will fire an event whenever a message is received.
var consumer = new AsyncEventingBasicConsumer(channel);
consumer.ReceivedAsync += (model, ea) =>
{
var receivedBody = ea.Body.ToArray();
var receivedMessage = Encoding.UTF8.GetString(receivedBody);
Console.WriteLine(" [x] Received {0}", receivedMessage);
return Task.CompletedTask;
};
await channel.BasicConsumeAsync(
queue: "hello-dotnet",
autoAck: true,
consumer: consumer);
Console.WriteLine(" [*] Waiting for messages. Press [enter] to exit.");Full Example
Here is a complete example that shows how to set up a publisher and a consumer.
Publisher (Publisher.cs)
using RabbitMQ.Client;
using System;
using System.Text;
using System.Threading.Tasks;
class Publisher
{
public static async Task Main()
{
var factory = new ConnectionFactory() { HostName = "your-flowmq-host" };
await using var connection = await factory.CreateConnectionAsync();
await using var channel = await connection.CreateChannelAsync();
await channel.QueueDeclareAsync(
queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
await channel.BasicPublishAsync(
exchange: "",
routingKey: "hello",
mandatory: false,
basicProperties: new BasicProperties(),
body: body);
Console.WriteLine(" [x] Sent {0}", message);
}
}Consumer (Consumer.cs)
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
using System.Threading.Tasks;
class Consumer
{
public static async Task Main()
{
var factory = new ConnectionFactory() { HostName = "your-flowmq-host" };
await using var connection = await factory.CreateConnectionAsync();
await using var channel = await connection.CreateChannelAsync();
await channel.QueueDeclareAsync(
queue: "hello",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
await channel.QueuePurgeAsync("hello");
var consumer = new AsyncEventingBasicConsumer(channel);
consumer.ReceivedAsync += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
return Task.CompletedTask;
};
await channel.BasicConsumeAsync(
queue: "hello",
autoAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}Additional Resources
- For more advanced scenarios and features, refer to the official RabbitMQ .NET Client Guide.
- Explore FlowMQ's advanced features for AMQP integration.