Unable to connect to Azure Service Bus from Spring Boot application using ServiceBusSenderClient with DefaultAzureCredentialBuilder
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);
}
}
}