Skip to content

Commit

Permalink
RabbitMQ: Configure Dead Letter Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Aug 5, 2024
1 parent 6465b65 commit 2376a7e
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docker-build-microservices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: docker-compose build
- name: docker compose build
run: |
cd src/Microservices
docker-compose build
docker compose build
- name: Log into Registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
- name: Tag and Push Images
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docker-build-modularmonolith.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: docker-compose build
- name: docker compose build
run: |
cd src/ModularMonolith
docker-compose build
docker compose build
- name: Log into Registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
- name: Tag and Push Images
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docker-build-monolith.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: docker-compose build
- name: docker compose build
run: |
cd src/Monolith
docker-compose build
docker compose build
- name: Log into Registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
- name: Tag and Push Images
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ public Task ReceiveAsync(Func<T, MetaData, Task> action, CancellationToken cance
arguments["x-single-active-consumer"] = true;
}

if (_options.DeadLetter != null)
{
if (!string.IsNullOrEmpty(_options.DeadLetter.ExchangeName))
{
arguments["x-dead-letter-exchange"] = _options.DeadLetter.ExchangeName;
}

if (!string.IsNullOrEmpty(_options.DeadLetter.RoutingKey))
{
arguments["x-dead-letter-routing-key"] = _options.DeadLetter.RoutingKey;
}

if (_options.DeadLetter.AutomaticCreateEnabled && !string.IsNullOrEmpty(_options.DeadLetter.QueueName))
{
_channel.QueueDeclare(_options.DeadLetter.QueueName, true, false, false, null);
_channel.QueueBind(_options.DeadLetter.QueueName, _options.DeadLetter.ExchangeName, _options.DeadLetter.RoutingKey, null);
}
}

arguments = arguments.Count == 0 ? null : arguments;

_channel.QueueDeclare(_options.QueueName, true, false, false, arguments);
Expand Down Expand Up @@ -108,6 +127,8 @@ public Task ReceiveAsync(Func<T, MetaData, Task> action, CancellationToken cance
catch (Exception ex)
{
// TODO: log here
await Task.Delay(1000);
_channel.BasicNack(deliveryTag: ea.DeliveryTag, multiple: false, requeue: _options.RequeueOnFailure);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,19 @@ public class RabbitMQReceiverOptions
public bool MessageEncryptionEnabled { get; set; }

public string MessageEncryptionKey { get; set; }

public bool RequeueOnFailure { get; set; }

public DeadLetterOptions DeadLetter { get; set; }
}

public class DeadLetterOptions
{
public string ExchangeName { get; set; }

public string RoutingKey { get; set; }

public string QueueName { get; set; }

public bool AutomaticCreateEnabled { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ public Task ReceiveAsync(Func<T, MetaData, Task> action, CancellationToken cance
arguments["x-single-active-consumer"] = true;
}

if (_options.DeadLetter != null)
{
if (!string.IsNullOrEmpty(_options.DeadLetter.ExchangeName))
{
arguments["x-dead-letter-exchange"] = _options.DeadLetter.ExchangeName;
}

if (!string.IsNullOrEmpty(_options.DeadLetter.RoutingKey))
{
arguments["x-dead-letter-routing-key"] = _options.DeadLetter.RoutingKey;
}

if (_options.DeadLetter.AutomaticCreateEnabled && !string.IsNullOrEmpty(_options.DeadLetter.QueueName))
{
_channel.QueueDeclare(_options.DeadLetter.QueueName, true, false, false, null);
_channel.QueueBind(_options.DeadLetter.QueueName, _options.DeadLetter.ExchangeName, _options.DeadLetter.RoutingKey, null);
}
}

arguments = arguments.Count == 0 ? null : arguments;

_channel.QueueDeclare(_options.QueueName, true, false, false, arguments);
Expand Down Expand Up @@ -108,6 +127,8 @@ public Task ReceiveAsync(Func<T, MetaData, Task> action, CancellationToken cance
catch (Exception ex)
{
// TODO: log here
await Task.Delay(1000);
_channel.BasicNack(deliveryTag: ea.DeliveryTag, multiple: false, requeue: _options.RequeueOnFailure);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,19 @@ public class RabbitMQReceiverOptions
public bool MessageEncryptionEnabled { get; set; }

public string MessageEncryptionKey { get; set; }

public bool RequeueOnFailure { get; set; }

public DeadLetterOptions DeadLetter { get; set; }
}

public class DeadLetterOptions
{
public string ExchangeName { get; set; }

public string RoutingKey { get; set; }

public string QueueName { get; set; }

public bool AutomaticCreateEnabled { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ public Task ReceiveAsync(Func<T, MetaData, Task> action, CancellationToken cance
arguments["x-single-active-consumer"] = true;
}

if (_options.DeadLetter != null)
{
if (!string.IsNullOrEmpty(_options.DeadLetter.ExchangeName))
{
arguments["x-dead-letter-exchange"] = _options.DeadLetter.ExchangeName;
}

if (!string.IsNullOrEmpty(_options.DeadLetter.RoutingKey))
{
arguments["x-dead-letter-routing-key"] = _options.DeadLetter.RoutingKey;
}

if (_options.DeadLetter.AutomaticCreateEnabled && !string.IsNullOrEmpty(_options.DeadLetter.QueueName))
{
_channel.QueueDeclare(_options.DeadLetter.QueueName, true, false, false, null);
_channel.QueueBind(_options.DeadLetter.QueueName, _options.DeadLetter.ExchangeName, _options.DeadLetter.RoutingKey, null);
}
}

arguments = arguments.Count == 0 ? null : arguments;

_channel.QueueDeclare(_options.QueueName, true, false, false, arguments);
Expand Down Expand Up @@ -108,6 +127,8 @@ public Task ReceiveAsync(Func<T, MetaData, Task> action, CancellationToken cance
catch (Exception ex)
{
// TODO: log here
await Task.Delay(1000);
_channel.BasicNack(deliveryTag: ea.DeliveryTag, multiple: false, requeue: _options.RequeueOnFailure);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,19 @@ public class RabbitMQReceiverOptions
public bool MessageEncryptionEnabled { get; set; }

public string MessageEncryptionKey { get; set; }

public bool RequeueOnFailure { get; set; }

public DeadLetterOptions DeadLetter { get; set; }
}

public class DeadLetterOptions
{
public string ExchangeName { get; set; }

public string RoutingKey { get; set; }

public string QueueName { get; set; }

public bool AutomaticCreateEnabled { get; set; }
}

0 comments on commit 2376a7e

Please sign in to comment.