Class EventGridEvent
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.
-
Constructor Summary
ConstructorDescriptionEventGridEvent
(String subject, String eventType, BinaryData data, String dataVersion) Create a new instance of the EventGridEvent, with the given required fields. -
Method Summary
Modifier and TypeMethodDescriptionstatic List<EventGridEvent>
fromString
(String eventGridJsonString) DeserializeEventGridEvent
JSON string representation that has one EventGridEvent object or an array of CloudEvent objects into a list of EventGridEvents.getData()
Get the data associated with this event as aBinaryData
, which has API to deserialize the data to any objects by usingBinaryData.toObject(TypeReference)
.Get the version of the data in the event.Get the time associated with the occurrence of this event.Get the type of this event.getId()
Get the unique id associated with this event.Get the subject associated with this event.getTopic()
Get the topic associated with this event if it is associated with a domain.setEventTime
(OffsetDateTime time) Set the time associated with the event.Set the unique id of the event.Set the topic associated with this event.
-
Constructor Details
-
EventGridEvent
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 thisBinaryData
must be a Json value.dataVersion
- the version of the data sent along with the event.- Throws:
NullPointerException
- if subject, eventType, data, or dataVersion isnull
.IllegalArgumentException
- if the content of data isn't a Json value.
-
-
Method Details
-
fromString
DeserializeEventGridEvent
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 isnull
.IllegalArgumentException
- if the {eventGridJsonString isn't a Json string or can't be deserialized into valid EventGridEvent instances.
-
getId
Get the unique id associated with this event.- Returns:
- the id.
-
setId
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
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
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
Get the subject associated with this event.- Returns:
- the subject.
-
getData
Get the data associated with this event as aBinaryData
, which has API to deserialize the data to any objects by usingBinaryData.toObject(TypeReference)
.- Returns:
- A
BinaryData
that wraps the this event's data payload.
-
getEventType
Get the type of this event.- Returns:
- the event type.
-
getEventTime
Get the time associated with the occurrence of this event.- Returns:
- the event time.
-
setEventTime
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
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.
-