ServiceBusMessage Class

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

public class ServiceBusMessage

The data structure encapsulating the message being sent to Service Bus. The message structure is discussed in detail in the product documentation.

Constructor Summary

Constructor Description
ServiceBusMessage(byte[] body)

Creates a ServiceBusMessage with given byte array body.

ServiceBusMessage(AmqpMessageBody amqpMessageBody)

This constructor provides an easy way to create ServiceBusMessage with message body as AMQP Data types SEQUENCE and VALUE.

ServiceBusMessage(BinaryData body)

Creates a ServiceBusMessage containing the body.The BinaryData provides various convenience API representing byte array.

ServiceBusMessage(ServiceBusReceivedMessage receivedMessage)

Creates a ServiceBusMessage using properties from receivedMessage.

ServiceBusMessage(String body)

Creates a ServiceBusMessage with a UTF-8 encoded body.

Method Summary

Modifier and Type Method and Description
ServiceBusMessage addContext(String key, Object value)

Adds a new key value pair to the existing context on Message.

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.

BinaryData getBody()

Gets the actual payload wrapped by the ServiceBusMessage.

String getContentType()

Gets the content type of the message.

String getCorrelationId()

Gets a correlation identifier.

String getMessageId()

Gets the message id.

String getPartitionKey()

Gets the partition key for sending a message to a partitioned entity.

AmqpAnnotatedMessage getRawAmqpMessage()

Gets the AmqpAnnotatedMessage.

String getReplyTo()

Gets the address of an entity to send replies to.

String getReplyToSessionId()

Gets or sets a session identifier augmenting the getReplyTo() address.

OffsetDateTime getScheduledEnqueueTime()

Gets the scheduled enqueue time of this message.

String getSessionId()

Gets the session identifier for a session-aware entity.

String getSubject()

Gets the subject for the message.

Duration getTimeToLive()

Gets the duration before this message expires.

String getTo()

Gets the "to" address.

ServiceBusMessage setContentType(String contentType)

Sets the content type of the ServiceBusMessage.

ServiceBusMessage setCorrelationId(String correlationId)

Sets a correlation identifier.

ServiceBusMessage setMessageId(String messageId)

Sets the message id.

ServiceBusMessage setPartitionKey(String partitionKey)

Sets a partition key for sending a message to a partitioned entity

ServiceBusMessage setReplyTo(String replyTo)

Sets the address of an entity to send replies to.

ServiceBusMessage setReplyToSessionId(String replyToSessionId)

Gets or sets a session identifier augmenting the getReplyTo() address.

ServiceBusMessage setScheduledEnqueueTime(OffsetDateTime scheduledEnqueueTime)

Sets the scheduled enqueue time of this message.

ServiceBusMessage setSessionId(String sessionId)

Sets the session identifier for a session-aware entity.

ServiceBusMessage setSubject(String subject)

Sets the subject for the message.

ServiceBusMessage setTimeToLive(Duration timeToLive)

Sets the duration of time before this message expires.

ServiceBusMessage setTo(String to)

Sets the "to" address.

Methods inherited from java.lang.Object

Constructor Details

ServiceBusMessage

public ServiceBusMessage(byte[] body)

Creates a ServiceBusMessage with given byte array body.

Parameters:

body - The content of the Service bus message.

ServiceBusMessage

public ServiceBusMessage(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.

ServiceBusMessage

public ServiceBusMessage(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.

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.

ServiceBusMessage

public ServiceBusMessage(String body)

Creates a ServiceBusMessage with a UTF-8 encoded body.

Parameters:

body - The content of the Service Bus message.

Method Details

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.

getApplicationProperties

public Map 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 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.

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.

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.

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:

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 getSessionId() property overrides this value.

Returns:

The partition key of this message.

getRawAmqpMessage

public 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

getReplyToSessionId

public String getReplyToSessionId()

Gets or sets a session identifier augmenting the getReplyTo() address.

This value augments the getReplyTo() 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.

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

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.

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.

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

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

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.

setCorrelationId

public ServiceBusMessage setCorrelationId(String correlationId)

Sets a correlation identifier.

Parameters:

correlationId - correlation id of this message

Returns:

The updated ServiceBusMessage.

setMessageId

public ServiceBusMessage setMessageId(String messageId)

Sets the message id.

Parameters:

messageId - The message id to be set.

Returns:

The updated ServiceBusMessage.

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.

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.

setReplyToSessionId

public ServiceBusMessage setReplyToSessionId(String replyToSessionId)

Gets or sets a session identifier augmenting the getReplyTo() address.

Parameters:

replyToSessionId - The ReplyToGroupId property value of this message.

Returns:

The updated ServiceBusMessage.

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.

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.

setSubject

public ServiceBusMessage setSubject(String subject)

Sets the subject for the message.

Parameters:

subject - The application specific subject.

Returns:

The updated ServiceBusMessage object.

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.

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.

Applies to