Class EventGridEvent

java.lang.Object
com.azure.messaging.eventgrid.EventGridEvent

public final class EventGridEvent extends Object
Represents the EventGrid event conforming to the EventGrid event schema.

Depending on your scenario, you can either use the constructor EventGridEvent(String, String, BinaryData, String) to create an EventGridEvent, or use the factory method fromString(String) to deserialize EventGridEvent instances from a Json String representation of EventGrid events.

If you have the data payload of an EventGridEvent and want to send it out, use the constructor EventGridEvent(String, String, BinaryData, String) to create it. Then use EventGridPublisherAsyncClient or EventGridPublisherClient to send it the EventGrid service.

Create EventGridEvent Samples

 // Use BinaryData.fromObject() to create EventGridEvent data
 // From a model class
 User user = new User("Stephen", "James");
 EventGridEvent eventGridEventDataObject = new EventGridEvent("/EventGridEvents/example/source",
     "Example.EventType", BinaryData.fromObject(user), "0.1");

 // From a String
 EventGridEvent eventGridEventDataStr = new EventGridEvent("/EventGridEvents/example/source",
     "Example.EventType", BinaryData.fromObject("Hello World"), "0.1");

 // From an Integer
 EventGridEvent eventGridEventDataInt = new EventGridEvent("/EventGridEvents/example/source",
     "Example.EventType", BinaryData.fromObject(1), "0.1");

 // From a Boolean
 EventGridEvent eventGridEventDataBool = new EventGridEvent("/EventGridEvents/example/source",
     "Example.EventType", BinaryData.fromObject(true), "0.1");

 // From null
 EventGridEvent eventGridEventDataNull = new EventGridEvent("/EventGridEvents/example/source",
     "Example.EventType", BinaryData.fromObject(null), "0.1");

 // Use BinaryData.fromString() if you have a Json String for the EventGridEvent data.
 String jsonStringForData = "\"Hello World\"";  // A json String.
 EventGridEvent eventGridEventDataDataJsonStr = new EventGridEvent("/EventGridEvents/example/source",
     "Example.EventType", BinaryData.fromString(jsonStringForData), "0.1");
 

On the contrary, if you receive events from any event handlers and therefore have the Json string representation of one or more of EventGridEvents, use fromString(String) to deserialize them from the Json string.

Deserialize EventGridEvent Samples

 List<EventGridEvent> eventGridEventList = EventGridEvent.fromString(eventGridEventJsonString);
 EventGridEvent eventGridEvent = eventGridEventList.get(0);
 BinaryData eventGridEventData = eventGridEvent.getData();

 User objectValue = eventGridEventData.toObject(User.class);  // If data payload is a User object.
 int intValue = eventGridEventData.toObject(Integer.class);  // If data payload is an int.
 boolean boolValue = eventGridEventData.toObject(Boolean.class);  // If data payload is boolean.
 String stringValue = eventGridEventData.toObject(String.class);  // If data payload is String.
 String jsonStringValue = eventGridEventData.toString();  // The data payload represented in Json String.
 
See Also:
  • Constructor Details

    • EventGridEvent

      public EventGridEvent(String subject, String eventType, BinaryData data, String dataVersion)
      Create a new instance of the EventGridEvent, with the given required fields.

      Create EventGridEvent Samples

       // Use BinaryData.fromObject() to create EventGridEvent data
       // From a model class
       User user = new User("Stephen", "James");
       EventGridEvent eventGridEventDataObject = new EventGridEvent("/EventGridEvents/example/source",
           "Example.EventType", BinaryData.fromObject(user), "0.1");
      
       // From a String
       EventGridEvent eventGridEventDataStr = new EventGridEvent("/EventGridEvents/example/source",
           "Example.EventType", BinaryData.fromObject("Hello World"), "0.1");
      
       // From an Integer
       EventGridEvent eventGridEventDataInt = new EventGridEvent("/EventGridEvents/example/source",
           "Example.EventType", BinaryData.fromObject(1), "0.1");
      
       // From a Boolean
       EventGridEvent eventGridEventDataBool = new EventGridEvent("/EventGridEvents/example/source",
           "Example.EventType", BinaryData.fromObject(true), "0.1");
      
       // From null
       EventGridEvent eventGridEventDataNull = new EventGridEvent("/EventGridEvents/example/source",
           "Example.EventType", BinaryData.fromObject(null), "0.1");
      
       // Use BinaryData.fromString() if you have a Json String for the EventGridEvent data.
       String jsonStringForData = "\"Hello World\"";  // A json String.
       EventGridEvent eventGridEventDataDataJsonStr = new EventGridEvent("/EventGridEvents/example/source",
           "Example.EventType", BinaryData.fromString(jsonStringForData), "0.1");
       
      Parameters:
      subject - the subject of the event.
      eventType - the type of the event, e.g. "Contoso.Items.ItemReceived".
      data - the data associated with this event. The content of this BinaryData must be a Json value.
      dataVersion - the version of the data sent along with the event.
      Throws:
      NullPointerException - if subject, eventType, data, or dataVersion is null.
      IllegalArgumentException - if the content of data isn't a Json value.
  • Method Details

    • fromString

      public static List<EventGridEvent> fromString(String eventGridJsonString)
      Deserialize EventGridEvent JSON string representation that has one EventGridEvent object or an array of CloudEvent objects into a list of EventGridEvents.

      Deserialize EventGridEvent Samples

       List<EventGridEvent> eventGridEventList = EventGridEvent.fromString(eventGridEventJsonString);
       EventGridEvent eventGridEvent = eventGridEventList.get(0);
       BinaryData eventGridEventData = eventGridEvent.getData();
      
       User objectValue = eventGridEventData.toObject(User.class);  // If data payload is a User object.
       int intValue = eventGridEventData.toObject(Integer.class);  // If data payload is an int.
       boolean boolValue = eventGridEventData.toObject(Boolean.class);  // If data payload is boolean.
       String stringValue = eventGridEventData.toObject(String.class);  // If data payload is String.
       String jsonStringValue = eventGridEventData.toString();  // The data payload represented in Json String.
       
      Parameters:
      eventGridJsonString - the JSON string containing one or more EventGridEvent objects.
      Returns:
      A list of EventGridEvents deserialized from eventGridJsonString.
      Throws:
      IllegalArgumentException - if eventGridJsonString isn't a JSON string for a eventgrid event or an array of it.
      NullPointerException - if eventGridJsonString is null.
      IllegalArgumentException - if the {eventGridJsonString isn't a Json string or can't be deserialized into valid EventGridEvent instances.
    • getId

      public String getId()
      Get the unique id associated with this event.
      Returns:
      the id.
    • setId

      public EventGridEvent setId(String id)
      Set the unique id of the event. Note that a random id has already been set by default.
      Parameters:
      id - the unique id to set.
      Returns:
      the event itself.
      Throws:
      NullPointerException - if id is null.
      IllegalArgumentException - if id is an empty String.
    • getTopic

      public String getTopic()
      Get the topic associated with this event if it is associated with a domain.
      Returns:
      the topic, or null if the topic is not set.
    • setTopic

      public EventGridEvent setTopic(String topic)
      Set the topic associated with this event. Used to route events from domain endpoints.
      Parameters:
      topic - the topic to set.
      Returns:
      the event itself.
    • getSubject

      public String getSubject()
      Get the subject associated with this event.
      Returns:
      the subject.
    • getData

      public BinaryData getData()
      Get the data associated with this event as a BinaryData, which has API to deserialize the data to any objects by using BinaryData.toObject(TypeReference).
      Returns:
      A BinaryData that wraps the this event's data payload.
    • getEventType

      public String getEventType()
      Get the type of this event.
      Returns:
      the event type.
    • getEventTime

      public OffsetDateTime getEventTime()
      Get the time associated with the occurrence of this event.
      Returns:
      the event time.
    • setEventTime

      public EventGridEvent setEventTime(OffsetDateTime time)
      Set the time associated with the event. Note that a default time has already been set when the event was constructed.
      Parameters:
      time - the time to set.
      Returns:
      the event itself.
    • getDataVersion

      public String getDataVersion()
      Get the version of the data in the event. This can be used to specify versioning of event data schemas over time.
      Returns:
      the version of the event data.