1

I am using the spring cloud stream event binder for Azure event hubs and follow the setup in this tutorial. Now I want to set a custom value to the id Header of the message produced.

I know that those headers are read only in the Spring Message interface

So if I do something like this

 MessageBuilder.withPayload(myPayload)
                .setHeader("id", "myCustomIdHeaderValue")
                .build(),

I get

java.lang.IllegalArgumentException: 'id' header is read-only

I want to integrate with an existing system which requires the messages header id to hold specific values and specified by the producer - unfortunately I cannot use another custom header for this one. As a fallback I could use the plain azure event hub SDK but would prefer using Spring Cloud Stream binder instead if possible. The restrictions are there for valid reasons and overriding them doesn't sound like a good idea, but is there a way to overcome this anyway ?

ltsallas
  • 1,918
  • 1
  • 12
  • 25
  • 1
    System specific headers are usually prefixed. The Message interface indeed has few headers that user can NOT set/change and ID and TIMESTAMP are those headers – Oleg Zhurakousky Oct 18 '22 at 12:21
  • Thanks for the feedback @OlegZhurakousky and I agree with you, its just that this is a convention used already and I cannot adjust this contract right now. – ltsallas Oct 18 '22 at 15:18
  • 1
    Can you have some custom Map as a single header in Message headers where you fully control the name of the keys? For example (in json for clarity): `"mySpecialHeaders":{"id":123, "foo":"bar", ...}`? – Oleg Zhurakousky Oct 19 '22 at 10:43
  • Thanks again for trying to help, this solution would still require to update existing consumers and I don't control this - so I am looking for a solution that wouldn't require consumer updates. Maybe a custom MessageBuilderFactory could solve this(not use the default one)? One more scenario we discovered(opposite direction), if a plain azure sdk producer placed in the Id a custom value then a spring-cloud-stream event hub based consumer would not be able to provide this value because the framework would change it while deserializing the message – ltsallas Oct 19 '22 at 12:41

0 Answers0