apache / pulsar-dotpulsar

The official .NET client library for Apache Pulsar

Home Page:https://pulsar.apache.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to Implement Delayed Message Delivery

Ceyword opened this issue · comments

Hello,
I have the below code:

await Task.Run(async () =>
{
const string myTopic = "TestScheduling";
await using var client = DotPulsar.PulsarClient.Builder().ServiceUrl(new Uri("pulsar://127.0.0.1:6650")).Build();
await using var producer = client.NewProducer(DotPulsar.Schema.String).Topic(myTopic).Create();
await producer.Send("Hello Pulsar now");
await producer.Send(new DotPulsar.MessageMetadata { DeliverAtTimeAsDateTime = DateTime.UtcNow.AddMinutes(10) }, "Hello Pulsar after 10 minutes");
await using var consumer = client.NewConsumer(DotPulsar.Schema.String)
.SubscriptionName("MySubscription")
.Topic(myTopic)
.InitialPosition(DotPulsar.SubscriptionInitialPosition.Latest)
.Create();
var messages = consumer.Messages();
await foreach (var message in messages)
{
Console.WriteLine($"Received: {Encoding.UTF8.GetString(message.Data.ToArray())} @ {DateTime.UtcNow}");
await consumer.Acknowledge(message);
}
});

I expect the second message to be delivered after about ten minutes. Both got delivered, immediately. What am I missing to successfully implement delayed message delivery?

Thank you

Hi @Ceyword
From https://pulsar.apache.org/docs/3.1.x/concepts-messaging/#delayed-message-delivery

Only shared and key-shared subscriptions support delayed message delivery. In other subscriptions, delayed messages are dispatched immediately.

commented

Hi @Ceyword. It works but as @blankensteiner pointed out. You need to set it to be a shared or key-shared subscription.

The following code will hold the message for 1 min.

Node the line
.SubscriptionType(SubscriptionType.Shared)

      await Task.Run(async () =>
      {
          const string myTopic = "TestScheduling";
          await using var client = DotPulsar.PulsarClient.Builder().ServiceUrl(new Uri("pulsar://127.0.0.1:6650")).Build();
          await using var producer = client.NewProducer(DotPulsar.Schema.String).Topic(myTopic).Create();
          await producer.Send("Hello Pulsar now");
          await producer.Send(new DotPulsar.MessageMetadata { DeliverAtTimeAsDateTime = DateTime.UtcNow.AddMinutes(1) }, "Hello Pulsar after 1 minutes");
          await using var consumer = client.NewConsumer(DotPulsar.Schema.String)
              .SubscriptionName("MySubscription")
              .Topic(myTopic)
              .InitialPosition(DotPulsar.SubscriptionInitialPosition.Latest)
              .SubscriptionType(SubscriptionType.Shared)
              .Create();
          var messages = consumer.Messages();
          await foreach (var message in messages)
          {
              Console.WriteLine($"Received: {Encoding.UTF8.GetString(message.Data.ToArray())} @ {DateTime.UtcNow}");
              await consumer.Acknowledge(message);
          }
      });

image

I hope it makes sense 😄
If this is the answer you are looking for, please remember to close the issue.

Thank you much @blankensteiner and @entvex . Yes it makes sense @entvex.

I will be closing this issue as resolved.