Skip to content

Commit

Permalink
Simplify not adding type header in SqsTemplate sent messages (#659) (
Browse files Browse the repository at this point in the history
…#1066)

---------

Co-authored-by: Maciej Walkowiak <walkowiak.maciej@yahoo.com>
  • Loading branch information
levys17 and maciejwalkowiak authored Mar 15, 2024
1 parent 417d8b8 commit 3643a9b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/src/main/asciidoc/sqs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,8 @@ public interface MessagingMessageConverter<S> {

The default header-based type mapping can be configured to use a different header name by using the `setPayloadTypeHeader` method.

It is also possible not to include payload type information in the header by using the `doNotSendPayloadTypeHeader` method.

More complex mapping can be achieved by using the `setPayloadTypeMapper` method, which overrides the default header-based mapping.
This method receives a `Function<Message<?>, Class<?>> payloadTypeMapper` that will be applied to incoming messages.

Expand All @@ -1260,6 +1262,9 @@ SqsMessagingMessageConverter messageConverter = new SqsMessagingMessageConverter
// Configure Type Header
messageConverter.setPayloadTypeHeader("myTypeHeader");
// Do not send Type Header
messageConverter.doNotSendPayloadTypeHeader();
// Configure Header Mapper
SqsHeaderMapper headerMapper = new SqsHeaderMapper();
headerMapper.setAdditionalHeadersFunction(((sqsMessage, accessor) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ public void setHeaderMapper(HeaderMapper<S> headerMapper) {
this.headerMapper = headerMapper;
}

/**
* Configure the converter to not include payload type information in the
* {@link software.amazon.awssdk.services.sqs.model.Message} headers.
*/
public void doNotSendPayloadTypeHeader() {
this.payloadTypeHeaderFunction = message -> null;
}

protected abstract HeaderMapper<S> createDefaultHeaderMapper();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.IntStream;

import io.awspring.cloud.sqs.support.converter.AbstractMessagingMessageConverter;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -305,7 +307,7 @@ void shouldSendAndReceiveBatchFifo() {
@Test
void shouldSendAndReceiveRecordMessageWithoutPayloadInfoHeader() {
SqsTemplate template = SqsTemplate.builder().sqsAsyncClient(this.asyncClient)
.configureDefaultConverter(converter -> converter.setPayloadTypeHeaderValueFunction(msg -> null))
.configureDefaultConverter(AbstractMessagingMessageConverter::doNotSendPayloadTypeHeader)
.build();
SampleRecord testRecord = new SampleRecord("Hello world!",
"From shouldSendAndReceiveRecordMessageWithoutPayloadInfoHeader!");
Expand Down

0 comments on commit 3643a9b

Please sign in to comment.