CloudEvent Class
- java.
lang. Object - com.
azure. core. models. CloudEvent
- com.
Implements
public final class CloudEvent
implements JsonSerializable<CloudEvent>
Represents the CloudEvent conforming to the 1.0 schema defined by the Cloud Native Computing Foundation.
CloudEvents is a specification for describing event data in common formats to provide interoperability across services, platforms and systems.
Some Azure services, for instance, EventGrid, are compatible with this specification. You can use this class to communicate with these Azure services.
Depending on your scenario, you can either use the constructor CloudEvent(String source, String type, BinaryData data, CloudEventDataFormat format, String dataContentType) to create a CloudEvent, or use the factory method fromString(String cloudEventsJson) to deserialize CloudEvent instances from a Json String representation of CloudEvents.
If you have the data payload of a CloudEvent and want to send it out, use the constructor CloudEvent(String source, String type, BinaryData data, CloudEventDataFormat format, String dataContentType) to create it. Then you can serialize the CloudEvent into its Json String representation and send it.
Create CloudEvent Samples
// Use BinaryData.fromBytes() to create data in format CloudEventDataFormat.BYTES
byte[] exampleBytes = "Hello World".getBytes(StandardCharsets.UTF_8);
CloudEvent cloudEvent = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromBytes(exampleBytes), CloudEventDataFormat.BYTES, "application/octet-stream");
// Use BinaryData.fromObject() to create CloudEvent data in format CloudEventDataFormat.JSON
// From a model class
User user = new User("Stephen", "James");
CloudEvent cloudEventDataObject = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromObject(user), CloudEventDataFormat.JSON, "application/json");
// From a String
CloudEvent cloudEventDataStr = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromObject("Hello World"), CloudEventDataFormat.JSON, "text/plain");
// From an Integer
CloudEvent cloudEventDataInt = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromObject(1), CloudEventDataFormat.JSON, "int");
// From a Boolean
CloudEvent cloudEventDataBool = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromObject(true), CloudEventDataFormat.JSON, "bool");
// From null
CloudEvent cloudEventDataNull = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromObject(null), CloudEventDataFormat.JSON, "null");
// Use BinaryData.fromString() if you have a Json String for the CloudEvent data.
String jsonStringForData = "\"Hello World\""; // A json String.
CloudEvent cloudEventDataJsonStr = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromString(jsonStringForData), CloudEventDataFormat.JSON, "text/plain");
On the contrary, if you receive CloudEvents and have the Json string representation of one or more of CloudEvents, use fromString(String cloudEventsJson) to deserialize them from the Json string.
Deserialize CloudEvent Samples
List<CloudEvent> cloudEventList = CloudEvent.fromString(cloudEventJsonString);
CloudEvent cloudEvent = cloudEventList.get(0);
BinaryData cloudEventData = cloudEvent.getData();
byte[] bytesValue = cloudEventData.toBytes(); // If data payload is in bytes (data_base64 is not null).
User objectValue = cloudEventData.toObject(User.class); // If data payload is a User object.
int intValue = cloudEventData.toObject(Integer.class); // If data payload is an int.
boolean boolValue = cloudEventData.toObject(Boolean.class); // If data payload is boolean.
String stringValue = cloudEventData.toObject(String.class); // If data payload is String.
String jsonStringValue = cloudEventData.toString(); // The data payload represented in Json String.
Constructor Summary
| Constructor | Description |
|---|---|
| CloudEvent(String source, String type, BinaryData data, CloudEventDataFormat format, String dataContentType) |
Create an instance of CloudEvent. |
Method Summary
| Modifier and Type | Method and Description |
|---|---|
|
Cloud |
addExtensionAttribute(String name, Object value)
Add/Overwrite a single extension attribute to the cloud event. |
|
static
Cloud |
fromJson(JsonReader jsonReader)
Reads a JSON stream into a CloudEvent. |
|
static
List<Cloud |
fromString(String cloudEventsJson)
Deserialize CloudEvent JSON string representation that has one Cloud |
|
static
List<Cloud |
fromString(String cloudEventsJson, boolean skipValidation)
Deserialize CloudEvent JSON string representation that has one Cloud |
|
Binary |
getData()
Get the data associated with this event as a BinaryData, which has API to deserialize the data into a String, an Object, or a byte[]. |
| String |
getDataContentType()
Get the content MIME type that the data is in. |
| String |
getDataSchema()
Get the schema that the data adheres to. |
| Map<String,Object> |
getExtensionAttributes()
Get a map of the additional user-defined attributes associated with this event. |
| String |
getId()
Get the id of the cloud event. |
| String |
getSource()
Get the source of the event. |
| String |
getSubject()
Get the subject associated with this event. |
|
Offset |
getTime()
Get the time associated with the occurrence of the event. |
| String |
getType()
Get the type of event, e. |
|
Cloud |
setDataSchema(String dataSchema)
Set the schema that the data adheres to. |
|
Cloud |
setId(String id)
Set a custom id. |
|
Cloud |
setSubject(String subject)
Set the subject of the event. |
|
Cloud |
setTime(OffsetDateTime time)
Set the time associated with the occurrence of the event. |
|
Json |
toJson(JsonWriter jsonWriter) |
Methods inherited from java.lang.Object
Constructor Details
CloudEvent
public CloudEvent(String source, String type, BinaryData data, CloudEventDataFormat format, String dataContentType)
Create an instance of CloudEvent.
source, type, id, and specversion are required attributes according to the CNCF CloudEvent spec. You must set the source and type when using this constructor. For convenience, id and specversion are automatically assigned. You can change the id by using setId(String id) after you create a CloudEvent. But you can not change specversion because this class is specifically for CloudEvent 1.0 schema.
For the CloudEvent data payload, this constructor accepts data of BinaryData as the CloudEvent payload. The data can be created from objects of type String, bytes, boolean, null, array or other types. A CloudEvent will be serialized to its Json String representation to be sent out. Use param format to indicate whether the data will be serialized as bytes, or Json. When BYTES is used, the data payload will be serialized to base64 bytes and stored in attribute data_base64 of the CloudEvent's Json representation. When JSON is used, the data payload will be serialized as Json data and stored in attribute data of the CloudEvent's Json representation.
Create CloudEvent Samples
// Use BinaryData.fromBytes() to create data in format CloudEventDataFormat.BYTES
byte[] exampleBytes = "Hello World".getBytes(StandardCharsets.UTF_8);
CloudEvent cloudEvent = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromBytes(exampleBytes), CloudEventDataFormat.BYTES, "application/octet-stream");
// Use BinaryData.fromObject() to create CloudEvent data in format CloudEventDataFormat.JSON
// From a model class
User user = new User("Stephen", "James");
CloudEvent cloudEventDataObject = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromObject(user), CloudEventDataFormat.JSON, "application/json");
// From a String
CloudEvent cloudEventDataStr = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromObject("Hello World"), CloudEventDataFormat.JSON, "text/plain");
// From an Integer
CloudEvent cloudEventDataInt = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromObject(1), CloudEventDataFormat.JSON, "int");
// From a Boolean
CloudEvent cloudEventDataBool = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromObject(true), CloudEventDataFormat.JSON, "bool");
// From null
CloudEvent cloudEventDataNull = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromObject(null), CloudEventDataFormat.JSON, "null");
// Use BinaryData.fromString() if you have a Json String for the CloudEvent data.
String jsonStringForData = "\"Hello World\""; // A json String.
CloudEvent cloudEventDataJsonStr = new CloudEvent("/cloudevents/example/source", "Example.EventType",
BinaryData.fromString(jsonStringForData), CloudEventDataFormat.JSON, "text/plain");
Parameters:
Method Details
addExtensionAttribute
public CloudEvent addExtensionAttribute(String name, Object value)
Add/Overwrite a single extension attribute to the cloud event.
Parameters:
Returns:
fromJson
public static CloudEvent fromJson(JsonReader jsonReader)
Reads a JSON stream into a CloudEvent.
Parameters:
Returns:
Throws:
fromString
public static List<CloudEvent> fromString(String cloudEventsJson)
Deserialize CloudEvent JSON string representation that has one CloudEvent object or an array of CloudEvent objects into a list of CloudEvents, and validate whether any CloudEvents have null id, source, or type. If you want to skip this validation, use fromString(String cloudEventsJson, boolean skipValidation).
Deserialize CloudEvent Samples
List<CloudEvent> cloudEventList = CloudEvent.fromString(cloudEventJsonString);
CloudEvent cloudEvent = cloudEventList.get(0);
BinaryData cloudEventData = cloudEvent.getData();
byte[] bytesValue = cloudEventData.toBytes(); // If data payload is in bytes (data_base64 is not null).
User objectValue = cloudEventData.toObject(User.class); // If data payload is a User object.
int intValue = cloudEventData.toObject(Integer.class); // If data payload is an int.
boolean boolValue = cloudEventData.toObject(Boolean.class); // If data payload is boolean.
String stringValue = cloudEventData.toObject(String.class); // If data payload is String.
String jsonStringValue = cloudEventData.toString(); // The data payload represented in Json String.
Parameters:
Returns:
fromString
public static List<CloudEvent> fromString(String cloudEventsJson, boolean skipValidation)
Deserialize CloudEvent JSON string representation that has one CloudEvent object or an array of CloudEvent objects into a list of CloudEvents.
Parameters:
Returns:
getData
public BinaryData getData()
Get the data associated with this event as a BinaryData, which has API to deserialize the data into a String, an Object, or a byte[].
Returns:
getDataContentType
public String getDataContentType()
Get the content MIME type that the data is in.
Returns:
getDataSchema
public String getDataSchema()
Get the schema that the data adheres to.
Returns:
getExtensionAttributes
public Map<String,Object> getExtensionAttributes()
Get a map of the additional user-defined attributes associated with this event.
Returns:
getId
public String getId()
Get the id of the cloud event.
Returns:
getSource
public String getSource()
Get the source of the event.
Returns:
getSubject
public String getSubject()
Get the subject associated with this event.
Returns:
getTime
public OffsetDateTime getTime()
Get the time associated with the occurrence of the event.
Returns:
getType
public String getType()
Get the type of event, e.g. "Contoso.Items.ItemReceived".
Returns:
setDataSchema
public CloudEvent setDataSchema(String dataSchema)
Set the schema that the data adheres to.
Parameters:
Returns:
setId
public CloudEvent setId(String id)
Set a custom id. Note that a random id is already set by default.
Parameters:
Returns:
setSubject
public CloudEvent setSubject(String subject)
Set the subject of the event.
Parameters:
Returns:
setTime
public CloudEvent setTime(OffsetDateTime time)
Set the time associated with the occurrence of the event.
At creation, the time is set to the current UTC time. It can be unset by setting it to null.
Parameters:
Returns: