spring-cloud-aws icon indicating copy to clipboard operation
spring-cloud-aws copied to clipboard

Simplify not adding type header in SqsTemplate sent messages

Open tomazfernandes opened this issue 2 years ago • 5 comments

Currently in order to not add type information to messages sent with the SqsTemplate it's necessary to do something like this:

SqsTemplate template = SqsTemplate.builder().sqsAsyncClient(this.asyncClient)
		.configureDefaultConverter(converter -> converter.setPayloadTypeHeaderValueFunction(msg -> null))
		.build();

We could make it more user-friendly and perhaps have a TemplateOptions.doNotAddTypeInformationToSentMessages() method or something similar.

tomazfernandes avatar Feb 03 '23 11:02 tomazfernandes

May I try this one? Seems to be good to start!

levys17 avatar Feb 08 '24 14:02 levys17

Sounds good @levys17!

Let me know if there's anything you'd like to discuss.

tomazfernandes avatar Feb 09 '24 00:02 tomazfernandes

Hii, @tomazfernandes!

Considering that AbstractMessagingMessageConverter is responsible to set headers ( typeHeader included), what do you thing about implement this method in this class?

public void sendMessageWithoutTypeHeader() {
		this.payloadTypeHeaderFunction = message -> null;
}
@Test
	void shouldSendAndReceiveRecordMessageWithoutPayloadInfoHeader() {
		SqsTemplate template = SqsTemplate.builder().sqsAsyncClient(this.asyncClient)
				.configureDefaultConverter(converter -> converter.sendMessageWithoutTypeHeader())
				.build();
		SampleRecord testRecord = new SampleRecord("Hello world!",
				"From shouldSendAndReceiveRecordMessageWithoutPayloadInfoHeader!");
		SendResult<SampleRecord> result = template.send(RECORD_WITHOUT_TYPE_HEADER_QUEUE_NAME, testRecord);
		assertThat(result).isNotNull();
		Optional<Message<SampleRecord>> receivedMessage = template
				.receive(from -> from.queue(RECORD_WITHOUT_TYPE_HEADER_QUEUE_NAME), SampleRecord.class);
		assertThat(receivedMessage).isPresent().get().extracting(Message::getPayload).isEqualTo(testRecord);
	}

Should I keep going implementing this way or perhaps there is a better way?

levys17 avatar Feb 22 '24 01:02 levys17

Hey @levys17, sorry for the delay!

what do you thing about implement this method in this class?

This makes sense. I had thought of setting it in the ContainerOptions to try to make it more obvious and easier to find, but maybe it'd just add unnecessary complexity.

Regarding the method name, how about converter.doNotSendTypeHeader()? Seems a bit more straightforward, but I'm open to suggestions.

Let me know your thoughts, thanks!

tomazfernandes avatar Feb 27 '24 00:02 tomazfernandes

No problem, @tomazfernandes! Thanks for answer! Ok, I'll finish the implementation considering this. About the method name, seems really better than I used.

I'll open a PR soon.

Thanks!

levys17 avatar Feb 27 '24 10:02 levys17

@tomazfernandes looks like a good candidate for 3.1.1?

maciejwalkowiak avatar Mar 09 '24 08:03 maciejwalkowiak

@tomazfernandes looks like a good candidate for 3.1.1?

Yup, definitely!

tomazfernandes avatar Mar 09 '24 18:03 tomazfernandes

Fixed in https://github.com/awspring/spring-cloud-aws/pull/1066

maciejwalkowiak avatar Mar 15 '24 10:03 maciejwalkowiak