Blog Post

Azure PaaS Blog
2 MIN READ

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

yosing's avatar
yosing
Icon for Microsoft rankMicrosoft
Mar 22, 2021

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.

 

Pre-Requisites:

  • 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.

Scenarios:

  • 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:

 

Code

Reference Code

Properties

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());
accessorMap.put(MessageHeaders.CONTENT_TYPE,"application/json");

 

Reference Payload

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

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

 

Construction of Message

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

  newMessage.setPartitionKey("12");

  newMessage.setTimeToLive(Duration.ofMinutes(1));

  newMessage.setProperties(accessorMap);

 

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.

 

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

public static class CustomizeMessageIdServiceBusTopicTemplate extends ServiceBusTopicTemplate {

    public CustomizeMessageIdServiceBusTopicTemplate(
        ServiceBusTopicClientFactory clientFactory) {
        super(clientFactory);
        this.messageConverter = new ServiceBusMessageConverter(){
            @Override
            protected void setCustomHeaders(MessageHeaders headers, IMessage serviceBusMessage) {
                super.setCustomHeaders(headers, serviceBusMessage);
                if(headers.containsKey("customer-message-id")){
                    serviceBusMessage.setMessageId(headers.get("customer-message-id",String.class));
                }
            }
        };
    }
}

 

 

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!

 

 

 

 

 

 

 

Published Mar 22, 2021
Version 1.0
  • Manuel2095's avatar
    Manuel2095
    Copper Contributor

    Hi,

     

    I can not make it work. Is this still valid?

     

    Best regards.