How to clear pending message from azure service bus? - azure

I am using azure service bus for receiving message to processing my background process. In my background process I want to clear already processed messages from azure service bus. Is there any way or method to clear azure service bus messages???

Read How to receive messages from a queue and make sure you use _queueClient.Complete() or _queueClient.Abandon() to finish with each message.

You can use "Microsoft.ServiceBus.Messaging" and purge messages by en-queue time. Receive the messages, filter by ScheduledEnqueueTime and perform purge when the message has been en-queued at the specific time.
using Microsoft.ServiceBus.Messaging;
MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);
var queueClient = messagingFactory.CreateQueueClient(resourceName, ReceiveMode.PeekLock);
var client = messagingFactory.CreateMessageReceiver(resourceName, ReceiveMode.PeekLock);
BrokeredMessage message = client.Receive();
if (message.EnqueuedTimeUtc < MessageEnqueuedDateTime)
{
message.Complete();
}

Related

How to send messages to the dead letter queue in Azure Service Bus using MassTransit?

I'm new to Azure Service Bus and MassTransit. Now I'm experimenting with messages and I want to send a Message that goes into the Dead Letter Queue of the Azure Service Bus. What should such a message look like? Will it only appear in the Dead Letter Queue if there is no consumer? And how should I consume a Dead Letter Message by creating a dead letter consumer?
Thanks in advance :)
Consumer:
public Task Consume(ConsumeContext<IEventCreated> context)
{
try
{
var serializedMessage = JsonSerializer.Serialize(context.Message, new JsonSerializerOptions());
logger.LogInformation("Event consumed: {serializedMessage}", serializedMessage);
return Task.CompletedTask;
}
catch (Exception ex)
{
logger.LogError(ex, "Error while processing message {messageId}. Retry attempt {retryAttempt}", context.MessageId, context.GetRetryAttempt());
throw;
}
}
My consumer definition:
public class EventCreatedConsumerDefinition : ConsumerDefinition<EventCreatedConsumer>
{
protected override void ConfigureConsumer(IReceiveEndpointConfigurator endpointConfigurator,IConsumerConfigurator<EventCreatedConsumer> consumerConfigurator)
{
endpointConfigurator.UseMessageRetry(r => r.Interval(5, 500));
endpointConfigurator.ConfigureDeadLetterQueueDeadLetterTransport();
endpointConfigurator.ConfigureDeadLetterQueueErrorTransport();
}
}
When configured to use the dead-letter queue (DLQ), MassTransit will move faulted or skipped messages to the DLQ instead of creating separate _error or _skipped queues.
If a message consumer throws an exception, and all retry/redelivery limits are reached, the message is dead-lettered using the Azure SDK along with additional message headers detailing the reason/exceptions that occurred.
As for how to deal with messages in the DLQ, you can use Azure Service Bus Explorer (in the Azure Portal) or the old client Windows application to examine/move the messages, or consider other options like a SaaS service that can monitor/move those messages for you.
Message Aid is an early stage tool that does this, author is the co-founder of MassTransit.

Azure Function v2 with ServiceBusQueueTrigger in NodeJS (index.js) - How to send message to Deadletter Queue if there is exception

I am using ServiceBusQueueTrigger in NodeJS. When there is error, it is expected to put message to Deadletter Queue instead of Complete it. How can I do that?
Below is my code in index.js:
module.exports = async function(context, message) {
...
}
According to my research, if the function fails, it will call Abandon method and tell service bus message processing failed. The service bus will check if the DeliveryCount of this message reaches MaxDeliveryCount. If it reaches, service bus will move it to Deadletter Queue. Otherwhise, service bus will let the function continue to process it. For more details, please refer to https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus#trigger---poison-messages and https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dead-letter-queueshttps://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dead-letter-queues

How to subscribe message from Azure service bus topic subscription to web api?

We have multiple webapi(microservices). To communicate between these micro services we are using Azure service bus Topic\Subscription. How webapi controller can get the message subscription so that api can process the message and insert to database. I don't want to use any azure functions. I want subscribe message in my webapi(micro service directly).
You may create a client for the topic to subscribe the message and set the options to complete after the necessary process is done.
please find the below sample code, where you can read the subscription messages from a topic.
you can use WindowsAzure.ServiceBus -Nuget Package
Sample Code:
using Microsoft.ServiceBus.Messaging;
using Microsoft.ServiceBus;
var con = "your connection string for the topic";
var topic = "your topic name";
var subclient = SubscriptionClient.CreateFromConnectionString(con, topic, "yourSubscriptionName");
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
client.OnMessage(message =>
{
Console.WriteLine("Message:" + message.GetBody<string>());
message.Complete();
}, options);
you may put it in separate method and use as needful.
To poll you may create a webjob, or you can place the code which triggers the web api, so that it will poll the messages at the same time.

Azure Servicebus: Receive all the message from one session id of a queue

I have 2 question for which I failed to find any solution.
Is there any built in way in azure service bus queue, where I can receive messages only from one session (for that sessionId) in my session handler.
Also is there any way I can receive messages only from the deadlettered queue for that session alone.
I can do this programmatically in my code, but I don't want to write if else logic in my code.
Is there any built in way in azure service bus queue, where I can receive messages only from one session (for that sessionId) in my session handler.
As Sean Feldman mentioned Queue​Client.​Accept​Message​Session​Async(sessionID) allows to achieve that for a normal queue.
If want to use Queue​Client.​Accept​Message​Session​Async(sessionID) to receive queue message, we need to create the with RequiresSession true,
var description = new QueueDescription(QueueName) 
{ 
    RequiresSession = true 
}; 
namespaceManager.CreateQueue(description);
Also is there any way I can receive messages only from the deadlettered queue for that session alone.
Base on my test we can't get the dead-letter queue message from the deadletter queue with following code
var dlqName= QueueClient.FormatDeadLetterPath(queueName);
var dlqClient = QueueClient.CreateFromConnectionString(connectionstring, dfQueue);
var gid = Guid.NewGuid().ToString();
var messageSession = client.AcceptMessageSession(gid);
var receiveMessage = messageSession.Receive();
Cannot create a MessageSession for a sub-queue. TrackingId:06a39820-7bf6-412d-ab31-80ef5c174a12_G20_B31, SystemTracker:tomsbservice:Queue: queuename |$DeadLetterQueue
My workaround is that we could get the dead-letter queue message and submit to queue or another normal queue.
I don't think you can do that for Service Bus queues. You can archieve something similar with Service Bus topic and filtered subscription.
So, you send your messages to a topic, and then you'd have to create a subscription per session ID similar to this:
var filter = new SqlFilter(#"sys.SessionId = \"SESSIONID\"");
namespaceManager.CreateSubscription("YOUR_TOPIC", "SESSIONID_SUBSCRIPTION", filter);
and then just receive messages from this subscription:
var client = new SubscriptionClient("...", "YOUR_TOPIC", "SESSIONID_SUBSCRIPTION");
var message = client.Receive();
Message Sessions feature is specifically designed for that.
Queue​Client.​Accept​Message​Session​Async(sessionID) allows to achieve that for a normal queue.
As for the DLQ - not sure if that works.

Why use a QueueClient vs MessageFactory?

In Azure Service Bus, you can send a brokered message using QueueClient and MessageFactory. I would like to know why would you want to use one over the other.
Azure Service Bus provides different way to send/receive messages.
You can use the QueueClient to send and receive message to/from a queue.
You can use the TopicClient to send message to a topic
And you can use the SubscriptionClient to receive message from a subscription.
Using MessageSender and MessageReceiver, you create sender and receiver that are entity type invariant:
var factory = MessagingFactory.CreateFromConnectionString("MyConnectionString");
A MessageSender can send messages to both topic or queue:
var sender = factory.CreateMessageSender("Queue ou topic path");
A MessageReceiver ca receive messages from both queue and subscription:
var receiver = factory.CreateMessageReceiver("Queue ou subscription path");
Theses abstractions can give you more flexibility if you need to switch from a queue to a topic or vice versa because you just need to change the path of the service bus entity (This could be in your configuration file) so no code change needed. Using QueueClient, TopicClient, SubscriptionClient, you'll have to change your code if you want to move from a queue to a topic.
So my advice is to always use a MessageReceiver/MessageSender when you have to send/receive message from/to a an Azure ServiceBus queue topic/subscription.
NOTE: This does not apply for Eventhub which has a different implementation.

Resources