How to create a WebPubSubServiceClient with Java and Azure Identity

This how-to guide shows you how to create a WebPubSubServiceClient using Microsoft Entra ID in Java.

Requirements

  • Add azure-identity dependency in your pom.xml.

    <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-identity</artifactId>
      <version>1.4.1</version>
    </dependency>
    

    Tip

    Latest version can be found on this page

    See Azure authentication with Java and Azure Identity to learn more.

  • Add azure-messaging-webpubsub dependency in your pom.xml.

    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-messaging-webpubsub</artifactId>
        <version>1.0.0</version>
    </dependency>
    

    Tip

    Latest version can be found on this page

Sample codes

  1. Create a TokenCredential with Azure Identity SDK.

    package com.webpubsub.tutorial;
    
    import com.azure.core.credential.TokenCredential;
    import com.azure.identity.DefaultAzureCredentialBuilder;
    
    public class App {
    
        public static void main(String[] args) {
            TokenCredential credential = new DefaultAzureCredentialBuilder().build();
        }
    }
    

    credential can be any class that inherits from TokenCredential class.

    • EnvironmentCredential
    • ClientSecretCredential
    • ClientCertificateCredential
    • ManagedIdentityCredential
    • VisualStudioCredential
    • VisualStudioCodeCredential
    • AzureCliCredential

    To learn more, see Azure Identity client library for Java

  2. Then create a client with endpoint, hub, and credential.

    package com.webpubsub.tutorial;
    
    import com.azure.core.credential.TokenCredential;
    import com.azure.identity.DefaultAzureCredentialBuilder;
    import com.azure.messaging.webpubsub.WebPubSubServiceClient;
    import com.azure.messaging.webpubsub.WebPubSubServiceClientBuilder;
    
    public class App {
        public static void main(String[] args) {
    
            TokenCredential credential = new DefaultAzureCredentialBuilder().build();
    
            // create the service client
            WebPubSubServiceClient client = new WebPubSubServiceClientBuilder()
                    .endpoint("<endpoint>")
                    .credential(credential)
                    .hub("<hub>")
                    .buildClient();
        }
    }
    

    Learn how to use this client, see Azure Web PubSub service client library for Java