Class EventGridPublisherAsyncClient<T>

java.lang.Object
com.azure.messaging.eventgrid.EventGridPublisherAsyncClient<T>

public final class EventGridPublisherAsyncClient<T> extends Object
A service client that publishes events to an EventGrid topic or domain asynchronously. Use EventGridPublisherClientBuilder to create an instance of this client.

Create EventGridPublisherAsyncClient for CloudEvent Samples

 // Create a client to send events of CloudEvent schema (com.azure.core.models.CloudEvent)
 EventGridPublisherAsyncClient<CloudEvent> cloudEventPublisherClient = new EventGridPublisherClientBuilder()
     .endpoint(System.getenv("AZURE_EVENTGRID_CLOUDEVENT_ENDPOINT"))  // make sure it accepts CloudEvent
     .credential(new AzureKeyCredential(System.getenv("AZURE_EVENTGRID_CLOUDEVENT_KEY")))
     .buildCloudEventPublisherAsyncClient();
 

Send CloudEvent Samples

 // Create a com.azure.models.CloudEvent.
 User user = new User("Stephen", "James");
 CloudEvent cloudEventDataObject = new CloudEvent("/cloudevents/example/source", "Example.EventType",
     BinaryData.fromObject(user), CloudEventDataFormat.JSON, "application/json");

 // Send a single CloudEvent
 cloudEventPublisherClient.sendEvent(cloudEventDataObject).block();

 // Send a list of CloudEvents to the EventGrid service altogether.
 // This has better performance than sending one by one.
 cloudEventPublisherClient.sendEvents(Arrays.asList(
     cloudEventDataObject
     // add more CloudEvents objects
 )).block();
 

Create EventGridPublisherAsyncClient for EventGridEvent Samples

 // Create a client to send events of EventGridEvent schema
 EventGridPublisherAsyncClient<EventGridEvent> eventGridEventPublisherClient = new EventGridPublisherClientBuilder()
     .endpoint(System.getenv("AZURE_EVENTGRID_EVENT_ENDPOINT"))  // make sure it accepts EventGridEvent
     .credential(new AzureKeyCredential(System.getenv("AZURE_EVENTGRID_EVENT_KEY")))
     .buildEventGridEventPublisherAsyncClient();
 

Send EventGridEvent Samples

 // Create an EventGridEvent
 User user = new User("John", "James");
 EventGridEvent eventGridEvent = new EventGridEvent("/EventGridEvents/example/source",
     "Example.EventType", BinaryData.fromObject(user), "0.1");

 // Send a single EventGridEvent
 eventGridEventPublisherClient.sendEvent(eventGridEvent).block();

 // Send a list of EventGridEvents to the EventGrid service altogether.
 // This has better performance than sending one by one.
 eventGridEventPublisherClient.sendEvents(Arrays.asList(
     eventGridEvent
     // add more EventGridEvents objects
 )).block();
 

Create EventGridPublisherAsyncClient for Custom Event Schema Samples

 // Create a client to send events of custom event
 EventGridPublisherAsyncClient<BinaryData> customEventPublisherClient = new EventGridPublisherClientBuilder()
     .endpoint(System.getenv("AZURE_CUSTOM_EVENT_ENDPOINT"))  // make sure it accepts custom events
     .credential(new AzureKeyCredential(System.getenv("AZURE_CUSTOM_EVENT_KEY")))
     .buildCustomEventPublisherAsyncClient();
 

Send Custom Event Schema Samples

 // Create an custom event object (both POJO and Map work)
 Map<String, Object> customEvent = new HashMap<String, Object>() {
     {
         put("id", UUID.randomUUID().toString());
         put("subject", "Test");
         put("foo", "bar");
         put("type", "Microsoft.MockPublisher.TestEvent");
         put("data", 100.0);
         put("dataVersion", "0.1");
     }
 };

 // Send a single custom event
 customEventPublisherClient.sendEvent(BinaryData.fromObject(customEvent)).block();

 // Send a list of EventGridEvents to the EventGrid service altogether.
 // This has better performance than sending one by one.
 customEventPublisherClient.sendEvents(Arrays.asList(
     BinaryData.fromObject(customEvent)
     // add more custom events in BinaryData
 )).block();
 
See Also:
  • Method Details

    • generateSas

      public static String generateSas(String endpoint, AzureKeyCredential keyCredential, OffsetDateTime expirationTime)
      Generate a shared access signature to provide time-limited authentication for requests to the Event Grid service with the latest Event Grid service API defined in EventGridServiceVersion.getLatest().
      Parameters:
      endpoint - the endpoint of the Event Grid topic or domain.
      expirationTime - the time in which the signature should expire, no longer providing authentication.
      keyCredential - the access key obtained from the Event Grid topic or domain.
      Returns:
      the shared access signature string which can be used to construct an instance of AzureSasCredential.
      Throws:
      NullPointerException - if endpoint, keyCredential or expirationTime is null.
      RuntimeException - if java security doesn't have algorithm "hmacSHA256".
    • generateSas

      public static String generateSas(String endpoint, AzureKeyCredential keyCredential, OffsetDateTime expirationTime, EventGridServiceVersion apiVersion)
      Generate a shared access signature to provide time-limited authentication for requests to the Event Grid service.
      Parameters:
      endpoint - the endpoint of the Event Grid topic or domain.
      expirationTime - the time in which the signature should expire, no longer providing authentication.
      keyCredential - the access key obtained from the Event Grid topic or domain.
      apiVersion - the EventGrid service api version defined in EventGridServiceVersion
      Returns:
      the shared access signature string which can be used to construct an instance of AzureSasCredential.
      Throws:
      NullPointerException - if endpoint, keyCredential or expirationTime is null.
      RuntimeException - if java security doesn't have algorithm "hmacSHA256".
    • sendEvents

      public Mono<Void> sendEvents(Iterable<T> events)
      Publishes the given events to the set topic or domain.
      Parameters:
      events - the events to publish.
      Returns:
      A Mono that completes when the events are sent to the service.
      Throws:
      NullPointerException - if events is null.
    • sendEventsWithResponse

      public Mono<Response<Void>> sendEventsWithResponse(Iterable<T> events)
      Publishes the given events to the set topic or domain and gives the response issued by EventGrid.
      Parameters:
      events - the events to publish.
      Returns:
      the response from the EventGrid service.
      Throws:
      NullPointerException - if events is null.
    • sendEventsWithResponse

      public Mono<Response<Void>> sendEventsWithResponse(Iterable<T> events, String channelName)
      Publishes the given events to the set topic or domain and gives the response issued by EventGrid.
      Parameters:
      events - the events to publish.
      channelName - the channel name to send to Event Grid service. This is only applicable for sending Cloud Events to a partner topic in partner namespace. For more details, refer to Partner Events Overview.
      Returns:
      the response from the EventGrid service.
      Throws:
      NullPointerException - if events is null.
    • sendEvent

      public Mono<Void> sendEvent(T event)
      Publishes the given events to the set topic or domain.
      Parameters:
      event - the event to publish.
      Returns:
      A Mono that completes when the event is sent to the service.
      Throws:
      NullPointerException - if events is null.