Change broker property of Azure Service Bus message using azure-spring-cloud-stream-binder Library

Published 03-22-2021 03:33 AM 696 Views

Azure Service bus ||change broker property of Azure Service Bus message using azure-spring-cloud-stream-binder-servicebus Library


Use Case:

To change broker property of Azure Service Bus message using spring-cloud-stream-binder-servicebus library.



  • Azure Service bus Namespace
  • Azure Service bus SAS connection string
  • Console Application to change the messageID of the service bus using spring-cloud-stream library.


  • Set the custom MessageID of the Service bus message using the Spring-cloud-stream library.

Steps to follow:

At present azure-spring-cloud-stream-binder-servicebus library do not support setting the messageID.

MessageID/Broker properties are auto-generated from spring-cloud library and doesn’t use the properties from Service bus Message. 


Here we are trying to set the MessageID of the Service bus broker message as ‘ce095552-b466-4d03-ac41-430ec9286806’, however post receiving the same message and checking the messageID, it will be different ‘c0491323-a39b-3d19-a983-1157b055fdcf’ as this is auto-generated by the spring-cloud library as shown below:


Code trying to set the MessageID of the broker message:



Reference Code


Map<String,Object> accessorMap = new HashMap<>();
accessorMap.put(MessageHeaders.ID, "ce095552-b466-4d03-ac41-430ec9286806");
accessorMap.put("messageId", "ce095552-b466-4d03-ac41-430ec9286806");
accessorMap.put(MessageHeaders.TIMESTAMP, new Date().getTime());


Reference Payload

StreamMessage streamMsg = StreamMessage.builder().id(123123)

  .message("Message from Publisher").build();


Construction of Message newMessage = new"ce095552-b466-4d03-ac41-430ec9286806",MessageBody.fromValueData(streamMsg.toString()),"application/json");





Sent Message using Spring cloud stream

source.output().send(new GenericMessage<>(newMessage));



MessageID of the message set with above code:




Refer the workaround, in following code, you can provide a subclass of ServiceBusTopicTemplate, and override the logic of how the message id is defined.

So, in the example below, you can set the header when building spring integration message.


public ServiceBusTopicOperation topicOperation(ServiceBusTopicClientFactory factory) {
    return new CustomizeMessageIdServiceBusTopicTemplate(factory);

public static class CustomizeMessageIdServiceBusTopicTemplate extends ServiceBusTopicTemplate {

    public CustomizeMessageIdServiceBusTopicTemplate(
        ServiceBusTopicClientFactory clientFactory) {
        this.messageConverter = new ServiceBusMessageConverter(){
            protected void setCustomHeaders(MessageHeaders headers, IMessage serviceBusMessage) {
                super.setCustomHeaders(headers, serviceBusMessage);



Message<String> build = MessageBuilder.withPayload(message).setHeader("customer-message-id", WATH_EVER_VALUE_WANT_TO_BE_USED_AS_MESSAGE_ID).build();





Running the above sample code, you should be able to set the MessageID of the Azure service bus using azure-spring-cloud-stream-binder-servicebus library.

Hope this helps!








Version history
Last update:
‎Mar 22 2021 03:33 AM
Updated by: