<p>How could I get the number of unprocessed message count from Azure service bus Queues and based on the count, auto scale the nodes deployed on Azure Kubernetes Services?</p>
<p>I was able to get one of the links which uses Azure Monitor to get the count of messages in the queue and using HPA configuration scale up/down.</p>
<p><a href="https://ferhatvurucu.medium.com/queue-based-scaling-on-azure-kubernetes-service-aks-with-helm-e1d579cd77c3" rel="nofollow noreferrer">https://ferhatvurucu.medium.com/queue-based-scaling-on-azure-kubernetes-service-aks-with-helm-e1d579cd77c3</a></p>
<p>Is there any other way we can get the count of messages in the Azure Service Bus queue using Java Code or without using the monitor? Appreciate it if anyone could provide links to read up more on this.</p>

Deepthi
<p>Ideally, monitoring is the best option here if you are using the <strong>AKS monitoring</strong> or <strong>Prometheus</strong> for checking the queue size and scaling the number of PODs using the HPA.</p>
<p>Here is one nice example of same scenario with the RabbitMQ queue and using the <code>Prometheus</code> with custom metrics. <a href="https://ryanbaker.io/2019-10-07-scaling-rabbitmq-on-k8s/" rel="nofollow noreferrer">https://ryanbaker.io/2019-10-07-scaling-rabbitmq-on-k8s/</a></p>
<p>However if you looking for a custom solution using Java or any other code you can implement it.</p>
<p>Here is <strong>GetMessage counter</strong> : <a href="https://learn.microsoft.com/en-us/azure/service-bus-messaging/message-counters" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/azure/service-bus-messaging/message-counters</a></p>
<p>The returned MessageCountDetails object has the following properties: <code>ActiveMessageCount</code>, <code>DeadLetterMessageCount</code>, <code>ScheduledMessageCount</code>, <code>TransferDeadLetterMessageCount</code>, <code>TransferMessageCount</code>.</p>
<p>Some java sample : <a href="https://learn.microsoft.com/en-us/samples/azure/azure-sdk-for-java/servicebus-samples/" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/samples/azure/azure-sdk-for-java/servicebus-samples/</a></p>
<p>This will only give you the count of messages for scaling replicas further you might have to use the <code>K8s</code> client library to scale up/down replicas of deployment.</p>
