question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE REQ] Peek scheduled messages.

See original GitHub issue

Library name

Azure.Messaging.ServiceBus

Please describe the feature.

I’m not sure if this should be a bug instead. Feel free to change this as necessary.

In the previous iterations of the Azure Services Bus client libraries for .NET (WindowsAzure.ServiceBus and Microsoft.ServiceBus.Messaging) it was possible to peek scheduled messages with TopicClient.Peek() and TopicClient.PeekAsync()

According to this previously closed issue:

Scheduled messages reside in the topic until the scheduled time, and users should peek into the topic if they want to see the scheduled messages. Scheduled Messages can not be peeked from a subscription since they are not present in the subscription.

(strangely, the information added to the docs in this issue, seems to be gone again)

However in Azure.Messaging.ServiceBus, there is no equivalent to TopicClient, Instances of ServiceBusReceiver are always configured to a subscription on a topic, and never directly to the topic. Thus there is no way to peek scheduled messages, using Azure.Messaging.ServiceBus, despite the documentation here stating so.

Seeing as the the service bus API’s and earlier .NET client libraries support peeking scheduled messages directly on a topic, Azure.Messaging.ServiceBus should as well.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
MikalJcommented, Jun 2, 2022

Hello @JoshLove-msft

I had worked around the issue, and have just only found the time to follow up on your response. I was not sure which overload of CreateReceiver you suggested I should use: public virtual ServiceBusReceiver CreateReceiver(string topicName, string subscriptionName) with null or an empty string as subscriptionName or public virtual ServiceBusReceiver CreateReceiver(string queueName), so I wrote some tests to figure it out, and found some interesting results, you might be interested in.

Using CreateReceiver(string topicName, string subscriptionName) with null or an empty string as subscriptionName, will work if I first schedule a message using a sender from the same underlying ServiceBusClient. But will mostly fail if I don’t schedule a message first, and just peek on an empty topic.

Using CreateReceiver(string queueName) but passing the name of the topic rather than the name of a queue as the parameter suggests, always works. So I can confirm that it is possible to peek scheduled messages.

The test:

using System;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
using NUnit.Framework;

[TestFixture]
public class ServiceBusTopicPeekingTest
{

    private const string ConnectionString = "INSERT CONNECTIONSTRING HERE";
    private const string Topic = "testtopic";
    private ServiceBusClient serviceBusClient;

    [SetUp]
    public async Task Setup()
    {
        serviceBusClient = new ServiceBusClient(ConnectionString);
    }

    [TestCase((string)null)]
    [TestCase("")]
    public async Task Can_Peek_Scheduled_Message_On_Topic_Supplying_Blank_SubscriptionName(string subscription)
    {
        var sender = serviceBusClient.CreateSender(Topic);
        var receiver = serviceBusClient.CreateReceiver(Topic, subscription);
        var seqNo = await sender.ScheduleMessageAsync(new ServiceBusMessage("42"), DateTimeOffset.UtcNow.AddSeconds(10));
        var peekedMessage = await receiver.PeekMessageAsync();

        Assert.That(peekedMessage, Is.Not.Null);
        Assert.That(peekedMessage.SequenceNumber, Is.EqualTo(seqNo));

        await sender.CancelScheduledMessageAsync(seqNo);
    }

    [TestCase((string)null)]
    [TestCase("")]
    public async Task Can_Peek_Scheduled_Message_On_Empty_Topic_Supplying_Blank_SubscriptionName(string subscription)
    {
        // This test will occasionally pass, but mostly fail
        var receiver = serviceBusClient.CreateReceiver(Topic, subscription);
        Assert.DoesNotThrowAsync(() => receiver.PeekMessageAsync());
    }

    [Test]
    public async Task Can_Peek_Scheduled_Message_On_Topic_Without_Supplying_SubscriptionName()
    {
        var sender = serviceBusClient.CreateSender(Topic);
        var receiver = serviceBusClient.CreateReceiver(Topic);
        var seqNo = await sender.ScheduleMessageAsync(new ServiceBusMessage("42"), DateTimeOffset.UtcNow.AddSeconds(10));
        var peekedMessage = await receiver.PeekMessageAsync();

        Assert.That(peekedMessage, Is.Not.Null);
        Assert.That(peekedMessage.SequenceNumber, Is.EqualTo(seqNo));

        await sender.CancelScheduledMessageAsync(seqNo);
    }
    [Test]
    public async Task Can_Peek_Scheduled_Message_On_Empty_Topic_Without_Supplying_SubscriptionName()
    {
        var receiver = serviceBusClient.CreateReceiver(Topic);
        Assert.DoesNotThrowAsync(() => receiver.PeekMessageAsync());
    }
}

I wish the documentation (and possible method signature) reflected that ´CreateReceiver(string queueName)´ can be used to peek scheduled messages on a topic. But will otherwise consider the issue resolved.

0reactions
msftbot[bot]commented, Jun 2, 2022

Hi kimpenhaus, only the original author of the issue can ask that it be unresolved. Please open a new issue with your scenario and details if you would like to discuss this topic with the team.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Peek scheduled messages in Azure ServiceBus topic
Scheduled messages reside in the topic until the scheduled time, and users should peek into the topic if they want to see the...
Read more >
Azure Service bus || Peek/Delete scheduled message ...
Pull messages from the service bus entity using the 'Peek' method, this method fetches all the active (available to be received) and scheduled...
Read more >
Azure Service Bus Essentials — Scheduled Messages
Viewing scheduled messages​​ Although the scheduled messages can't be received until their enqueue time, you can still peek at them. Peeking is a ......
Read more >
Sending Scheduled Messages on Azure Service Bus - YouTube
We also show how to cancel a message that has been scheduled in the future and how to peek to see those messages...
Read more >
Azure Service Bus and its Complete Overview
Azure Service Bus supports a dead-letter queue (DLQ) to hold messages that cannot be delivered to any receiver, or messages that cannot be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found