Class EventData

java.lang.Object
com.azure.core.models.MessageContent
com.azure.messaging.eventhubs.EventData

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.

Here's how AMQP message sections map to EventData. For reference, the specification can be found here: AMQP 1.0 specification

  1. getProperties() - AMQPMessage.ApplicationProperties section
  2. getBody() - if AMQPMessage.Body has Data section

Serializing a received EventData with AMQP sections other than ApplicationProperties (with primitive Java types) and Data section is not supported.

See Also:
  • Constructor Details

    • EventData

      public EventData()
      Creates an event with an empty body.
    • EventData

      public EventData(byte[] body)
      Creates an event containing the body.
      Parameters:
      body - The data to set for this event.
      Throws:
      NullPointerException - if body is null.
    • EventData

      public EventData(ByteBuffer body)
      Creates an event containing the body.
      Parameters:
      body - The data to set for this event.
      Throws:
      NullPointerException - if body is null.
    • EventData

      public EventData(String body)
      Creates an event by encoding the body using UTF-8 charset.
      Parameters:
      body - The string that will be UTF-8 encoded to create an event.
      Throws:
      NullPointerException - if body is null.
    • EventData

      public EventData(BinaryData body)
      Creates an event with the provided BinaryData as payload.
      Parameters:
      body - The BinaryData payload for this event.
  • Method Details

    • getProperties

      public 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. 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());
       
      Returns:
      Application properties associated with this EventData. For received EventData, the map is a read-only view.
    • getSystemProperties

      public Map<String,Object> getSystemProperties()
      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:
      An encapsulation of all system properties appended by EventHubs service into EventData. If the EventData is not received from the Event Hubs service, the values returned are null.
    • 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:
      A byte array representing the data.
    • getBodyAsString

      public String getBodyAsString()
      Returns event data as UTF-8 decoded string.
      Returns:
      UTF-8 decoded string representation of the event data.
    • getBodyAsBinaryData

      public BinaryData getBodyAsBinaryData()
      Returns the BinaryData payload associated with this event.
      Overrides:
      getBodyAsBinaryData in class MessageContent
      Returns:
      the BinaryData payload associated with this event.
    • 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:
      setBodyAsBinaryData in class MessageContent
      Parameters:
      binaryData - The message body.
      Returns:
      The updated MessageContent object.
    • getOffset

      public Long getOffset()
      Gets the offset of the event when it was received from the associated Event Hub partition. This is only present on a received EventData.
      Returns:
      The offset within the Event Hub partition of the received event. null if the EventData was not received from Event Hubs service.
    • 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:
      A partition key for this Event Data. 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.
    • 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:
      The instant, in UTC, this was enqueued in the Event Hub partition. null if the EventData was not received from Event Hubs service.
    • 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:
      The sequence number for this event. null if the EventData was not received from Event Hubs service.
    • getRawAmqpMessage

      public AmqpAnnotatedMessage getRawAmqpMessage()
      Gets the underlying AMQP message.
      Returns:
      The underlying AMQP message.
    • getContentType

      public String getContentType()
      Gets the content type.
      Overrides:
      getContentType in class MessageContent
      Returns:
      The content type.
    • setContentType

      public EventData setContentType(String contentType)
      Sets the content type.
      Overrides:
      setContentType in class MessageContent
      Parameters:
      contentType - The content type.
      Returns:
      The updated EventData.
    • getCorrelationId

      public String getCorrelationId()
      Gets the correlation id.
      Returns:
      The correlation id. null if there is none set.
    • setCorrelationId

      public EventData setCorrelationId(String correlationId)
      Sets the correlation id.
      Parameters:
      correlationId - The correlation id.
      Returns:
      The updated EventData.
    • getMessageId

      public String getMessageId()
      Gets the message id.
      Returns:
      The message id. null if there is none set.
    • setMessageId

      public EventData setMessageId(String messageId)
      Sets the message id.
      Parameters:
      messageId - The message id.
      Returns:
      The updated EventData.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • addContext

      public EventData addContext(String key, Object value)
      Adds a new key value pair to the existing context on Event Data.
      Parameters:
      key - The key for this context object
      value - The value for this context object.
      Returns:
      The updated EventData.
      Throws:
      NullPointerException - if key or value is null.