Skip to content

Commit 2ffc203

Browse files
authored
Merge pull request #8 from mehyaa/feature/rabbitmq-serialization
Enhance RabbitMQ client with async serialization and content type sup…
2 parents 54c32cb + 9062837 commit 2ffc203

17 files changed

Lines changed: 162 additions & 81 deletions

src/Convey.MessageBrokers.RabbitMQ/src/Convey.MessageBrokers.RabbitMQ/Clients/RabbitMqClient.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ internal sealed class RabbitMqClient : IRabbitMqClient
2525
private readonly string _spanContextHeader;
2626
private readonly bool _persistMessages;
2727
private readonly int _maxChannels;
28+
private readonly string _contentType;
29+
private readonly string _contentEncoding;
2830

2931
private readonly ConcurrentDictionary<int, IChannel> _channels = new();
3032

@@ -48,6 +50,8 @@ public RabbitMqClient(
4850
_spanContextHeader = options.GetSpanContextHeader();
4951
_persistMessages = options?.MessagesPersisted ?? false;
5052
_maxChannels = options.MaxProducerChannels <= 0 ? 1000 : options.MaxProducerChannels;
53+
_contentType = options.Publish?.ContentType ?? "application/json";
54+
_contentEncoding = options.Publish?.ContentEncoding ?? "UTF-8";
5155
}
5256

5357
public async Task SendAsync(
@@ -109,8 +113,8 @@ public async Task SendAsync(
109113
var properties = new BasicProperties
110114
{
111115
AppId = _appOptions.Service,
112-
ContentEncoding = _serializer.ContentEncoding,
113-
ContentType = _serializer.ContentType,
116+
ContentEncoding = _contentEncoding,
117+
ContentType = _contentType,
114118
Persistent = _persistMessages,
115119
MessageId = string.IsNullOrWhiteSpace(messageId) ? Guid.NewGuid().ToString("N") : messageId,
116120
CorrelationId = string.IsNullOrWhiteSpace(correlationId) ? Guid.NewGuid().ToString("N") : correlationId,
@@ -121,7 +125,7 @@ public async Task SendAsync(
121125

122126
if (_contextEnabled)
123127
{
124-
IncludeMessageContext(messageContext, properties);
128+
await IncludeMessageContextAsync(messageContext, properties, cancellationToken);
125129
}
126130

127131
if (!string.IsNullOrWhiteSpace(spanContext))
@@ -152,7 +156,7 @@ public async Task SendAsync(
152156
properties.CorrelationId);
153157
}
154158

155-
var body = _serializer.Serialize(message);
159+
var body = await _serializer.SerializeAsync(message, _contentType, cancellationToken);
156160

157161
await channel.BasicPublishAsync(
158162
exchange: convention.Exchange,
@@ -163,7 +167,7 @@ await channel.BasicPublishAsync(
163167
cancellationToken: cancellationToken);
164168
}
165169

166-
private void IncludeMessageContext(object context, BasicProperties properties)
170+
private async Task IncludeMessageContextAsync(object context, BasicProperties properties, CancellationToken cancellationToken)
167171
{
168172
if (properties?.Headers is null)
169173
{
@@ -172,7 +176,7 @@ private void IncludeMessageContext(object context, BasicProperties properties)
172176

173177
if (context is not null)
174178
{
175-
properties.Headers.Add(_contextProvider.HeaderName, _serializer.Serialize(context));
179+
properties.Headers.Add(_contextProvider.HeaderName, await _serializer.SerializeAsync(context, _contentType, cancellationToken));
176180

177181
return;
178182
}

src/Convey.MessageBrokers.RabbitMQ/src/Convey.MessageBrokers.RabbitMQ/Contexts/ContextProvider.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
24

35
namespace Convey.MessageBrokers.RabbitMQ.Contexts;
46

@@ -18,7 +20,12 @@ public ContextProvider(IRabbitMqSerializer serializer, RabbitMqOptions options)
1820
: options.Context.Header;
1921
}
2022

21-
public object Get(object message, Type messageType, MessageProperties messageProperties)
23+
public async Task<object> GetAsync(
24+
object message,
25+
Type messageType,
26+
MessageProperties messageProperties,
27+
string contentType,
28+
CancellationToken cancellationToken = default)
2229
{
2330
if (messageProperties.Headers is null)
2431
{
@@ -32,7 +39,7 @@ public object Get(object message, Type messageType, MessageProperties messagePro
3239

3340
if (context is byte[] bytes)
3441
{
35-
return _serializer.Deserialize(bytes);
42+
return await _serializer.DeserializeAsync<object>(bytes, contentType, cancellationToken);
3643
}
3744

3845
return null;

src/Convey.MessageBrokers.RabbitMQ/src/Convey.MessageBrokers.RabbitMQ/Conventions/ConventionBuilder.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,39 @@ public ConventionBuilder(RabbitMqOptions options)
2525
StringComparison.InvariantCultureIgnoreCase) == true;
2626
}
2727

28-
public string GetRoutingKey(Type type)
28+
public string GetExchange(Type type)
2929
{
30-
var routingKey = type.Name;
30+
var exchange =
31+
string.IsNullOrWhiteSpace(_options.Exchange?.Name)
32+
? type.Assembly.GetName().Name
33+
: _options.Exchange.Name;
3134

32-
if (_options.Conventions?.MessageAttribute?.IgnoreRoutingKey is true)
35+
if (_options.Conventions?.MessageAttribute?.IgnoreExchange is true)
3336
{
34-
return WithCasing(routingKey);
37+
return WithCasing(exchange);
3538
}
3639

3740
var attribute = GeAttribute(type);
3841

39-
routingKey = string.IsNullOrWhiteSpace(attribute?.RoutingKey) ? routingKey : attribute.RoutingKey;
42+
exchange = string.IsNullOrWhiteSpace(attribute?.Exchange) ? exchange : attribute.Exchange;
4043

41-
return WithCasing(routingKey);
44+
return WithCasing(exchange);
4245
}
4346

44-
public string GetExchange(Type type)
47+
public string GetRoutingKey(Type type)
4548
{
46-
var exchange =
47-
string.IsNullOrWhiteSpace(_options.Exchange?.Name)
48-
? type.Assembly.GetName().Name
49-
: _options.Exchange.Name;
49+
var routingKey = type.Name;
5050

51-
if (_options.Conventions?.MessageAttribute?.IgnoreExchange is true)
51+
if (_options.Conventions?.MessageAttribute?.IgnoreRoutingKey is true)
5252
{
53-
return WithCasing(exchange);
53+
return WithCasing(routingKey);
5454
}
5555

5656
var attribute = GeAttribute(type);
5757

58-
exchange = string.IsNullOrWhiteSpace(attribute?.Exchange) ? exchange : attribute.Exchange;
58+
routingKey = string.IsNullOrWhiteSpace(attribute?.RoutingKey) ? routingKey : attribute.RoutingKey;
5959

60-
return WithCasing(exchange);
60+
return WithCasing(routingKey);
6161
}
6262

6363
public string GetQueue(Type type)

src/Convey.MessageBrokers.RabbitMQ/src/Convey.MessageBrokers.RabbitMQ/Conventions/ConventionProvider.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public ConventionProvider(IConventionRegistry registry, IConventionBuilder build
1616
_builder = builder;
1717
}
1818

19-
public IConvention Get<T>() => Get(typeof(T));
20-
2119
public IConvention Get(Type type)
2220
{
2321
if (_conventions.TryGetValue(type, out var convention))
@@ -29,8 +27,8 @@ public IConvention Get(Type type)
2927
_registry.Get(type) ??
3028
new MessageConvention(
3129
type,
32-
_builder.GetRoutingKey(type),
3330
_builder.GetExchange(type),
31+
_builder.GetRoutingKey(type),
3432
_builder.GetQueue(type));
3533

3634
_conventions.TryAdd(type, convention);

src/Convey.MessageBrokers.RabbitMQ/src/Convey.MessageBrokers.RabbitMQ/ConventionRegistry.cs renamed to src/Convey.MessageBrokers.RabbitMQ/src/Convey.MessageBrokers.RabbitMQ/Conventions/ConventionRegistry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33

4-
namespace Convey.MessageBrokers.RabbitMQ;
4+
namespace Convey.MessageBrokers.RabbitMQ.Conventions;
55

66
public class ConventionRegistry : IConventionRegistry
77
{

src/Convey.MessageBrokers.RabbitMQ/src/Convey.MessageBrokers.RabbitMQ/Conventions/MessageConvention.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ namespace Convey.MessageBrokers.RabbitMQ.Conventions;
55
public class MessageConvention : IConvention
66
{
77
public Type Type { get; }
8-
public string RoutingKey { get; }
98
public string Exchange { get; }
9+
public string RoutingKey { get; }
1010
public string Queue { get; }
1111

12-
public MessageConvention(Type type, string routingKey, string exchange, string queue)
12+
public MessageConvention(Type type, string exchange, string routingKey, string queue)
1313
{
1414
Type = type;
15-
RoutingKey = routingKey;
1615
Exchange = exchange;
16+
RoutingKey = routingKey;
1717
Queue = queue;
1818
}
1919
}

src/Convey.MessageBrokers.RabbitMQ/src/Convey.MessageBrokers.RabbitMQ/Extensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static IConveyBuilder AddRabbitMq(
7777
}
7878
else
7979
{
80-
builder.Services.AddSingleton<IRabbitMqSerializer, SystemTextJsonJsonRabbitMqSerializer>();
80+
builder.Services.AddSingleton<IRabbitMqSerializer, DefaultRabbitMqSerializer>();
8181
}
8282

8383
builder.Services.AddSingleton<IRabbitMqPluginsExecutor, RabbitMqPluginsExecutor>();
@@ -219,4 +219,7 @@ public static IConveyBuilder AddExceptionToFailedMessageMapper<T>(this IConveyBu
219219

220220
public static IBusSubscriber UseRabbitMq(this IApplicationBuilder app)
221221
=> app.ApplicationServices.GetRequiredService<IBusSubscriber>();
222+
223+
public static IConvention Get<T>(this IConventionProvider conventionProvider)
224+
=> conventionProvider.Get(typeof(T));
222225
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
24

35
namespace Convey.MessageBrokers.RabbitMQ;
46

57
public interface IContextProvider
68
{
79
string HeaderName { get; }
8-
object Get(object message, Type messageType, MessageProperties messageProperties);
10+
Task<object> GetAsync(object message, Type messageType, MessageProperties messageProperties, string contentType, CancellationToken cancellationToken = default);
911
}

src/Convey.MessageBrokers.RabbitMQ/src/Convey.MessageBrokers.RabbitMQ/IConvention.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Convey.MessageBrokers.RabbitMQ;
55
public interface IConvention
66
{
77
Type Type { get; }
8-
string RoutingKey { get; }
98
string Exchange { get; }
9+
string RoutingKey { get; }
1010
string Queue { get; }
1111
}

src/Convey.MessageBrokers.RabbitMQ/src/Convey.MessageBrokers.RabbitMQ/IConventionBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Convey.MessageBrokers.RabbitMQ;
44

55
public interface IConventionBuilder
66
{
7-
string GetRoutingKey(Type type);
87
string GetExchange(Type type);
8+
string GetRoutingKey(Type type);
99
string GetQueue(Type type);
1010
}

0 commit comments

Comments
 (0)