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

djensen 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 771e852  Add possibility of setting a prefix for the subscription for 
readers. (#248)
771e852 is described below

commit 771e852c1ddf0eb91ea701f7e1b22d8913acdb2d
Author: Nikolaj Lund Sørensen <[email protected]>
AuthorDate: Wed Jan 22 09:28:51 2025 +0100

    Add possibility of setting a prefix for the subscription for readers. (#248)
---
 src/DotPulsar/Abstractions/IReaderBuilder.cs |  7 ++++++-
 src/DotPulsar/Internal/Reader.cs             |  5 +++++
 src/DotPulsar/Internal/ReaderBuilder.cs      | 13 +++++++++++--
 src/DotPulsar/ReaderOptions.cs               |  8 +++++++-
 4 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/src/DotPulsar/Abstractions/IReaderBuilder.cs 
b/src/DotPulsar/Abstractions/IReaderBuilder.cs
index 184c4b0..dfb062b 100644
--- a/src/DotPulsar/Abstractions/IReaderBuilder.cs
+++ b/src/DotPulsar/Abstractions/IReaderBuilder.cs
@@ -1,4 +1,4 @@
-/*
+/*
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -49,6 +49,11 @@ public interface IReaderBuilder<TMessage>
     /// </summary>
     IReaderBuilder<TMessage> Topic(string topic);
 
+    /// <summary>
+    /// Set the prefix for the subscription being created by this reader. This 
is optional.
+    /// </summary>
+    IReaderBuilder<TMessage> SubscriptionNamePrefix(string 
subscriptionNamePrefix);
+
     /// <summary>
     /// Create the reader.
     /// </summary>
diff --git a/src/DotPulsar/Internal/Reader.cs b/src/DotPulsar/Internal/Reader.cs
index 93f07f6..b77a466 100644
--- a/src/DotPulsar/Internal/Reader.cs
+++ b/src/DotPulsar/Internal/Reader.cs
@@ -270,6 +270,11 @@ public sealed class Reader<TMessage> : IReader<TMessage>
     {
         var correlationId = Guid.NewGuid();
         var subscription = $"Reader-{correlationId:N}";
+        if (!string.IsNullOrEmpty(_readerOptions.SubscriptionNamePrefix))
+        {
+            subscription = 
$"{_readerOptions.SubscriptionNamePrefix}-{subscription}";
+        }
+
         var subscribe = new CommandSubscribe
         {
             ConsumerName = _readerOptions.ReaderName ?? subscription,
diff --git a/src/DotPulsar/Internal/ReaderBuilder.cs 
b/src/DotPulsar/Internal/ReaderBuilder.cs
index deb69cb..968434c 100644
--- a/src/DotPulsar/Internal/ReaderBuilder.cs
+++ b/src/DotPulsar/Internal/ReaderBuilder.cs
@@ -1,4 +1,4 @@
-/*
+/*
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -26,6 +26,8 @@ public sealed class ReaderBuilder<TMessage> : 
IReaderBuilder<TMessage>
     private bool _readCompacted;
     private MessageId? _startMessageId;
     private string? _topic;
+    private string? _subscriptionNamePrefix;
+
     private IHandleStateChanged<ReaderStateChanged>? _stateChangedHandler;
 
     public ReaderBuilder(IPulsarClient pulsarClient, ISchema<TMessage> schema)
@@ -72,6 +74,12 @@ public sealed class ReaderBuilder<TMessage> : 
IReaderBuilder<TMessage>
         return this;
     }
 
+    public IReaderBuilder<TMessage> SubscriptionNamePrefix(string 
subscriptionNamePrefix)
+    {
+        _subscriptionNamePrefix = subscriptionNamePrefix;
+        return this;
+    }
+
     public IReader<TMessage> Create()
     {
         if (_startMessageId is null)
@@ -85,7 +93,8 @@ public sealed class ReaderBuilder<TMessage> : 
IReaderBuilder<TMessage>
             MessagePrefetchCount = _messagePrefetchCount,
             ReadCompacted = _readCompacted,
             ReaderName = _readerName,
-            StateChangedHandler = _stateChangedHandler
+            StateChangedHandler = _stateChangedHandler,
+            SubscriptionNamePrefix = _subscriptionNamePrefix ?? string.Empty
         };
 
         return _pulsarClient.CreateReader(options);
diff --git a/src/DotPulsar/ReaderOptions.cs b/src/DotPulsar/ReaderOptions.cs
index a7f76a0..9ddeb4b 100644
--- a/src/DotPulsar/ReaderOptions.cs
+++ b/src/DotPulsar/ReaderOptions.cs
@@ -1,4 +1,4 @@
-/*
+/*
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -77,4 +77,10 @@ public sealed class ReaderOptions<TMessage>
     /// Set the topic for this reader. This is required.
     /// </summary>
     public string Topic { get; set; }
+
+    /// <summary>
+    /// The prefix for the subscription being created behind the scenes for 
the reader. This is optional
+    /// It can be necessary to set this if the policy for access is set to 
SubscriptionPrefix.
+    /// </summary>
+    public string SubscriptionNamePrefix { get; set; } = string.Empty;
 }

Reply via email to