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 069251d Renamed SubscriptionNamePrefix to SubscriptionRolePrefix
(mirroring the naming in the Java client) and added the ability to set the
subscription name for the reader
069251d is described below
commit 069251df3fc1875ca36c8e85ce067d1aae46e714
Author: Daniel Blankensteiner <[email protected]>
AuthorDate: Wed Jan 22 09:55:23 2025 +0100
Renamed SubscriptionNamePrefix to SubscriptionRolePrefix (mirroring the
naming in the Java client) and added the ability to set the subscription name
for the reader
---
CHANGELOG.md | 4 ++++
src/DotPulsar/Abstractions/IReaderBuilder.cs | 13 +++++++++----
src/DotPulsar/Internal/Reader.cs | 7 ++-----
src/DotPulsar/Internal/ReaderBuilder.cs | 21 +++++++++++++++------
src/DotPulsar/ReaderOptions.cs | 20 +++++++++++++++-----
5 files changed, 45 insertions(+), 20 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4ea04f6..25878d0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,10 @@ The format is based on [Keep a
Changelog](https://keepachangelog.com/en/1.1.0/)
## [Unreleased]
+### Added
+
+- The subscription name and role prefix can now be set for the reader
+
### Changed
- **Breaking**: The consumer, reader, and producer now implements IStateHolder
instead of IState
diff --git a/src/DotPulsar/Abstractions/IReaderBuilder.cs
b/src/DotPulsar/Abstractions/IReaderBuilder.cs
index dfb062b..747ea6b 100644
--- a/src/DotPulsar/Abstractions/IReaderBuilder.cs
+++ b/src/DotPulsar/Abstractions/IReaderBuilder.cs
@@ -45,14 +45,19 @@ public interface IReaderBuilder<TMessage>
IReaderBuilder<TMessage>
StateChangedHandler(IHandleStateChanged<ReaderStateChanged> handler);
/// <summary>
- /// Set the topic for this reader. This is required.
+ /// Set the subscription name for this reader. This is optional.
/// </summary>
- IReaderBuilder<TMessage> Topic(string topic);
+ IReaderBuilder<TMessage> SubscriptionName(string name);
/// <summary>
- /// Set the prefix for the subscription being created by this reader. This
is optional.
+ /// Set the subscription role prefix for this reader. The default is
'Reader'. This is optional.
/// </summary>
- IReaderBuilder<TMessage> SubscriptionNamePrefix(string
subscriptionNamePrefix);
+ IReaderBuilder<TMessage> SubscriptionRolePrefix(string
subscriptionRolePrefix);
+
+ /// <summary>
+ /// Set the topic for this reader. This is required.
+ /// </summary>
+ IReaderBuilder<TMessage> Topic(string topic);
/// <summary>
/// Create the reader.
diff --git a/src/DotPulsar/Internal/Reader.cs b/src/DotPulsar/Internal/Reader.cs
index b77a466..2043927 100644
--- a/src/DotPulsar/Internal/Reader.cs
+++ b/src/DotPulsar/Internal/Reader.cs
@@ -269,11 +269,8 @@ public sealed class Reader<TMessage> : IReader<TMessage>
private SubReader<TMessage> CreateSubReader(string topic)
{
var correlationId = Guid.NewGuid();
- var subscription = $"Reader-{correlationId:N}";
- if (!string.IsNullOrEmpty(_readerOptions.SubscriptionNamePrefix))
- {
- subscription =
$"{_readerOptions.SubscriptionNamePrefix}-{subscription}";
- }
+
+ var subscription = _readerOptions.SubscriptionName is not null ?
_readerOptions.SubscriptionName :
$"{_readerOptions.SubscriptionRolePrefix}-{correlationId:N}";
var subscribe = new CommandSubscribe
{
diff --git a/src/DotPulsar/Internal/ReaderBuilder.cs
b/src/DotPulsar/Internal/ReaderBuilder.cs
index 968434c..78fbbac 100644
--- a/src/DotPulsar/Internal/ReaderBuilder.cs
+++ b/src/DotPulsar/Internal/ReaderBuilder.cs
@@ -26,7 +26,8 @@ public sealed class ReaderBuilder<TMessage> :
IReaderBuilder<TMessage>
private bool _readCompacted;
private MessageId? _startMessageId;
private string? _topic;
- private string? _subscriptionNamePrefix;
+ private string? _subscriptionName;
+ private string _subscriptionRolePrefix;
private IHandleStateChanged<ReaderStateChanged>? _stateChangedHandler;
@@ -36,6 +37,7 @@ public sealed class ReaderBuilder<TMessage> :
IReaderBuilder<TMessage>
_schema = schema;
_messagePrefetchCount =
ReaderOptions<TMessage>.DefaultMessagePrefetchCount;
_readCompacted = ReaderOptions<TMessage>.DefaultReadCompacted;
+ _subscriptionRolePrefix =
ReaderOptions<TMessage>.DefaultSubscriptionRolePrefix;
}
public IReaderBuilder<TMessage> MessagePrefetchCount(uint count)
@@ -68,15 +70,21 @@ public sealed class ReaderBuilder<TMessage> :
IReaderBuilder<TMessage>
return this;
}
- public IReaderBuilder<TMessage> Topic(string topic)
+ public IReaderBuilder<TMessage> SubscriptionName(string subscriptionName)
{
- _topic = topic;
+ _subscriptionName = subscriptionName;
+ return this;
+ }
+
+ public IReaderBuilder<TMessage> SubscriptionRolePrefix(string
subscriptionRolePrefix)
+ {
+ _subscriptionRolePrefix = subscriptionRolePrefix;
return this;
}
- public IReaderBuilder<TMessage> SubscriptionNamePrefix(string
subscriptionNamePrefix)
+ public IReaderBuilder<TMessage> Topic(string topic)
{
- _subscriptionNamePrefix = subscriptionNamePrefix;
+ _topic = topic;
return this;
}
@@ -94,7 +102,8 @@ public sealed class ReaderBuilder<TMessage> :
IReaderBuilder<TMessage>
ReadCompacted = _readCompacted,
ReaderName = _readerName,
StateChangedHandler = _stateChangedHandler,
- SubscriptionNamePrefix = _subscriptionNamePrefix ?? string.Empty
+ SubscriptionName = _subscriptionName,
+ SubscriptionRolePrefix = _subscriptionRolePrefix
};
return _pulsarClient.CreateReader(options);
diff --git a/src/DotPulsar/ReaderOptions.cs b/src/DotPulsar/ReaderOptions.cs
index 9ddeb4b..1d626b1 100644
--- a/src/DotPulsar/ReaderOptions.cs
+++ b/src/DotPulsar/ReaderOptions.cs
@@ -31,6 +31,11 @@ public sealed class ReaderOptions<TMessage>
/// </summary>
public static readonly bool DefaultReadCompacted = false;
+ /// <summary>
+ /// The default subscription role prefix.
+ /// </summary>
+ public static readonly string DefaultSubscriptionRolePrefix = "Reader";
+
/// <summary>
/// Initializes a new instance using the specified startMessageId and
topic.
/// </summary>
@@ -39,6 +44,7 @@ public sealed class ReaderOptions<TMessage>
MessagePrefetchCount = DefaultMessagePrefetchCount;
ReadCompacted = DefaultReadCompacted;
StartMessageId = startMessageId;
+ SubscriptionRolePrefix = DefaultSubscriptionRolePrefix;
Topic = topic;
Schema = schema;
}
@@ -74,13 +80,17 @@ public sealed class ReaderOptions<TMessage>
public IHandleStateChanged<ReaderStateChanged>? StateChangedHandler { get;
set; }
/// <summary>
- /// Set the topic for this reader. This is required.
+ /// Set the subscription name for this reader. This is optional.
/// </summary>
- public string Topic { get; set; }
+ public string? SubscriptionName { 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.
+ /// Set the subscription role prefix for this reader. The default is
'Reader'. This is optional.
/// </summary>
- public string SubscriptionNamePrefix { get; set; } = string.Empty;
+ public string SubscriptionRolePrefix { get; set; }
+
+ /// <summary>
+ /// Set the topic for this reader. This is required.
+ /// </summary>
+ public string Topic { get; set; }
}