Unable to connect to Azure Service Bus from Spring Boot application using ServiceBusSenderClient with DefaultAzureCredentialBuilder

2024-12-11T03:04:53.0166667+00:00

Getting below error when trying to connect to Azure Service Bus from Spring boot - java application. ConnectionHandler -- {"az.sdk.message":"onTransportError","connectionId":"MF_1fc057_1733885826934","errorCondition":"proton:io","errorDescription":"Connection refused","hostName":"mytest-notifications-std-demo-ns.servicebus.windows.net"}.

.ReactorConnection -- {"az.sdk.message":"Disposing of ReactorConnection.","connectionId":"MF_1fc057_1733885826934","isTransient":false,"isInitiatedByClient":false,"shutdownMessage":"Connection refused, errorContext[NAMESPACE: treasury-notifications-std-demo-ns.servicebus.windows.net. ERROR CONTEXT: N/A]"}

Note: I have logged in with az login and tried telnet mytest--notifications-std-demo-ns.servicebus.windows.net 5671 which is successful.

Sample code:

public class TestServiceBusConnection {
    public static void main(String[] args) {
        try (ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
                .fullyQualifiedNamespace("mytest-notifications-std-demo-ns.servicebus.windows.net")
                .credential(new DefaultAzureCredentialBuilder().build())
                .sender()
                .queueName("mytest-notifications-test-queue")
                .buildClient()) {

            senderClient.sendMessage(new ServiceBusMessage("Test message from local machine"));
            System.out.println("Message sent successfully!");
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }
    }
}
Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
653 questions
Azure Spring Apps
Azure Spring Apps
An Azure platform as a service for running Spring Boot applications at cloud scale. Previously known as Azure Spring Cloud.
130 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.