Skip to content

Commit 344e270

Browse files
author
Felipe Mattioli
committed
FEAT: Adding new methods
1 parent 346bff3 commit 344e270

4 files changed

Lines changed: 82 additions & 23 deletions

File tree

src/AzureServiceBusFlow/Abstractions/IServiceBusProducer.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,52 @@
22

33
namespace AzureServiceBusFlow.Abstractions
44
{
5+
/// <summary>
6+
/// Defines a producer responsible for sending messages to Azure Service Bus.
7+
/// </summary>
8+
/// <typeparam name="TMessage">The type of the message to be sent.</typeparam>
59
public interface IServiceBusProducer<in TMessage> where TMessage : class, IServiceBusMessage
610
{
11+
/// <summary>
12+
/// Sends a message to the Azure Service Bus without any additional options.
13+
/// </summary>
14+
/// <param name="message">The message to be sent.</param>
15+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
716
Task ProduceAsync(TMessage message, CancellationToken cancellationToken);
817

18+
/// <summary>
19+
/// Sends a message to the Azure Service Bus with custom producer options,
20+
/// such as delivery delay or application properties.
21+
/// </summary>
22+
/// <param name="message">The message to be sent.</param>
23+
/// <param name="producerOptions">Additional options for message delivery.</param>
24+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
925
Task ProduceAsync(
1026
TMessage message,
1127
MessageOptions producerOptions,
1228
CancellationToken cancellationToken);
29+
30+
/// <summary>
31+
/// Sends a message to the Azure Service Bus with a specified delivery delay.
32+
/// </summary>
33+
/// <param name="message">The message to be sent.</param>
34+
/// <param name="delay">The delay duration before the message becomes available.</param>
35+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
36+
Task ProduceAsync(
37+
TMessage message,
38+
TimeSpan delay,
39+
CancellationToken cancellationToken);
40+
41+
/// <summary>
42+
/// Sends a message to the Azure Service Bus with application properties
43+
/// that can be used for downstream filtering or routing.
44+
/// </summary>
45+
/// <param name="message">The message to be sent.</param>
46+
/// <param name="applicationProperties">Key-value pairs attached to the message.</param>
47+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
48+
Task ProduceAsync(
49+
TMessage message,
50+
IDictionary<string, string> applicationProperties,
51+
CancellationToken cancellationToken);
1352
}
1453
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
namespace AzureServiceBusFlow.Models
22
{
3-
public record MessageOptions(TimeSpan? Delay, IDictionary<string, object>? ApplicationProperties);
3+
public record MessageOptions(TimeSpan? Delay, IDictionary<string, string>? ApplicationProperties);
44
}

src/AzureServiceBusFlow/Producers/CommandProducer.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,41 @@
33

44
namespace AzureServiceBusFlow.Producers
55
{
6+
/// <summary>
7+
/// A producer responsible for dispatching command messages through the underlying Service Bus producer.
8+
/// </summary>
9+
/// <typeparam name="TCommand">The type of command being produced.</typeparam>
610
public class CommandProducer<TCommand>(IServiceBusProducer<TCommand> producer) : ICommandProducer<TCommand>
7-
where TCommand : class, IServiceBusMessage
11+
where TCommand : class, IServiceBusMessage
812
{
913
private readonly IServiceBusProducer<TCommand> _producer = producer;
1014

15+
/// <inheritdoc />
1116
public Task ProduceCommandAsync(TCommand command, CancellationToken cancellationToken)
1217
{
1318
return _producer.ProduceAsync(command, cancellationToken);
1419
}
1520

21+
/// <inheritdoc />
1622
public Task ProduceCommandAsync(TCommand command, MessageOptions messageOptions, CancellationToken cancellationToken)
1723
{
1824
return _producer.ProduceAsync(command, messageOptions, cancellationToken);
1925
}
26+
27+
/// <summary>
28+
/// Produces a command with a delivery delay.
29+
/// </summary>
30+
public Task ProduceCommandAsync(TCommand command, TimeSpan delay, CancellationToken cancellationToken)
31+
{
32+
return _producer.ProduceAsync(command, delay, cancellationToken);
33+
}
34+
35+
/// <summary>
36+
/// Produces a command with application properties.
37+
/// </summary>
38+
public Task ProduceCommandAsync(TCommand command, IDictionary<string, string> applicationProperties, CancellationToken cancellationToken)
39+
{
40+
return _producer.ProduceAsync(command, applicationProperties, cancellationToken);
41+
}
2042
}
2143
}

src/AzureServiceBusFlow/Producers/ServiceBusProducer.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,15 @@ public ServiceBusProducer(AzureServiceBusConfiguration azureServiceBusConfigurat
1515
{
1616
var client = new ServiceBusClient(azureServiceBusConfiguration.ConnectionString);
1717
_sender = client.CreateSender(queueOrTopicName);
18-
1918
_logger = logger;
2019
}
2120

2221
public async Task ProduceAsync(TMessage message, CancellationToken cancellationToken)
2322
{
24-
var json = JsonConvert.SerializeObject(message);
25-
var serviceBusMessage = new ServiceBusMessage(json)
26-
{
27-
Subject = message.RoutingKey,
28-
ApplicationProperties =
29-
{
30-
{ "MessageType", message.GetType().FullName },
31-
{ "CreatedAt", message.CreatedDate.ToString("O") }
32-
}
33-
};
34-
35-
await _sender.SendMessageAsync(serviceBusMessage, cancellationToken);
36-
37-
_logger.LogInformation("Message {MessageType} published with successfully!", message.GetType().Name);
23+
await ProduceAsync(message, producerOptions: null, cancellationToken);
3824
}
3925

40-
public Task ProduceAsync(TMessage message, MessageOptions producerOptions, CancellationToken cancellationToken)
26+
public async Task ProduceAsync(TMessage message, MessageOptions? producerOptions, CancellationToken cancellationToken)
4127
{
4228
var json = JsonConvert.SerializeObject(message);
4329
var serviceBusMessage = new ServiceBusMessage(json)
@@ -52,19 +38,31 @@ public Task ProduceAsync(TMessage message, MessageOptions producerOptions, Cance
5238

5339
if (producerOptions?.ApplicationProperties is not null)
5440
{
55-
producerOptions?.ApplicationProperties?
56-
.Where(kvp => !serviceBusMessage.ApplicationProperties.ContainsKey(kvp.Key))
57-
.ToList()
58-
.ForEach(kvp => serviceBusMessage.ApplicationProperties.Add(kvp.Key, kvp.Value));
41+
foreach (var kvp in producerOptions.ApplicationProperties)
42+
{
43+
if (!serviceBusMessage.ApplicationProperties.ContainsKey(kvp.Key))
44+
serviceBusMessage.ApplicationProperties.Add(kvp.Key, kvp.Value);
45+
}
5946
}
6047

6148
if (producerOptions?.Delay is not null)
6249
{
6350
serviceBusMessage.ScheduledEnqueueTime = DateTimeOffset.UtcNow.Add(producerOptions.Delay.Value);
6451
}
6552

66-
return _sender.SendMessageAsync(serviceBusMessage, cancellationToken);
53+
await _sender.SendMessageAsync(serviceBusMessage, cancellationToken);
54+
55+
_logger.LogInformation("Message {MessageType} published successfully!", message.GetType().Name);
6756
}
6857

58+
public Task ProduceAsync(TMessage message, TimeSpan delay, CancellationToken cancellationToken)
59+
{
60+
return ProduceAsync(message, new MessageOptions(delay, null), cancellationToken);
61+
}
62+
63+
public Task ProduceAsync(TMessage message, IDictionary<string, string> applicationProperties, CancellationToken cancellationToken)
64+
{
65+
return ProduceAsync(message, new MessageOptions(null, applicationProperties), cancellationToken);
66+
}
6967
}
7068
}

0 commit comments

Comments
 (0)