NumberFormatException in connecting Azure Service Bus using Managed Identity

222 Views Asked by At

I am trying to connect to Azure Service Bus from my webjob using Managed Identities. But unfortunately, I am getting the following error

com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: For input string: "10/10/2020 6:50:38 AM +00:00"

I have enabled Identity and assigned Contributor role for connecting to Service bus. Also, in the code, I have used ClientSettings to create a connection with Service bus.

ClientFactory.createMessageReceiverFromEntityPathAsync(namespace, queueName,new ClientSettings(TokenProvider.createManagedServiceIdentityTokenProvider()), ReceiveMode.RECEIVEANDDELETE).get();

Any idea how to resolve this exception, as it is thrown from built-in class ManagedServiceIdentityTokenProvider. Any help would be really helpful.

Thanks in advance.

EDIT - I updated the service bus package to 3.2.0. Now I can't see NumberFormatException but I am getting java.lang.RuntimeException: java.net.SocketException: Permission denied: connect even though I have provided the 'Owner' role to the webjob.

1

There are 1 best solutions below

4
On

If you want to connect Azure Service bus with Azure Managed Identity, we need to assign some special role(Azure Service Bus Data Owner, Azure Service Bus Data Sender and Azure Service Bus Data Receiver ) to the MSI. For more details, please refer to here.

For example

  1. Assing role to the MSI

  2. Code

public static void main( String[] args )
    {
        IMessageReceiver receiver = null;
        try {
             receiver= ClientFactory.createMessageReceiverFromEntityPathAsync("bowman1012",
                    "myqueue",
                     new ClientSettings(TokenProvider.createManagedIdentityTokenProvider()),
                     ReceiveMode.PEEKLOCK).get();
            IMessage message = receiver.receiveAsync().get();
            String content = new String(message.getBody(), UTF_8);
            System.out.println(content);

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }finally {
          Boolean f=  receiver.closeAsync().complete(null);
          System.out.println(f);
        }
    }

enter image description here