Message queue is a form of asynchronous service-to-service communication. Messages are stored on the queue until they are processed and deleted and each message is processed only once, by a single worker (or consumer).
1. Worker or Consumer
A component that receives and processes messages from the message queue
2. Sender or Producer
A component that creates and sends messages to the message queue
3. FIFO (First-In, First-Out)
The principle where the first message to enter the queue is the first to be processed
4. Message Time-To-Live
Defines how long a message can remain in the queue before being deleted
5. Message broker
An intermediary software component that facilitates communication between different systems or different threads within the same system. Ex: Azure Service Bus, Apache Kafka and RabbitMQ
⏳ Long processing time
When there are tasks that require a significant amount of time to complete, put it in a queue allow the system to continue operating as usual.
💥 Actions prone to failure
When an action has a high probability of failure, put it in a queue can be a good strategy for handling these failures asynchronously. Ex: If the action fails, it can be placed back in the queue to retry later without broke the normal flow of the system.
This simple project simulates a messaging service using Azure Service Bus. Here you'll find:
message-queue-project
|
├── sender
│ ├── sender.ts
| ├── index.ts
| ├── interface.ts
| ├── routes.ts
| ├── service.ts
| ├── .env-example
| ├── package-lock.json
│ └── package.json
|
├── worker
│ ├── commons
│ | └── ServiceBusCommon.cs
│ |
│ ├── dtos
│ | ├── MessageBodyDto.cs
│ | └── MessageResponseDto.cs
| |
│ ├── Program.cs
│ ├── Startup.cs
│ ├── ServiceBusController.cs
│ ├── ServiceBusMessageProcessor.cs
│ ├── ServiceBusMessageReceiver.cs
| ├── appsettings-example.json
| ├── messages.txt
│ └── worker.csproj
|
└── README.md
Create and send messages to the queue.
📰 Uses Spaceflight News API to retrieve articles and create a message - it enqueue five articles
☝️ Create a personalized message to enqueue
{
"title": "string",
"imageUrl": "string",
"summary": "string",
"url": "string"
}
{
"title": "New message??",
"imageUrl": "https://wallpaperaccess.com/full/90977.jpg",
"summary": "Yes...",
"url": "https://github.com/kmlyteixeira"
}
After create your Service Bus queue (you'll find more here: Azure Service Bus | Microsoft Learn):
- Clone this repo
https://github.com/kmlyteixeira/message-queue.git
- Enter in the sender folder:
cd sender
- Use the
.env-example
file to assign your connection string and queue name - Run
npm install
to install the dependencies - Run
npm start
- The console should display this message: API de envio de mensagens iniciada em http://localhost:3001
Receives and processes messages from the queue.
⌚ Starts the processor and waits for new messages to be added to the messages.txt
file
🔍 Get first 50 enqueued messages from the queue in PEEK MODE
[
{
"id": "uuid",
"enqueuedAt": "timestamp",
"state": "string ('Active' | 'Deferred')",
"bodySize": "number",
"message": {
"title": "string",
"url": "string",
"imageUrl": "string",
"summary": "string"
}
}
]
After create your Service Bus queue and clone this repo:
- Enter in the worker folder:
cd worker
- Use the
.appsettings-example.json
file to assign your connection string and queue name - Run
dotnet build worker.csproj
to build this project - Run
dotnet run worker.csproj
- The console should display this message: Now listening on: http://localhost:5000
1️⃣ Quickstart - Use Azure Service Bus queues from .NET app - Azure Service Bus | Microsoft Learn
2️⃣ Create a Queue Service - .NET | Microsoft Learn
3️⃣ Introduction to Message Queuing. What is a Message Queue? | by Kabilesh Kumararatnam | Medium
4️⃣ FIFO (computing and electronics)