Skip to content

SqsTemplate ignores the payload class of later receive calls to the same queue #1726

Open

Description

@akshit-khandelwal47

Type: Bug

Component:
SQS

Describe the bug
SqsTemplate caches the MessageConversionContext it uses for receive operations per queue, and that context includes the payload class of the first receive call to the queue. Later calls to the same queue convert to the cached class, whatever payload class they pass.

This only shows when the received message has no JavaType header, because the header takes precedence over the context. That is the case the reference docs point to receive(queue, SampleRecord.class) for: messages sent by a producer other than SqsTemplate, or with the type header disabled.

It is the conversionContextCache.computeIfAbsent(endpointName, ...) in SqsTemplate.getReceiveMessageConversionContext. The cache has been there since 3.0.0 and is still on main (4.3.0-SNAPSHOT).

Sample
A queue with a JSON message that has no JavaType header:

SqsOperations template = SqsTemplate.newSyncTemplate(sqsAsyncClient);

template.receive(from -> from.queue("test-queue"));
// payload is the JSON String, as expected

Optional<Message<SampleRecord>> message = template.receive(from -> from.queue("test-queue"), SampleRecord.class);
message.get().getPayload();
// ClassCastException: class java.lang.String cannot be cast to class SampleRecord

I have a fix with a unit test in SqsTemplateTests and can open a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions