EventGridEvent Class
- java.
lang. Object - com.
azure. messaging. eventgrid. EventGridEvent
- com.
Implements
public final class EventGridEvent
implements JsonSerializable<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 Event |
Method Summary
| Modifier and Type | Method and Description |
|---|---|
|
static
Event |
fromJson(JsonReader jsonReader)
Reads a JSON stream into an EventGridEvent. |
|
static
List<Event |
fromString(String eventGridJsonString)
Deserialize EventGridEvent JSON string representation that has one Event |
|
Binary |
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. |
|
Offset |
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. |
|
Event |
setEventTime(OffsetDateTime time)
Set the time associated with the event. |
|
Event |
setId(String id)
Set the unique id of the event. |
|
Event |
setTopic(String topic)
Set the topic associated with this event. |
|
Json |
toJson(JsonWriter jsonWriter) |
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:
Method Details
fromJson
public static EventGridEvent fromJson(JsonReader jsonReader)
Reads a JSON stream into an EventGridEvent.
Parameters:
Returns:
Throws:
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:
Returns:
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:
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:
getEventTime
public OffsetDateTime getEventTime()
Get the time associated with the occurrence of this event.
Returns:
getEventType
public String getEventType()
Get the type of this event.
Returns:
getId
public String getId()
Get the unique id associated with this event.
Returns:
getSubject
public String getSubject()
Get the subject associated with this event.
Returns:
getTopic
public String getTopic()
Get the topic associated with this event if it is associated with a domain.
Returns:
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:
Returns:
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:
Returns:
setTopic
public EventGridEvent setTopic(String topic)
Set the topic associated with this event. Used to route events from domain endpoints.
Parameters:
Returns: