Class ServiceBusMessage

java.lang.Object
com.azure.messaging.servicebus.ServiceBusMessage

public class ServiceBusMessage extends Object
The data structure encapsulating the message being sent to Service Bus. The message structure is discussed in detail in the product documentation.
See Also:
  • Constructor Details

    • ServiceBusMessage

      public ServiceBusMessage(byte[] body)
      Creates a ServiceBusMessage with given byte array body.
      Parameters:
      body - The content of the Service bus message.
      Throws:
      NullPointerException - if body is null.
    • ServiceBusMessage

      public ServiceBusMessage(String body)
      Creates a ServiceBusMessage with a UTF-8 encoded body.
      Parameters:
      body - The content of the Service Bus message.
      Throws:
      NullPointerException - if body is null.
    • ServiceBusMessage

      public ServiceBusMessage(com.azure.core.util.BinaryData body)
      Creates a ServiceBusMessage containing the body.The BinaryData provides various convenience API representing byte array. It also provides a way to serialize Object into BinaryData.
      Parameters:
      body - The data to set for this ServiceBusMessage.
      Throws:
      NullPointerException - if body is null.
      See Also:
      • BinaryData
    • ServiceBusMessage

      public ServiceBusMessage(com.azure.core.amqp.models.AmqpMessageBody amqpMessageBody)
      This constructor provides an easy way to create ServiceBusMessage with message body as AMQP Data types SEQUENCE and VALUE. In case of SEQUENCE, tt support sending and receiving of only one AMQP Sequence at present. If you are sending message with single byte array or String data, you can also use other constructor.
      Parameters:
      amqpMessageBody - amqp message body.
      Throws:
      NullPointerException - if amqpMessageBody is null.
    • ServiceBusMessage

      public ServiceBusMessage(ServiceBusReceivedMessage receivedMessage)
      Creates a ServiceBusMessage using properties from receivedMessage. This is normally used when a ServiceBusReceivedMessage needs to be sent to another entity.
      Parameters:
      receivedMessage - The received message to create new message from.
      Throws:
      NullPointerException - if receivedMessage is null.
      IllegalStateException - for invalid AmqpMessageBodyType.
  • Method Details

    • getApplicationProperties

      public Map<String,Object> getApplicationProperties()
      Gets the set of free-form ServiceBusMessage properties which may be used for passing metadata associated with the ServiceBusMessage during Service Bus operations. A common use-case for getApplicationProperties() is to associate serialization hints for the getBody() as an aid to consumers who wish to deserialize the binary data.
      Returns:
      Application properties associated with this ServiceBusMessage.
    • getBody

      public com.azure.core.util.BinaryData getBody()
      Gets the actual payload wrapped by the ServiceBusMessage.

      The BinaryData wraps byte array and is an abstraction over many different ways it can be represented. It provides convenience APIs to serialize/deserialize the object.

      If the means for deserializing the raw data is not apparent to consumers, a common technique is to make use of getApplicationProperties() when creating the event, to associate serialization hints as an aid to consumers who wish to deserialize the binary data.

      Returns:
      Binary data representing the payload.
      Throws:
      IllegalStateException - if called for the messages which are not of binary data type.
    • getContentType

      public String getContentType()
      Gets the content type of the message.

      Optionally describes the payload of the message, with a descriptor following the format of RFC2045, Section 5, for example "application/json".

      Returns:
      The content type of the ServiceBusMessage.
    • setContentType

      public ServiceBusMessage setContentType(String contentType)
      Sets the content type of the ServiceBusMessage.

      Optionally describes the payload of the message, with a descriptor following the format of RFC2045, Section 5, for example "application/json".

      Parameters:
      contentType - RFC2045 Content-Type descriptor of the message.
      Returns:
      The updated ServiceBusMessage.
    • getCorrelationId

      public String getCorrelationId()
      Gets a correlation identifier.

      Allows an application to specify a context for the message for the purposes of correlation, for example reflecting the MessageId of a message that is being replied to.

      Returns:
      The correlation id of this message.
      See Also:
    • setCorrelationId

      public ServiceBusMessage setCorrelationId(String correlationId)
      Sets a correlation identifier.
      Parameters:
      correlationId - correlation id of this message
      Returns:
      The updated ServiceBusMessage.
      See Also:
    • getSubject

      public String getSubject()
      Gets the subject for the message.

      This property enables the application to indicate the purpose of the message to the receiver in a standardized fashion, similar to an email subject line. The mapped AMQP property is "subject".

      Returns:
      The subject for the message.
    • setSubject

      public ServiceBusMessage setSubject(String subject)
      Sets the subject for the message.
      Parameters:
      subject - The application specific subject.
      Returns:
      The updated ServiceBusMessage object.
    • getMessageId

      public String getMessageId()
      Gets the message id.

      The message identifier is an application-defined value that uniquely identifies the message and its payload. The identifier is a free-form string and can reflect a GUID or an identifier derived from the application context. If enabled, the duplicate detection feature identifies and removes second and further submissions of messages with the same messageId.

      Returns:
      Id of the ServiceBusMessage.
    • setMessageId

      public ServiceBusMessage setMessageId(String messageId)
      Sets the message id.
      Parameters:
      messageId - The message id to be set.
      Returns:
      The updated ServiceBusMessage.
      Throws:
      IllegalArgumentException - if messageId is too long.
    • getPartitionKey

      public String getPartitionKey()
      Gets the partition key for sending a message to a partitioned entity.

      For partitioned entities, setting this value enables assigning related messages to the same internal partition, so that submission sequence order is correctly recorded. The partition is chosen by a hash function over this value and cannot be chosen directly. For session-aware entities, the sessionId property overrides this value.

      Returns:
      The partition key of this message.
      See Also:
    • setPartitionKey

      public ServiceBusMessage setPartitionKey(String partitionKey)
      Sets a partition key for sending a message to a partitioned entity
      Parameters:
      partitionKey - The partition key of this message.
      Returns:
      The updated ServiceBusMessage.
      Throws:
      IllegalArgumentException - if partitionKey is too long or if the partitionKey does not match the sessionId.
      See Also:
    • getRawAmqpMessage

      public com.azure.core.amqp.models.AmqpAnnotatedMessage getRawAmqpMessage()
      Gets the AmqpAnnotatedMessage.
      Returns:
      The raw AMQP message.
    • getReplyTo

      public String getReplyTo()
      Gets the address of an entity to send replies to.

      This optional and application-defined value is a standard way to express a reply path to the receiver of the message. When a sender expects a reply, it sets the value to the absolute or relative path of the queue or topic it expects the reply to be sent to.

      Returns:
      ReplyTo property value of this message
      See Also:
    • setReplyTo

      public ServiceBusMessage setReplyTo(String replyTo)
      Sets the address of an entity to send replies to.
      Parameters:
      replyTo - ReplyTo property value of this message
      Returns:
      The updated ServiceBusMessage.
      See Also:
    • getTo

      public String getTo()
      Gets the "to" address.

      This property is reserved for future use in routing scenarios and presently ignored by the broker itself. Applications can use this value in rule-driven auto-forward chaining scenarios to indicate the intended logical destination of the message.

      Returns:
      "To" property value of this message
    • setTo

      public ServiceBusMessage setTo(String to)
      Sets the "to" address.

      This property is reserved for future use in routing scenarios and presently ignored by the broker itself. Applications can use this value in rule-driven auto-forward chaining scenarios to indicate the intended logical destination of the message.

      Parameters:
      to - To property value of this message.
      Returns:
      The updated ServiceBusMessage.
    • getTimeToLive

      public Duration getTimeToLive()
      Gets the duration before this message expires.

      This value is the relative duration after which the message expires, starting from the instant the message has been accepted and stored by the broker, as captured in getScheduledEnqueueTime(). When not set explicitly, the assumed value is the DefaultTimeToLive set for the respective queue or topic. A message-level TimeToLive value cannot be longer than the entity's DefaultTimeToLive setting and it is silently adjusted if it does.

      Returns:
      Time to live duration of this message
      See Also:
    • setTimeToLive

      public ServiceBusMessage setTimeToLive(Duration timeToLive)
      Sets the duration of time before this message expires.
      Parameters:
      timeToLive - Time to Live duration of this message
      Returns:
      The updated ServiceBusMessage.
      See Also:
    • getScheduledEnqueueTime

      public OffsetDateTime getScheduledEnqueueTime()
      Gets the scheduled enqueue time of this message.

      This value is used for delayed message availability. The message is safely added to the queue, but is not considered active and therefore not retrievable until the scheduled enqueue time. Mind that the message may not be activated (enqueued) at the exact given datetime; the actual activation time depends on the queue's workload and its state.

      Returns:
      the datetime at which the message will be enqueued in Azure Service Bus
      See Also:
    • setScheduledEnqueueTime

      public ServiceBusMessage setScheduledEnqueueTime(OffsetDateTime scheduledEnqueueTime)
      Sets the scheduled enqueue time of this message. A null will not be set. If this value needs to be unset it could be done by value removing from AmqpAnnotatedMessage.getMessageAnnotations() using key AmqpMessageConstant.SCHEDULED_ENQUEUE_UTC_TIME_NAME.
      Parameters:
      scheduledEnqueueTime - the datetime at which this message should be enqueued in Azure Service Bus.
      Returns:
      The updated ServiceBusMessage.
      See Also:
    • getReplyToSessionId

      public String getReplyToSessionId()
      Gets or sets a session identifier augmenting the ReplyTo address.

      This value augments the reply to information and specifies which sessionId should be set for the reply when sent to the reply entity.

      Returns:
      The getReplyToGroupId property value of this message.
      See Also:
    • setReplyToSessionId

      public ServiceBusMessage setReplyToSessionId(String replyToSessionId)
      Gets or sets a session identifier augmenting the ReplyTo address.
      Parameters:
      replyToSessionId - The ReplyToGroupId property value of this message.
      Returns:
      The updated ServiceBusMessage.
    • getSessionId

      public String getSessionId()
      Gets the session identifier for a session-aware entity.

      For session-aware entities, this application-defined value specifies the session affiliation of the message. Messages with the same session identifier are subject to summary locking and enable exact in-order processing and demultiplexing. For session-unaware entities, this value is ignored. See Message Sessions.

      Returns:
      The session id of the ServiceBusMessage.
      See Also:
    • setSessionId

      public ServiceBusMessage setSessionId(String sessionId)
      Sets the session identifier for a session-aware entity.
      Parameters:
      sessionId - The session identifier to be set.
      Returns:
      The updated ServiceBusMessage.
      Throws:
      IllegalArgumentException - if sessionId is too long or if the sessionId does not match the partitionKey.
    • addContext

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