You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by bl...@apache.org on 2020/12/12 15:42:14 UTC

[pulsar-dotpulsar] branch master updated: Make it easier to read the EventTime and PublishTime as a DateTime on the Message. Make it easier to set the EventTime and DeliverAtTime as a DateTime on the MessageMetadata and via the IMessageBuilder.

This is an automated email from the ASF dual-hosted git repository.

blankensteiner pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-dotpulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 1a19c0b  Make it easier to read the EventTime and PublishTime as a DateTime on the Message. Make it easier to set the EventTime and DeliverAtTime as a DateTime on the MessageMetadata and via the IMessageBuilder.
1a19c0b is described below

commit 1a19c0b8d02449de9070ec2111893de2d3b53c2a
Author: Daniel Blankensteiner <db...@vmail.dk>
AuthorDate: Sat Dec 12 16:42:03 2020 +0100

    Make it easier to read the EventTime and PublishTime as a DateTime on the Message.
    Make it easier to set the EventTime and DeliverAtTime as a DateTime on the MessageMetadata and via the IMessageBuilder.
---
 samples/Consuming/Program.cs                       | 26 +++++++++++-----------
 src/DotPulsar/Abstractions/IMessageBuilder.cs      | 18 +++++++++++----
 .../Extensions/MessageMetadataExtensions.cs        | 15 +++++++++++++
 src/DotPulsar/Internal/MessageBuilder.cs           | 16 +++++++++++--
 src/DotPulsar/Message.cs                           | 18 +++++++++++----
 src/DotPulsar/MessageMetadata.cs                   | 26 ++++++++++++++++++----
 6 files changed, 92 insertions(+), 27 deletions(-)

diff --git a/samples/Consuming/Program.cs b/samples/Consuming/Program.cs
index 914fc8a..0cc6e06 100644
--- a/samples/Consuming/Program.cs
+++ b/samples/Consuming/Program.cs
@@ -78,19 +78,19 @@ namespace Consuming
 
         private static void Monitor(ConsumerStateChanged stateChanged, CancellationToken cancellationToken)
         {
-                var stateMessage = stateChanged.ConsumerState switch
-                {
-                    ConsumerState.Active => "is active",
-                    ConsumerState.Inactive => "is inactive",
-                    ConsumerState.Disconnected => "is disconnected",
-                    ConsumerState.Closed => "has closed",
-                    ConsumerState.ReachedEndOfTopic => "has reached end of topic",
-                    ConsumerState.Faulted => "has faulted",
-                    _ => $"has an unknown state '{stateChanged.ConsumerState}'"
-                };
-
-                var topic = stateChanged.Consumer.Topic;
-                Console.WriteLine($"The consumer for topic '{topic}' " + stateMessage);
+            var stateMessage = stateChanged.ConsumerState switch
+            {
+                ConsumerState.Active => "is active",
+                ConsumerState.Inactive => "is inactive",
+                ConsumerState.Disconnected => "is disconnected",
+                ConsumerState.Closed => "has closed",
+                ConsumerState.ReachedEndOfTopic => "has reached end of topic",
+                ConsumerState.Faulted => "has faulted",
+                _ => $"has an unknown state '{stateChanged.ConsumerState}'"
+            };
+
+            var topic = stateChanged.Consumer.Topic;
+            Console.WriteLine($"The consumer for topic '{topic}' " + stateMessage);
         }
     }
 }
diff --git a/src/DotPulsar/Abstractions/IMessageBuilder.cs b/src/DotPulsar/Abstractions/IMessageBuilder.cs
index c5812ed..1720a4e 100644
--- a/src/DotPulsar/Abstractions/IMessageBuilder.cs
+++ b/src/DotPulsar/Abstractions/IMessageBuilder.cs
@@ -24,22 +24,32 @@ namespace DotPulsar.Abstractions
     public interface IMessageBuilder
     {
         /// <summary>
-        /// Timestamp indicating when the message should be delivered to consumers.
+        /// Timestamp as unix time in milliseconds indicating when the message should be delivered to consumers.
         /// </summary>
         IMessageBuilder DeliverAt(long timestamp);
 
         /// <summary>
-        /// Timestamp indicating when the message should be delivered to consumers.
+        /// Timestamp as UTC DateTime indicating when the message should be delivered to consumers.
+        /// </summary>
+        IMessageBuilder DeliverAt(DateTime timestamp);
+
+        /// <summary>
+        /// Timestamp as DateTimeOffset indicating when the message should be delivered to consumers.
         /// </summary>
         IMessageBuilder DeliverAt(DateTimeOffset timestamp);
 
         /// <summary>
-        /// Set the event time of the message.
+        /// The event time of the message as unix time in milliseconds.
         /// </summary>
         IMessageBuilder EventTime(ulong eventTime);
 
         /// <summary>
-        /// Set the event time of the message.
+        /// The event time of the message as an UTC DateTime.
+        /// </summary>
+        IMessageBuilder EventTime(DateTime eventTime);
+
+        /// <summary>
+        /// The event time of the message as a DateTimeOffset.
         /// </summary>
         IMessageBuilder EventTime(DateTimeOffset eventTime);
 
diff --git a/src/DotPulsar/Internal/Extensions/MessageMetadataExtensions.cs b/src/DotPulsar/Internal/Extensions/MessageMetadataExtensions.cs
index e653650..3e558fc 100644
--- a/src/DotPulsar/Internal/Extensions/MessageMetadataExtensions.cs
+++ b/src/DotPulsar/Internal/Extensions/MessageMetadataExtensions.cs
@@ -19,18 +19,33 @@ namespace DotPulsar.Internal.Extensions
 
     public static class MessageMetadataExtensions
     {
+        // Deliver at
+        public static DateTime GetDeliverAtTimeAsDateTime(this Metadata metadata)
+            => metadata.GetDeliverAtTimeAsDateTimeOffset().UtcDateTime;
+
+        public static void SetDeliverAtTime(this Metadata metadata, DateTime timestamp)
+            => metadata.SetDeliverAtTime(new DateTimeOffset(timestamp));
+
         public static DateTimeOffset GetDeliverAtTimeAsDateTimeOffset(this Metadata metadata)
             => DateTimeOffset.FromUnixTimeMilliseconds(metadata.DeliverAtTime);
 
         public static void SetDeliverAtTime(this Metadata metadata, DateTimeOffset timestamp)
             => metadata.DeliverAtTime = timestamp.ToUnixTimeMilliseconds();
 
+        // Event time
+        public static DateTime GetEventTimeAsDateTime(this Metadata metadata)
+            => metadata.GetEventTimeAsDateTimeOffset().UtcDateTime;
+
+        public static void SetEventTime(this Metadata metadata, DateTime timestamp)
+            => metadata.SetEventTime(new DateTimeOffset(timestamp));
+
         public static DateTimeOffset GetEventTimeAsDateTimeOffset(this Metadata metadata)
             => DateTimeOffset.FromUnixTimeMilliseconds((long) metadata.EventTime);
 
         public static void SetEventTime(this Metadata metadata, DateTimeOffset timestamp)
             => metadata.EventTime = (ulong) timestamp.ToUnixTimeMilliseconds();
 
+        // Key
         public static byte[]? GetKeyAsBytes(this Metadata metadata)
             => metadata.PartitionKeyB64Encoded ? Convert.FromBase64String(metadata.PartitionKey) : null;
 
diff --git a/src/DotPulsar/Internal/MessageBuilder.cs b/src/DotPulsar/Internal/MessageBuilder.cs
index 4e2a499..d7c7b02 100644
--- a/src/DotPulsar/Internal/MessageBuilder.cs
+++ b/src/DotPulsar/Internal/MessageBuilder.cs
@@ -37,9 +37,15 @@ namespace DotPulsar.Internal
             return this;
         }
 
-        public IMessageBuilder DeliverAt(DateTimeOffset deliverAt)
+        public IMessageBuilder DeliverAt(DateTime timestamp)
         {
-            _metadata.Metadata.SetDeliverAtTime(deliverAt);
+            _metadata.Metadata.SetDeliverAtTime(timestamp);
+            return this;
+        }
+
+        public IMessageBuilder DeliverAt(DateTimeOffset timestamp)
+        {
+            _metadata.Metadata.SetDeliverAtTime(timestamp);
             return this;
         }
 
@@ -49,6 +55,12 @@ namespace DotPulsar.Internal
             return this;
         }
 
+        public IMessageBuilder EventTime(DateTime eventTime)
+        {
+            _metadata.Metadata.SetEventTime(eventTime);
+            return this;
+        }
+
         public IMessageBuilder EventTime(DateTimeOffset eventTime)
         {
             _metadata.Metadata.SetEventTime(eventTime);
diff --git a/src/DotPulsar/Message.cs b/src/DotPulsar/Message.cs
index 812ed8f..32575d4 100644
--- a/src/DotPulsar/Message.cs
+++ b/src/DotPulsar/Message.cs
@@ -80,12 +80,17 @@ namespace DotPulsar
         public bool HasEventTime => EventTime != 0;
 
         /// <summary>
-        /// The event time of the message (unix time in milliseconds).
+        /// The event time of the message as unix time in milliseconds.
         /// </summary>
         public ulong EventTime { get; }
 
         /// <summary>
-        /// The event time of the message.
+        /// The event time of the message as an UTC DateTime.
+        /// </summary>
+        public DateTime EventTimeAsDateTime => EventTimeAsDateTimeOffset.UtcDateTime;
+
+        /// <summary>
+        /// The event time of the message as a DateTimeOffset with an offset of 0.
         /// </summary>
         public DateTimeOffset EventTimeAsDateTimeOffset => DateTimeOffset.FromUnixTimeMilliseconds((long) EventTime);
 
@@ -120,12 +125,17 @@ namespace DotPulsar
         public byte[]? OrderingKey { get; }
 
         /// <summary>
-        /// The publish time of the message (unix time in milliseconds).
+        /// The publish time of the message as unix time in milliseconds.
         /// </summary>
         public ulong PublishTime { get; }
 
         /// <summary>
-        /// The publish time of the message.
+        /// The publish time of the message as an UTC DateTime.
+        /// </summary>
+        public DateTime PublishTimeAsDateTime => PublishTimeAsDateTimeOffset.UtcDateTime;
+
+        /// <summary>
+        /// The publish time of the message as a DateTimeOffset with an offset of 0.
         /// </summary>
         public DateTimeOffset PublishTimeAsDateTimeOffset => DateTimeOffset.FromUnixTimeMilliseconds((long) PublishTime);
 
diff --git a/src/DotPulsar/MessageMetadata.cs b/src/DotPulsar/MessageMetadata.cs
index bb89777..fb7f632 100644
--- a/src/DotPulsar/MessageMetadata.cs
+++ b/src/DotPulsar/MessageMetadata.cs
@@ -29,7 +29,7 @@ namespace DotPulsar
         internal readonly Internal.PulsarApi.MessageMetadata Metadata;
 
         /// <summary>
-        /// The delivery time of the message (unix time in milliseconds).
+        /// The delivery time of the message as unix time in milliseconds.
         /// </summary>
         public long DeliverAtTime
         {
@@ -38,7 +38,16 @@ namespace DotPulsar
         }
 
         /// <summary>
-        /// The delivery time of the message.
+        /// The delivery time of the message as an UTC DateTime.
+        /// </summary>
+        public DateTime DeliverAtTimeAsDateTime
+        {
+            get => Metadata.GetDeliverAtTimeAsDateTime();
+            set => Metadata.SetDeliverAtTime(value);
+        }
+
+        /// <summary>
+        /// The delivery time of the message as a DateTimeOffset.
         /// </summary>
         public DateTimeOffset DeliverAtTimeAsDateTimeOffset
         {
@@ -47,7 +56,7 @@ namespace DotPulsar
         }
 
         /// <summary>
-        /// The event time of the message (unix time in milliseconds).
+        /// The event time of the message as unix time in milliseconds.
         /// </summary>
         public ulong EventTime
         {
@@ -56,7 +65,16 @@ namespace DotPulsar
         }
 
         /// <summary>
-        /// The event time of the message.
+        /// The event time of the message as an UTC DateTime.
+        /// </summary>
+        public DateTime EventTimeAsDateTime
+        {
+            get => Metadata.GetEventTimeAsDateTime();
+            set => Metadata.SetEventTime(value);
+        }
+
+        /// <summary>
+        /// The event time of the message as a DateTimeOffset.
         /// </summary>
         public DateTimeOffset EventTimeAsDateTimeOffset
         {