1

How can I send messages to a topic using Azure managed identity in java? Right now im using the connectionString to send the message to the topic.

   ServiceBusSenderClient senderClient = new ServiceBusClientBuilder()
       .connectionString(connectionString)
       .sender()
       .topicName(topicName)
       .buildClient();

In the Azure SDK for java, i could only find this example, which is for service bus queue

ServiceBusSenderAsyncClient sender = new ServiceBusClientBuilder()
           .credential("<<fully-qualified-namespace>>", credential)
           .sender()
           .queueName("<<queue-name>>")
           .buildAsyncClient();
LegendMVB
  • 23
  • 7

1 Answers1

0

Your second snippet is mostly correct; you're missing the step of creating the credential that you're passing to the builder. That is discussed in the Authorizing with DefaultAzureCredential section of the overview and looks like:

TokenCredential credential = new DefaultAzureCredentialBuilder()
    .build();

ServiceBusReceiverAsyncClient receiver = new ServiceBusClientBuilder()
    .credential("<<fully-qualified-namespace>>", credential)
    .receiver()
    .queueName("<<queue-name>>")
    .buildAsyncClient();

Service Bus can use any of the Azure.Identity credentials for authorization. DefaultAzureCredentialBuilder is demonstrated only because it is a chained credential that allows for success in a variety of scenarios. More information can be found in the Azure.Identity overview.

If you'd prefer to restrict authorization to only a managed identity, you can do so by using ManagedIdentityCredentialBuilder rather than the default credential. An example of creating the can be found here. It can then be passed to Service Bus in the same manner as the default credential.

Jesse Squire
  • 6,107
  • 1
  • 27
  • 30