简体   繁体   English

SqsListener 方法运行异常并且似乎没有轮询所有消息?

[英]SqsListener method is acting strangely and doesn't seem to poll all messages?

I have set up Spring Boot with Amazon SQS.我已经使用 Amazon SQS 设置了 Spring Boot。 My listener method is just printing out the received messages and I'm running a test server.我的侦听器方法只是打印出收到的消息,我正在运行一个测试服务器。 I have a sender method that sends a message every 30 seconds but my received only shows (in the log) receives one message every approximately 5 or 6 minutes which I can't find anywhere I have configured that and it only receives the last one that is being sent not all the others in between.我有一个发件人方法,每 30 秒发送一条消息,但我收到的消息只显示(在日志中)每大约 5 或 6 分钟收到一条消息,我在配置的任何地方都找不到,它只收到最后一条消息正在发送的不是所有其他人。

Looking at the monitoring tab in AWS console I can't really tell if my listener is consuming them or not.查看 AWS 控制台中的监控选项卡,我无法真正判断我的侦听器是否正在使用它们。 It's my first time using Amazon SQS and I'm not sure if something I am missing in my configuration.这是我第一次使用 Amazon SQS,我不确定我的配置中是否遗漏了什么。

This is some of my configurations:这是我的一些配置:

    @Bean
    fun simpleMessageListenerContainerFactory(amazonSqs: AmazonSQSAsync?): SimpleMessageListenerContainerFactory? {
        val factory = SimpleMessageListenerContainerFactory()
        factory.setAmazonSqs(amazonSqs)
        factory.setAutoStartup(true)
        factory.setMaxNumberOfMessages(5)
        //factory.setVisibilityTimeout(10)
        factory.setWaitTimeOut(15)
        // ...
        return factory
    }

    @Bean
    fun queueMessageHandlerFactory(): QueueMessageHandlerFactory? {
        val factory = QueueMessageHandlerFactory()
        val messageConverter = MappingJackson2MessageConverter()
        messageConverter.isStrictContentTypeMatch = false
        factory.setArgumentResolvers(mutableListOf<HandlerMethodArgumentResolver>(PayloadMethodArgumentResolver(messageConverter)))
        return factory
    }

and here is my sender/listener service:这是我的发件人/收听者服务:

@Service
class MyMessageService(
    private val queueMessagingTemplate: QueueMessagingTemplate
){



    fun send(message: String) {

        val headers: MutableMap<String, Any> = HashMap()
        headers["message-group-id"] = "messageGroupId-${(1..1000).random()}"
        headers["message-deduplication-id"] = "messageDeduplicationId"
        logger.info { "===> Sending $message" }
        queueMessagingTemplate.send("MyTestQueue.fifo",
            MessageBuilder.createMessage(message, SqsMessageHeaders(headers)))
    }

    @SqsListener("MyTestQueue.fifo", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
    fun receive(message: String) {
        logger.info { "<=== Received $message" }
    }
}

And this is the log, as you can see the listener doesn't seem to receive everything:这是日志,正如您所看到的,侦听器似乎没有收到所有信息:

2022-11-18 12:17:00.009  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 226 }
2022-11-18 12:17:30.004  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 23 }
2022-11-18 12:18:00.005  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 488 }
2022-11-18 12:18:00.082  INFO 75159 --- [enerContainer-2]      : <=== Received { "test": 488 }
2022-11-18 12:18:30.003  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 240 }
2022-11-18 12:19:00.004  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 797 }
2022-11-18 12:19:30.005  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 501 }
2022-11-18 12:20:00.004  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 978 }
2022-11-18 12:20:30.003  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 583 }
2022-11-18 12:21:00.001  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 124 }
2022-11-18 12:21:30.005  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 218 }
2022-11-18 12:22:00.002  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 617 }
2022-11-18 12:22:30.004  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 581 }
2022-11-18 12:23:00.004  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 984 }
2022-11-18 12:23:30.010  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 827 }
2022-11-18 12:23:30.070  INFO 75159 --- [enerContainer-2]      : <=== Received { "test": 827 }
2022-11-18 12:24:00.010  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 363 }
2022-11-18 12:24:30.003  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 990 }
2022-11-18 12:25:00.006  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 844 }
2022-11-18 12:25:30.005  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 650 }
2022-11-18 12:26:00.004  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 420 }
2022-11-18 12:26:30.006  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 719 }
2022-11-18 12:27:00.009  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 873 }
2022-11-18 12:27:30.004  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 521 }
2022-11-18 12:28:00.006  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 911 }
2022-11-18 12:28:30.010  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 267 }
2022-11-18 12:29:00.002  INFO 75159 --- [   scheduling-1]      : ===> Sending { "test": 164 }
2022-11-18 12:29:00.058  INFO 75159 --- [enerContainer-2]      : <=== Received { "test": 164 }

I'd like to answer my own question because this can also happen to others.我想回答我自己的问题,因为这也可能发生在其他人身上。 So basically the issue was this line:所以基本上问题是这一行:

headers["message-deduplication-id"] = "messageDeduplicationId"

It's been hardcoded to a fixed value which as explained here:它已被硬编码为固定值,如下所述:

The message deduplication ID is the token used for deduplication of sent messages.消息去重ID是用于对已发送消息去重的token。 If a message with a particular message deduplication ID is sent successfully, any messages sent with the same message deduplication ID are accepted successfully but aren't delivered during the 5-minute deduplication interval.如果成功发送了具有特定消息重复数据删除 ID 的消息,则在 5 分钟重复数据删除时间间隔内,任何使用相同消息重复数据删除 ID 发送的消息都会被成功接受,但不会被传送。

Link: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/using-messagededuplicationid-property.html链接: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/using-messagededuplicationid-property.html

The above, explains why it takes about 5 minutes for the receiver to poll new messages because the messages sent takes about 5 minutes to be pushed.上面解释了为什么接收方轮询新消息需要大约 5 分钟,因为发送的消息需要大约 5 分钟才能被推送。

I just changed that code to this and it works like a charm:我只是将该代码更改为此,它就像一个魅力:

headers["message-deduplication-id"] = "messageDeduplicationId-${(1..100).random()}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Amazon SQS Springn 云注释@SqsListener 轮询特定数量的消息 - How to Poll for a specific number of messages using Amazon SQS Springn cloud annotation @SqsListener @SqsListener 不消费队列中的消息 - @SqsListener Not consuming the messages in the queue 暂停 spring SqsListener 从队列中拉取消息 - Pause spring SqsListener to pull messages from queue SnapStart 功能启动 - 似乎不起作用 - SnapStart function priming - Doesn't seem to be working create-with-container 似乎没有启动容器 - create-with-container doesn't seem to start the container flutter FCM 订阅主题似乎不起作用 - flutter FCM subscribe to a topic doesn't seem to work AWS SQS FIFO 消息似乎没有重试 - AWS SQS FIFO message doesn't seem to be retrying 无服务器错误 AWS 配置文件“”似乎没有配置? - Serverless Error AWS profile "" doesn't seem to be configured? 为什么我的 html 文件的 javascript 部分似乎无法正常工作? - Why does the javascript part of my html file doesn't seem to work as it is supposed? 我可以通过启动轮询队列的多个 EC2 实例来并行处理多个 SQS 消息吗? - Can I process multiple SQS messages in parallel by launching multiple EC2 instances which poll the queue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM