EventData Class
- java.
lang. Object - com.
azure. core. models. MessageContent - com.
azure. messaging. eventhubs. EventData
- com.
- com.
public class EventData
extends MessageContent
The data structure encapsulating the event being sent-to and received-from Event Hubs. Each Event Hub partition can be visualized as a stream of EventData. This class is not thread-safe.
Constructor Summary
Constructor | Description |
---|---|
EventData() |
Creates an event with an empty body. |
EventData(byte[] body) |
Creates an event containing the |
EventData(BinaryData body) |
Creates an event with the provided BinaryData as payload. |
EventData(String body) |
Creates an event by encoding the |
EventData(ByteBuffer body) |
Creates an event containing the |
Method Summary
Modifier and Type | Method and Description |
---|---|
Event |
addContext(String key, Object value)
Adds a new key value pair to the existing context on Event Data. |
boolean |
equals(Object o)
True if the object is an EventData and the binary contents of getBody() are equal. |
byte[] |
getBody()
Gets the actual payload/data wrapped by Event |
Binary |
getBodyAsBinaryData()
Returns the BinaryData payload associated with this event. |
String |
getBodyAsString()
Returns event data as UTF-8 decoded string. |
String |
getContentType()
Gets the MIME type describing the data contained in the getBody(), intended to allow consumers to make informed decisions for inspecting and processing the event. |
String |
getCorrelationId()
Gets an application-defined value that represents the context to use for correlation across one or more operations. |
Instant |
getEnqueuedTime()
Gets the instant, in UTC, of when the event was enqueued in the Event Hub partition. |
String |
getMessageId()
Gets an application-defined value that uniquely identifies the event. |
Long |
getOffset()
Gets the offset of the event when it was received from the associated Event Hub partition. |
String |
getPartitionKey()
Gets the partition hashing key if it was set when originally publishing the event. |
Map<String,Object> |
getProperties()
Gets the set of free-form event properties which may be used for passing metadata associated with the event with the event body during Event Hubs operations. |
Amqp |
getRawAmqpMessage()
Gets the underlying AMQP message. |
Long |
getSequenceNumber()
Gets the sequence number assigned to the event when it was enqueued in the associated Event Hub partition. |
Map<String,Object> |
getSystemProperties()
Properties that are populated by Event Hubs service. |
int |
hashCode()
Gets a hash of the binary contents in getBody(). |
Event |
setBodyAsBinaryData(BinaryData binaryData)
Sets a new binary body and corresponding AmqpAnnotatedMessage on the event. |
Event |
setContentType(String contentType)
Sets the MIME type describing the data contained in the getBody(), intended to allow consumers to make informed decisions for inspecting and processing the event. |
Event |
setCorrelationId(String correlationId)
Sets an application-defined value that represents the context to use for correlation across one or more operations. |
Event |
setMessageId(String messageId)
Sets an application-defined value that uniquely identifies the event. |
Methods inherited from MessageContent
Methods inherited from java.lang.Object
Constructor Details
EventData
public EventData()
Creates an event with an empty body.
EventData
public EventData(byte[] body)
Creates an event containing the body
.
Parameters:
EventData
public EventData(BinaryData body)
Creates an event with the provided BinaryData as payload.
Parameters:
EventData
public EventData(String body)
Creates an event by encoding the body
using UTF-8 charset.
Parameters:
EventData
public EventData(ByteBuffer body)
Creates an event containing the body
.
Parameters:
Method Details
addContext
public EventData addContext(String key, Object value)
Adds a new key value pair to the existing context on Event Data.
Parameters:
Returns:
equals
getBody
public byte[] getBody()
Gets the actual payload/data wrapped by EventData.
If the means for deserializing the raw data is not apparent to consumers, a common technique is to make use of getProperties() when creating the event, to associate serialization hints as an aid to consumers who wish to deserialize the binary data.
Returns:
getBodyAsBinaryData
public BinaryData getBodyAsBinaryData()
Returns the BinaryData payload associated with this event.
Overrides:
EventData.getBodyAsBinaryData()Returns:
getBodyAsString
public String getBodyAsString()
Returns event data as UTF-8 decoded string.
Returns:
getContentType
public String getContentType()
Gets the MIME type describing the data contained in the getBody(), intended to allow consumers to make informed decisions for inspecting and processing the event.
Overrides:
EventData.getContentType()Returns:
getCorrelationId
public String getCorrelationId()
Gets an application-defined value that represents the context to use for correlation across one or more operations. The identifier is a free-form value and may reflect a unique identity or a shared data element with significance to the application.
Returns:
null
if there is none set.getEnqueuedTime
public Instant getEnqueuedTime()
Gets the instant, in UTC, of when the event was enqueued in the Event Hub partition. This is only present on a received EventData.
Returns:
null
if the EventData
was not received from Event Hubs service.getMessageId
public String getMessageId()
Gets an application-defined value that uniquely identifies the event. The identifier is a free-form value and can reflect a GUID or an identifier derived from the application context.
Returns:
null
if there is none set.getOffset
getPartitionKey
public String getPartitionKey()
Gets the partition hashing key if it was set when originally publishing the event. If it exists, this value was used to compute a hash to select a partition to send the message to. This is only present on a received EventData.
Returns:
null
if the EventData was not received from Event
Hubs service or there was no partition key set when the event was sent to the Event Hub.getProperties
public Map
Gets the set of free-form event properties which may be used for passing metadata associated with the event with the event body during Event Hubs operations. A common use-case for properties()
is to associate serialization hints for the getBody() as an aid to consumers who wish to deserialize the binary data.
Adding serialization hint using getProperties()
In the sample, the type of telemetry is indicated by adding an application property with key "eventType".
TelemetryEvent telemetry = new TelemetryEvent("temperature", "37");
byte[] serializedTelemetryData = telemetry.toString().getBytes(UTF_8);
EventData eventData = new EventData(serializedTelemetryData);
eventData.getProperties().put("eventType", TelemetryEvent.class.getName());
The following types are supported:
- Character
- Date
- Double
- Float
- Integer
- Long
- Short
- String
Returns:
getRawAmqpMessage
public AmqpAnnotatedMessage getRawAmqpMessage()
Gets the underlying AMQP message.
Returns:
getSequenceNumber
public Long getSequenceNumber()
Gets the sequence number assigned to the event when it was enqueued in the associated Event Hub partition. This is unique for every message received in the Event Hub partition. This is only present on a received EventData.
Returns:
null
if the EventData was not received from Event
Hubs service.getSystemProperties
public Map
Properties that are populated by Event Hubs service. As these are populated by the Event Hubs service, they are only present on a received EventData. Provides an abstraction on top of properties exposed by getRawAmqpMessage(). These properties are read-only and can be modified via getRawAmqpMessage().
Returns:
hashCode
public int hashCode()
Gets a hash of the binary contents in getBody().
Overrides:
EventData.hashCode()setBodyAsBinaryData
public EventData setBodyAsBinaryData(BinaryData binaryData)
Sets a new binary body and corresponding AmqpAnnotatedMessage on the event. Contents from getRawAmqpMessage() are shallow copied to the new underlying message.
Overrides:
EventData.setBodyAsBinaryData(BinaryData binaryData)Parameters:
setContentType
public EventData setContentType(String contentType)
Sets the MIME type describing the data contained in the getBody(), intended to allow consumers to make informed decisions for inspecting and processing the event.
Overrides:
EventData.setContentType(String contentType)Parameters:
Returns:
setCorrelationId
public EventData setCorrelationId(String correlationId)
Sets an application-defined value that represents the context to use for correlation across one or more operations. The identifier is a free-form value and may reflect a unique identity or a shared data element with significance to the application.
Parameters:
Returns:
setMessageId
public EventData setMessageId(String messageId)
Sets an application-defined value that uniquely identifies the event. The identifier is a free-form value and can reflect a GUID or an identifier derived from the application context.
Parameters:
Returns:
Applies to
Azure SDK for Java