EventGridEvent Class

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

public final class EventGridEvent

Represents the EventGrid event conforming to the EventGrid event schema.

Depending on your scenario, you can either use the constructor EventGridEvent(String subject, String eventType, BinaryData data, String dataVersion) to create an EventGridEvent, or use the factory method fromString(String eventGridJsonString) 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 subject, String eventType, BinaryData data, String dataVersion) to create it. Then use EventGridPublisherAsyncClient<T> or EventGridPublisherClient<T> 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 eventGridJsonString) 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.

Constructor Summary

Constructor Description
EventGridEvent(String subject, String eventType, BinaryData data, String dataVersion)

Create a new instance of the EventGridEvent, with the given required fields.

Method Summary

Modifier and Type Method and Description
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.

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).

String getDataVersion()

Get the version of the data in the event.

OffsetDateTime getEventTime()

Get the time associated with the occurrence of this event.

String getEventType()

Get the type of this event.

String getId()

Get the unique id associated with this event.

String getSubject()

Get the subject associated with this event.

String getTopic()

Get the topic associated with this event if it is associated with a domain.

EventGridEvent setEventTime(OffsetDateTime time)

Set the time associated with the event.

EventGridEvent setId(String id)

Set the unique id of the event.

EventGridEvent setTopic(String topic)

Set the topic associated with this event.

Methods inherited from java.lang.Object

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.

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 EventGridEvent deserialized from eventGridJsonString.

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.

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.

getEventTime

public OffsetDateTime getEventTime()

Get the time associated with the occurrence of this event.

Returns:

the event time.

getEventType

public String getEventType()

Get the type of this event.

Returns:

the event type.

getId

public String getId()

Get the unique id associated with this event.

Returns:

the id.

getSubject

public String getSubject()

Get the subject associated with this event.

Returns:

the subject.

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.

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.

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.

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.

Applies to