Tselofan
Tselofan

Reputation: 211

Azure Service Bus topic with exclusive, autodelete subscriptions with a generated name

How can I create topic and subscribe on it multiple independent subscribers with different subscriptions for each without specifying subscription names. If the subscriber disconnect, the corresponding subscription should be automatic removed. This case can be realised with rabbitmq server for logging purposes, for example. https://www.rabbitmq.com/tutorials/tutorial-three-dotnet.html.

In the .NET client, when we supply no parameters to queueDeclare() we create a non-durable, exclusive, autodelete queue with a generated name.

If it is impossible, how can I wrap .net client for realising this case? Thanks.

Upvotes: 2

Views: 1060

Answers (1)

Fei Han
Fei Han

Reputation: 27803

As you mentioned in your comment, you can create new subscription with unique GUID as subscription name when the client connect (or app start). And specifying SubscriptionDescription.AutoDeleteOnIdle property to set the TimeSpan idle interval after which the subscription is automatically deleted.

var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

var subscriptionname = Guid.NewGuid().ToString();

if (!namespaceManager.SubscriptionExists(topicname, subscriptionname))
{
    SqlFilter updatedMessagesFilter =
        new SqlFilter("mypro = 'test'");

    namespaceManager.CreateSubscription(new SubscriptionDescription(topicname, subscriptionname) { AutoDeleteOnIdle = TimeSpan.FromMinutes(5) },
        updatedMessagesFilter);
} 

When client disconnect, you can delete the subscription manually.

if (namespaceManager.SubscriptionExists(topicname, subscriptionname))
{
    namespaceManager.DeleteSubscription(topicname, subscriptionname);
}

Note: to guarantee 100% delete subscription, you can retain information about client and subscriptionname (unique GUID) in an external storage, and every time when a client connect/reconnect, you can detect if a record exists in external storage that indicates a subscription (used by this client before) is still not be deleted for current client, if the record exists, you can delete that subscription before you create a new subscription.

Upvotes: 3

Related Questions