-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb593f2
commit 84b4205
Showing
8 changed files
with
119 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 60 additions & 24 deletions
84
src/Barista/NCafe.Barista.Api/MessageBus/OrdersConsumerService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/Common/NCafe.Infrastructure/MessageBus/RabbitMqHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using RabbitMQ.Client; | ||
using System.Diagnostics; | ||
|
||
namespace NCafe.Infrastructure.MessageBus; | ||
internal static class RabbitMqHelper | ||
{ | ||
public static IModel CreateModelAndDeclareTestQueue(IConnection connection, string queueName) | ||
{ | ||
var channel = connection.CreateModel(); | ||
|
||
channel.QueueDeclare(queue: queueName, | ||
durable: true, | ||
exclusive: false, | ||
autoDelete: false, | ||
arguments: null); | ||
|
||
return channel; | ||
} | ||
|
||
public static void AddMessagingTags(Activity activity, string exchangeName, string queueName) | ||
{ | ||
// These tags are added demonstrating the semantic conventions of the OpenTelemetry messaging specification | ||
// See: | ||
// * https://github.com/open-telemetry/semantic-conventions/blob/main/docs/messaging/messaging-spans.md#messaging-attributes | ||
// * https://github.com/open-telemetry/semantic-conventions/blob/main/docs/messaging/rabbitmq.md | ||
activity?.SetTag("messaging.system", "rabbitmq"); | ||
activity?.SetTag("messaging.destination_kind", "queue"); | ||
activity?.SetTag("messaging.destination", exchangeName); | ||
activity?.SetTag("messaging.rabbitmq.routing_key", queueName); | ||
} | ||
} |
30 changes: 23 additions & 7 deletions
30
src/Common/NCafe.Infrastructure/MessageBus/RabbitMqPublisher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,31 @@ | ||
using EasyNetQ; | ||
using Microsoft.Extensions.Configuration; | ||
using NCafe.Core.MessageBus; | ||
using NCafe.Core.MessageBus; | ||
using RabbitMQ.Client; | ||
using System.Diagnostics; | ||
using System.Text.Json; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace NCafe.Infrastructure.MessageBus; | ||
|
||
internal class RabbitMqPublisher(IConfiguration configuration) : IPublisher | ||
internal class RabbitMqPublisher(IConnection connection, ILogger<RabbitMqPublisher> logger) : IPublisher | ||
{ | ||
private readonly IBus _bus = RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMq")); | ||
private readonly IConnection _connection = connection; | ||
private readonly ILogger _logger = logger; | ||
|
||
public async Task Publish<T>(string topicName, T message) where T : class, IBusMessage | ||
public Task Publish<T>(string queueName, T message) where T : class, IBusMessage | ||
{ | ||
await _bus.PubSub.PublishAsync(message, topicName); | ||
using var channel = RabbitMqHelper.CreateModelAndDeclareTestQueue(_connection, queueName); | ||
|
||
RabbitMqHelper.AddMessagingTags(Activity.Current, string.Empty, queueName); | ||
|
||
var body = JsonSerializer.SerializeToUtf8Bytes(message); | ||
|
||
channel.BasicPublish(exchange: string.Empty, | ||
routingKey: queueName, | ||
basicProperties: null, | ||
body: body); | ||
|
||
_logger.LogInformation("Published {MessageType} Message: {@Message}", message.GetType().Name, message); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters