You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by aa...@apache.org on 2023/03/07 07:20:07 UTC
[rocketmq-clients] 02/02: Support to use DateTime.Now to send message
This is an automated email from the ASF dual-hosted git repository.
aaronai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git
commit cd4302c5f23c9602b5bc9d70587ff5c856963aee
Author: Aaron Ai <ya...@gmail.com>
AuthorDate: Tue Mar 7 14:50:53 2023 +0800
Support to use DateTime.Now to send message
---
csharp/examples/ProducerDelayMessageExample.cs | 2 +-
csharp/rocketmq-client-csharp/IClientManager.cs | 3 +--
csharp/rocketmq-client-csharp/Message.cs | 4 +++-
csharp/rocketmq-client-csharp/MessageView.cs | 5 +++--
csharp/rocketmq-client-csharp/Producer.cs | 3 ++-
csharp/rocketmq-client-csharp/PublishingMessage.cs | 2 +-
csharp/tests/EndpointsTest.cs | 4 ++--
csharp/tests/MessageTest.cs | 25 ++++++++++++++++++++++
8 files changed, 38 insertions(+), 10 deletions(-)
diff --git a/csharp/examples/ProducerDelayMessageExample.cs b/csharp/examples/ProducerDelayMessageExample.cs
index f1119245..84808872 100644
--- a/csharp/examples/ProducerDelayMessageExample.cs
+++ b/csharp/examples/ProducerDelayMessageExample.cs
@@ -59,7 +59,7 @@ namespace examples
.SetTag(tag)
// You could set multiple keys for the single message actually.
.SetKeys("yourMessageKey-2f00df144e48")
- .SetDeliveryTimestamp(DateTime.UtcNow + TimeSpan.FromSeconds(30))
+ .SetDeliveryTimestamp(DateTime.Now + TimeSpan.FromSeconds(30))
.Build();
var sendReceipt = await producer.Send(message);
diff --git a/csharp/rocketmq-client-csharp/IClientManager.cs b/csharp/rocketmq-client-csharp/IClientManager.cs
index d69b0506..19f9459f 100644
--- a/csharp/rocketmq-client-csharp/IClientManager.cs
+++ b/csharp/rocketmq-client-csharp/IClientManager.cs
@@ -90,8 +90,7 @@ namespace Org.Apache.Rocketmq
/// <param name="timeout">Request max duration.</param>
/// <returns></returns>
Task<RpcInvocation<ReceiveMessageRequest, List<ReceiveMessageResponse>>> ReceiveMessage(Endpoints endpoints,
- ReceiveMessageRequest request,
- TimeSpan timeout);
+ ReceiveMessageRequest request, TimeSpan timeout);
/// <summary>
/// Message acknowledgement towards remote endpoints.
diff --git a/csharp/rocketmq-client-csharp/Message.cs b/csharp/rocketmq-client-csharp/Message.cs
index 42c271a5..b71a4650 100644
--- a/csharp/rocketmq-client-csharp/Message.cs
+++ b/csharp/rocketmq-client-csharp/Message.cs
@@ -131,7 +131,9 @@ namespace Org.Apache.Rocketmq
{
Preconditions.CheckArgument(null == _messageGroup,
"deliveryTimestamp and messageGroup should not be set at same time");
- _deliveryTimestamp = deliveryTimestamp;
+ _deliveryTimestamp = DateTimeKind.Utc == deliveryTimestamp.Kind
+ ? TimeZoneInfo.ConvertTimeFromUtc(deliveryTimestamp, TimeZoneInfo.Local)
+ : deliveryTimestamp;
return this;
}
diff --git a/csharp/rocketmq-client-csharp/MessageView.cs b/csharp/rocketmq-client-csharp/MessageView.cs
index 57a10619..2581f48b 100644
--- a/csharp/rocketmq-client-csharp/MessageView.cs
+++ b/csharp/rocketmq-client-csharp/MessageView.cs
@@ -158,11 +158,12 @@ namespace Org.Apache.Rocketmq
var messageGroup = systemProperties.HasMessageGroup ? systemProperties.MessageGroup : null;
DateTime? deliveryTime = null == systemProperties.DeliveryTimestamp
? null
- : systemProperties.DeliveryTimestamp.ToDateTime();
+ : TimeZoneInfo.ConvertTimeFromUtc(systemProperties.DeliveryTimestamp.ToDateTime(), TimeZoneInfo.Local);
var keys = systemProperties.Keys.ToList();
var bornHost = systemProperties.BornHost;
- var bornTime = systemProperties.BornTimestamp.ToDateTime();
+ var bornTime =
+ TimeZoneInfo.ConvertTimeFromUtc(systemProperties.BornTimestamp.ToDateTime(), TimeZoneInfo.Local);
var deliveryAttempt = systemProperties.DeliveryAttempt;
var queueOffset = systemProperties.QueueOffset;
var properties = new Dictionary<string, string>();
diff --git a/csharp/rocketmq-client-csharp/Producer.cs b/csharp/rocketmq-client-csharp/Producer.cs
index 95ddf98b..eebc24a2 100644
--- a/csharp/rocketmq-client-csharp/Producer.cs
+++ b/csharp/rocketmq-client-csharp/Producer.cs
@@ -188,7 +188,8 @@ namespace Org.Apache.Rocketmq
}
}
- throw exception!;
+ throw new Exception($"Failed to send message finally, topic={message.Topic}, clientId={ClientId}",
+ exception);
}
public async Task<ISendReceipt> Send(Message message)
diff --git a/csharp/rocketmq-client-csharp/PublishingMessage.cs b/csharp/rocketmq-client-csharp/PublishingMessage.cs
index b65f4660..ff681fe7 100644
--- a/csharp/rocketmq-client-csharp/PublishingMessage.cs
+++ b/csharp/rocketmq-client-csharp/PublishingMessage.cs
@@ -92,7 +92,7 @@ namespace Org.Apache.Rocketmq
if (DeliveryTimestamp.HasValue)
{
- systemProperties.DeliveryTimestamp = Timestamp.FromDateTime(DeliveryTimestamp.Value);
+ systemProperties.DeliveryTimestamp = Timestamp.FromDateTime(DeliveryTimestamp.Value.ToUniversalTime());
}
if (null != MessageGroup)
diff --git a/csharp/tests/EndpointsTest.cs b/csharp/tests/EndpointsTest.cs
index 4c85209e..8e71ae23 100644
--- a/csharp/tests/EndpointsTest.cs
+++ b/csharp/tests/EndpointsTest.cs
@@ -35,8 +35,8 @@ namespace tests
public void TestGrpcTargetWithSsl()
{
var endpoints = new Endpoints("127.0.0.1");
- var targetWithoutSsl = endpoints.GrpcTarget(true);
- Assert.AreEqual("https://127.0.0.1:80", targetWithoutSsl);
+ var targetWithSsl = endpoints.GrpcTarget(true);
+ Assert.AreEqual("https://127.0.0.1:80", targetWithSsl);
}
}
}
\ No newline at end of file
diff --git a/csharp/tests/MessageTest.cs b/csharp/tests/MessageTest.cs
index d8191d28..56d942cf 100644
--- a/csharp/tests/MessageTest.cs
+++ b/csharp/tests/MessageTest.cs
@@ -148,6 +148,31 @@ namespace tests
new Message.Builder().SetKeys("a", "b");
}
+ [TestMethod]
+ public void TestSetDeliveryTimestampWithLocalTime()
+ {
+ var deliveryTimestamp = DateTime.Now;
+ var message = new Message.Builder().SetTopic("yourTopic").SetDeliveryTimestamp(deliveryTimestamp)
+ .SetBody(Encoding.UTF8.GetBytes("foobar"))
+ .Build();
+ Assert.IsTrue(message.DeliveryTimestamp.HasValue);
+ Assert.AreEqual(DateTimeKind.Local, message.DeliveryTimestamp.Value.Kind);
+ Assert.AreEqual(deliveryTimestamp, message.DeliveryTimestamp.Value);
+ }
+
+ [TestMethod]
+ public void TestSetDeliveryTimestampWithUtcTime()
+ {
+ var deliveryTimestamp = DateTime.UtcNow;
+ var message = new Message.Builder().SetTopic("yourTopic").SetDeliveryTimestamp(deliveryTimestamp)
+ .SetBody(Encoding.UTF8.GetBytes("foobar"))
+ .Build();
+ Assert.IsTrue(message.DeliveryTimestamp.HasValue);
+ Assert.AreEqual(DateTimeKind.Local, message.DeliveryTimestamp.Value.Kind);
+ var localTimestamp = TimeZoneInfo.ConvertTimeFromUtc(deliveryTimestamp, TimeZoneInfo.Local);
+ Assert.AreEqual(localTimestamp, message.DeliveryTimestamp.Value);
+ }
+
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TestSetDeliveryTimestampAndMessageGroup()