Queue4GP.RabbitMQ
1.0.2
See the version list below for details.
dotnet add package Queue4GP.RabbitMQ --version 1.0.2
NuGet\Install-Package Queue4GP.RabbitMQ -Version 1.0.2
<PackageReference Include="Queue4GP.RabbitMQ" Version="1.0.2" />
paket add Queue4GP.RabbitMQ --version 1.0.2
#r "nuget: Queue4GP.RabbitMQ, 1.0.2"
// Install Queue4GP.RabbitMQ as a Cake Addin
#addin nuget:?package=Queue4GP.RabbitMQ&version=1.0.2
// Install Queue4GP.RabbitMQ as a Cake Tool
#tool nuget:?package=Queue4GP.RabbitMQ&version=1.0.2
Queue4GP
This library is useful when you need to manage a simple queue. It is not intended for anybody needs a full queue management.
Right now it works only with RabbitMQ.
NuGet
To install the package run the following command on the Package Manager Console:
PM> Install-Package Queue4GP.RabbitMQ
Prerequisites
A RabbitMQ server is required. The fastest way to configure it is to use a Docker container:
docker run -d --hostname my-rabbit --name my-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management
This command will install a new docker container named my-rabbit that uses the following port:
- 5672: used by the library for send/recive messages
- 15672: used for web management. Browe to http://localhost:15672 for see the RabbitMQ management website (guest/guest are the default credentials)
Use the library
Console application
The library can be used in a console application. In this sample you can see how much easy is to use the library.
class Program
{
static void Main(string[] args)
{
// Rabbit MQ configuration
var config = new QueueConfiguration
{
Host = "localhost",
Port = 5672,
User = "guest",
Password = "guest"
};
// Istantiate a subscriber
using (var subscriber = new QueueSubscriber<MyMessage>(config, new MyHandler()))
{
// Starts listening
subscriber.StartListening();
// Istantiate a subscriber
using (var publisher = new MyPublisher(config))
{
// Running while put an empty message
while (true)
{
Console.WriteLine("Write message text (empty for exit)");
// Creates a message
var msg = new MyMessage { MessageId = DateTime.Now.ToLongDateString() };
msg.Text = Console.ReadLine();
if (string.IsNullOrEmpty(msg.Text)) break;
// Publish it to the queue (and the subscriber will write it on the console)
publisher.Send(msg);
}
}
// Stop listening
subscriber.StopListening();
}
}
}
/// <summary>
/// Message to send/receive
/// </summary>
class MyMessage : IQueueMessage
{
/// <summary>
/// Message id
/// </summary>
public string MessageId { get; set; }
/// <summary>
/// Message text
/// </summary>
public string Text { get; set; }
}
/// <summary>
/// Subscriber handler. Will be called any time a message arrives to the queue
/// </summary>
class MyHandler : IQueueHandler<MyMessage>
{
public void HandleMessage(MyMessage message)
{
// Handles the message
Console.WriteLine($"R: {message.MessageId} - {message.Text}");
}
}
/// <summary>
/// Publisher: sends messages to the queue
/// </summary>
class MyPublisher : QueuePublisher<MyMessage>
{
public MyPublisher(QueueConfiguration configuration) : base(configuration)
{
}
}
ASP.NET Core
The library can be used on ASP.NET core also. You simply need to set the connection info on the appsettings.json file:
"Queue4GP": {
"Host": "localhost",
"Port": 5672,
"User": "guest",
"Password": "guest"
}
Then you have to register this configuration on the standard IOC:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// Configure the queue settings
services.Configure<QueueConfiguration>(options => Configuration.GetSection("Queue4GP").Bind(options));
}
Finally, in the controller you want to send the message, you need to:
- Add the IOption<QueueConfiguration> for read the configuration from the appsettings.json file
- Create the publisher
- Send the message
In this example you can see how to send a MyMessage to the queue:
[Route("api/[controller]")]
[ApiController]
public class QueueController : ControllerBase
{
public QueueController(IOptions<QueueConfiguration> config)
{
QueueConfiguration = config.Value;
}
private QueueConfiguration QueueConfiguration { get; }
// GET api/values
[HttpGet("SendMessage")]
public ActionResult SendMessage([FromQuery]string message)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
if (string.IsNullOrEmpty(message)) return BadRequest("message is mandatory");
using (var publisher = new MyPublisher(QueueConfiguration))
{
var msg = new MyMessage
{
MessageId = Guid.NewGuid().ToString(),
Text = message
};
publisher.Send(msg);
}
return Ok();
}
}
/// <summary>
/// Publisher: sends messages to the queue
/// </summary>
class MyPublisher : QueuePublisher<MyMessage>
{
public MyPublisher(QueueConfiguration configuration) : base(configuration)
{
}
}
/// <summary>
/// Message to send/receive
/// </summary>
class MyMessage : IQueueMessage
{
/// <summary>
/// Message id
/// </summary>
public string MessageId { get; set; }
/// <summary>
/// Message text
/// </summary>
public string Text { get; set; }
}
Built With
- RabbitMQ.Client - Used for comunication with the RabbitMQ server
- Jon.NET (Newtonsoft) - Used to serialize/deserialize queue messages
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
- Microsoft.Extensions.Options (>= 2.2.0)
- Newtonsoft.Json (>= 12.0.1)
- RabbitMQ.Client (>= 5.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.3 | 1,252 | 10/10/2019 |
First release of the library