Skip to content

Commit d584d55

Browse files
authored
Use ApplicationData by Default instead of AMQPValue for the message creation (#116)
* Change the message interface from object to byte array. Change the Message body section from AMQPValue to Data. It is better for cross-protocol use cases --------- Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent 4e3d7d7 commit d584d55

File tree

16 files changed

+164
-38
lines changed

16 files changed

+164
-38
lines changed

.ci/ubuntu/cluster/gha-setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function run_docker_compose
1919
docker compose --file "$script_dir/docker-compose.yml" $@
2020
}
2121

22-
readonly rabbitmq_image="${RABBITMQ_IMAGE:-rabbitmq:4.1.0-beta.4-management-alpine}"
22+
readonly rabbitmq_image="${RABBITMQ_IMAGE:-rabbitmq:4.1.0-management-alpine}"
2323

2424
if [[ ! -v GITHUB_ACTIONS ]]
2525
then

.ci/ubuntu/one-node/gha-setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ readonly script_dir
99
echo "[INFO] script_dir: '$script_dir'"
1010

1111

12-
readonly rabbitmq_image="${RABBITMQ_IMAGE:-rabbitmq:4.1.0-beta.4-management-alpine}"
12+
readonly rabbitmq_image="${RABBITMQ_IMAGE:-rabbitmq:4.1.0-management-alpine}"
1313

1414

1515

.ci/windows/versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"erlang": "27.2",
3-
"rabbitmq": "4.1.0-beta.4"
3+
"rabbitmq": "4.1.0"
44
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,5 @@ docs/temp/
129129

130130
# ci logs
131131
.ci/ubuntu/log/*
132+
.ci/ubuntu/cluster/log/*
132133

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<PackageVersion Include="AMQPNetLite.Core" Version="2.4.11" />
88
<PackageVersion Include="OpenTelemetry" Version="1.10.0" />
99
<PackageVersion Include="OpenTelemetry.Exporter.Console" Version="1.10.0" />
10+
<PackageVersion Include="RabbitMQ.Client" Version="6.8.1" />
1011
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="9.0.0" />
1112
<!-- HAClient -->
1213
<PackageVersion Include="DotNext.Threading" Version="5.15.0" />

RabbitMQ.AMQP.Client/IMessage.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ public interface IMessage
9696
public object Annotation(string key);
9797
public IMessage Annotation(string key, object value);
9898

99-
public object Body();
99+
public byte[] Body();
100+
101+
public string BodyAsString();
102+
100103
public IMessage Body(object body);
101104

102105
IMessageAddressBuilder ToAddress();

RabbitMQ.AMQP.Client/IRpcServer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public interface IRpcServer : ILifeCycle
7878

7979
public interface IContext
8080
{
81-
IMessage Message(object body);
81+
IMessage Message(byte[] body);
82+
IMessage Message(string body);
8283
}
8384
}
8485
}

RabbitMQ.AMQP.Client/Impl/AmqpMessage.cs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,24 @@ public AmqpMessage()
2424
NativeMessage = new Message();
2525
}
2626

27-
public AmqpMessage(object body)
27+
/// <summary>
28+
/// Create a message with a body of type byte[] and BodySection of type Data.
29+
/// </summary>
30+
/// <param name="body"></param>
31+
public AmqpMessage(byte[] body)
2832
{
29-
NativeMessage = new Message(body);
33+
NativeMessage = new Message();
34+
NativeMessage.BodySection = new Data { Binary = body };
35+
}
36+
37+
/// <summary>
38+
/// Create a message with a body of type string and BodySection of type Data.
39+
/// The string is converted to a byte[] using UTF8 encoding.
40+
/// </summary>
41+
public AmqpMessage(string body)
42+
{
43+
NativeMessage = new Message();
44+
NativeMessage.BodySection = new Data() { Binary = System.Text.Encoding.UTF8.GetBytes(body) };
3045
}
3146

3247
public AmqpMessage(Message nativeMessage)
@@ -245,35 +260,40 @@ public object Annotation(string key)
245260
return NativeMessage.MessageAnnotations[new Symbol(key)];
246261
}
247262

248-
public object Body()
263+
public byte[] Body()
249264
{
250-
return NativeMessage.Body;
265+
return (byte[])NativeMessage.Body;
266+
}
267+
268+
public string BodyAsString()
269+
{
270+
if (NativeMessage.BodySection is Data data)
271+
{
272+
return System.Text.Encoding.UTF8.GetString(data.Binary);
273+
}
274+
else
275+
{
276+
throw new InvalidOperationException("Body is not an Application Data");
277+
}
278+
251279
}
252280

253281
public IMessage Body(object body)
254282
{
255283
RestrictedDescribed bodySection;
256284
if (body is byte[] byteArray)
257285
{
258-
bodySection = new Data
259-
{
260-
Binary = byteArray
261-
};
286+
bodySection = new Data { Binary = byteArray };
262287
}
263288
else if (body is IList list)
264289
{
265-
bodySection = new AmqpSequence
266-
{
267-
List = list
268-
};
290+
bodySection = new AmqpSequence { List = list };
269291
}
270292
else
271293
{
272-
bodySection = new AmqpValue
273-
{
274-
Value = body
275-
};
294+
bodySection = new AmqpValue { Value = body };
276295
}
296+
277297
NativeMessage.BodySection = bodySection;
278298
return this;
279299
}

RabbitMQ.AMQP.Client/Impl/AmqpRpcServer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ await Utils.WaitWithBackOffUntilFuncAsync(async () =>
166166

167167
private class RpcServerContext : IRpcServer.IContext
168168
{
169-
public IMessage Message(object body) => new AmqpMessage(body);
169+
public IMessage Message(byte[] body) => new AmqpMessage(body);
170+
public IMessage Message(string body) => new AmqpMessage(body);
170171
}
171172

172173
public override async Task CloseAsync()

RabbitMQ.AMQP.Client/PublicAPI.Unshipped.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,9 @@ RabbitMQ.AMQP.Client.IMessage.AbsoluteExpiryTime() -> System.DateTime
245245
RabbitMQ.AMQP.Client.IMessage.AbsoluteExpiryTime(System.DateTime absoluteExpiryTime) -> RabbitMQ.AMQP.Client.IMessage!
246246
RabbitMQ.AMQP.Client.IMessage.Annotation(string! key) -> object!
247247
RabbitMQ.AMQP.Client.IMessage.Annotation(string! key, object! value) -> RabbitMQ.AMQP.Client.IMessage!
248-
RabbitMQ.AMQP.Client.IMessage.Body() -> object!
248+
RabbitMQ.AMQP.Client.IMessage.Body() -> byte[]!
249249
RabbitMQ.AMQP.Client.IMessage.Body(object! body) -> RabbitMQ.AMQP.Client.IMessage!
250+
RabbitMQ.AMQP.Client.IMessage.BodyAsString() -> string!
250251
RabbitMQ.AMQP.Client.IMessage.ContentEncoding() -> string!
251252
RabbitMQ.AMQP.Client.IMessage.ContentEncoding(string! contentEncoding) -> RabbitMQ.AMQP.Client.IMessage!
252253
RabbitMQ.AMQP.Client.IMessage.ContentType() -> string!
@@ -391,11 +392,13 @@ RabbitMQ.AMQP.Client.Impl.AmqpMessage.AbsoluteExpiryTime() -> System.DateTime
391392
RabbitMQ.AMQP.Client.Impl.AmqpMessage.AbsoluteExpiryTime(System.DateTime absoluteExpiryTime) -> RabbitMQ.AMQP.Client.IMessage!
392393
RabbitMQ.AMQP.Client.Impl.AmqpMessage.AmqpMessage() -> void
393394
RabbitMQ.AMQP.Client.Impl.AmqpMessage.AmqpMessage(Amqp.Message! nativeMessage) -> void
394-
RabbitMQ.AMQP.Client.Impl.AmqpMessage.AmqpMessage(object! body) -> void
395+
RabbitMQ.AMQP.Client.Impl.AmqpMessage.AmqpMessage(byte[]! body) -> void
396+
RabbitMQ.AMQP.Client.Impl.AmqpMessage.AmqpMessage(string! body) -> void
395397
RabbitMQ.AMQP.Client.Impl.AmqpMessage.Annotation(string! key) -> object!
396398
RabbitMQ.AMQP.Client.Impl.AmqpMessage.Annotation(string! key, object! value) -> RabbitMQ.AMQP.Client.IMessage!
397-
RabbitMQ.AMQP.Client.Impl.AmqpMessage.Body() -> object!
399+
RabbitMQ.AMQP.Client.Impl.AmqpMessage.Body() -> byte[]!
398400
RabbitMQ.AMQP.Client.Impl.AmqpMessage.Body(object! body) -> RabbitMQ.AMQP.Client.IMessage!
401+
RabbitMQ.AMQP.Client.Impl.AmqpMessage.BodyAsString() -> string!
399402
RabbitMQ.AMQP.Client.Impl.AmqpMessage.ContentEncoding() -> string!
400403
RabbitMQ.AMQP.Client.Impl.AmqpMessage.ContentEncoding(string! contentEncoding) -> RabbitMQ.AMQP.Client.IMessage!
401404
RabbitMQ.AMQP.Client.Impl.AmqpMessage.ContentType() -> string!
@@ -674,7 +677,8 @@ RabbitMQ.AMQP.Client.IRpcClientBuilder.RequestPostProcessor(System.Func<RabbitMQ
674677
RabbitMQ.AMQP.Client.IRpcClientBuilder.Timeout(System.TimeSpan timeout) -> RabbitMQ.AMQP.Client.IRpcClientBuilder!
675678
RabbitMQ.AMQP.Client.IRpcServer
676679
RabbitMQ.AMQP.Client.IRpcServer.IContext
677-
RabbitMQ.AMQP.Client.IRpcServer.IContext.Message(object! body) -> RabbitMQ.AMQP.Client.IMessage!
680+
RabbitMQ.AMQP.Client.IRpcServer.IContext.Message(byte[]! body) -> RabbitMQ.AMQP.Client.IMessage!
681+
RabbitMQ.AMQP.Client.IRpcServer.IContext.Message(string! body) -> RabbitMQ.AMQP.Client.IMessage!
678682
RabbitMQ.AMQP.Client.IRpcServerBuilder
679683
RabbitMQ.AMQP.Client.IRpcServerBuilder.BuildAsync() -> System.Threading.Tasks.Task<RabbitMQ.AMQP.Client.IRpcServer!>!
680684
RabbitMQ.AMQP.Client.IRpcServerBuilder.CorrelationIdExtractor(System.Func<RabbitMQ.AMQP.Client.IMessage!, object!>? correlationIdExtractor) -> RabbitMQ.AMQP.Client.IRpcServerBuilder!

0 commit comments

Comments
 (0)