azure-eventgridazure-java-sdk

Domain Topic and Event Subscription using java sdk


I have seen many examples for creating a domain topic and event subscription using rest API's but I want to create it using the azure java sdk which is already there. any leads would greatly help. thanks


Solution

  • Prerequisites to create Domain Topic and Event Subscription using java SDK

    #create Topic
    az eventgrid topic create --location <location> --resource-group <your-resource-group-name> --name <your-resource-name>
    
    #create Domain
    az eventgrid domain create --location <location> --resource-group <your-resource-group-name> --name <your-resource-name>
    

    Include the Required Packages

    Add BOM file:

    Include the azure-sdk-bom to your project to take a dependency on GA version of the Library.

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-sdk-bom</artifactId>
                <version>{bom_version_to_target}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    And add the direct dependency in the dependencies section without the version flag

    <dependencies>
      <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-messaging-eventgrid</artifactId>
      </dependency>
    </dependencies>
    

    Authenticate the Client

    The authentication can be either a key credential, a shared access signature, or Azure Active Directory token authentication. Refer here

    Create the client

    Using endpoint and Access key to create the client

    // For custom event
    EventGridPublisherAsyncClient<BinaryData> customEventAsyncClient = new EventGridPublisherClientBuilder()
        .endpoint("<endpoint of your event grid topic/domain that accepts custom event schema>")
        .credential(new AzureKeyCredential("<key for the endpoint>"))
        .buildCustomEventPublisherAsyncClient();
    

    Using endpoint and SAS token to create the client

    EventGridPublisherAsyncClient<CloudEvent> cloudEventAsyncClient = new EventGridPublisherClientBuilder()
        .endpoint("<endpoint of your event grid topic/domain that accepts CloudEvent schema>")
        .credential(new AzureSasCredential("<sas token that can access the endpoint>"))
        .buildCloudEventPublisherAsyncClient();
    

    Refer here for more information