将消息发布回 AWS SQS,可见时间为20秒。

huangapple go评论58阅读模式
英文:

Release a message back to AWS SQS with 20secs visibility time

问题

I am using spring-boot:2.2.6.RELEASE and aws-java-sdk-sqs:1.11.415. I have a listener which polls message from SQS. There is a condition where the listener process the message and if the condition is not met then we should place the message back to the queue and it should be available to any other component or same listener after 20secs. I am not sure how to achieve the later one, place message back to the queue and it should be visible after 20secs to other listeners,

Please find below code for reference,

@Autowired
private QueueMessagingTemplate messagingTemplate;

@PostMapping("/employee")
public String save(@RequestBody Employee employee){
    Map<String,Object> headers = new HashMap<>();
    headers.put("subject", "send employee details to sqs");
    headers.put("name","Donald");
    headers.put("traceId","sample");
    messagingTemplate.convertAndSend("sample-standard-queue", employee, headers);
    return "success";
}

@SqsListener(value = "sample-standard-queue")
public void receivesqs(Employee message, @Headers Map<String,Object> headers, @Header("name") String name) {

    // Retrieve employee details from DB using message.id
    // If it is in-progress then place the message back to the queue and it should be visible after 20secs
}

I saw this post But it place the message back to the queue and it will be immediately available to other readers. In my case it should be available after 20secs.

英文:

I am using spring-boot:2.2.6.RELEASE and aws-java-sdk-sqs:1.11.415. I have a listener which polls message from SQS. There is a condition where the listener process the message and if the condition is not met then we should place the message back to the queue and it should be available to any other component or same listener after 20secs. I am not sure how to achieve the later one, place message back to the queue and it should be visible after 20secs to other listeners,

Please find below code for reference,

@Autowired
private QueueMessagingTemplate messagingTemplate;

@PostMapping(&quot;/employee&quot;)
public String save(@RequestBody Employee employee){
    Map&lt;String,Object&gt; headers = new HashMap&lt;&gt;();
    headers.put(&quot;subject&quot;, &quot;send employee details to sqs&quot;);
    headers.put(&quot;name&quot;,&quot;Donald&quot;);
    headers.put(&quot;traceId&quot;,&quot;sample&quot;);
    messagingTemplate.convertAndSend(&quot;sample-standard-queue&quot;, employee, headers);
    return &quot;success&quot;;
}

@SqsListener(value = &quot;sample-standard-queue&quot;)
public void receivesqs(Employee message, @Headers Map&lt;String,Object&gt; headers, @Header(&quot;name&quot;) String name) {

    // Retrieve employee details from DB using message.id
    // If it is in-progress then place the message back to the queue and it should be visible after 20secs
}

I saw this post But it place the message back to the queue and it will be immediately available to other readers. In my case it should be available after 20secs.

答案1

得分: 1

以下是您要翻译的内容:

根据此处提到的SQS Listener Manual Acknowledgement

@SqsListener(value = &quot;sample-standard-queue&quot;, deletionPolicy = SqsMessageDeletionPolicy.NEVER) 不会删除消息,您可以通过使用确认参数来手动删除它们。

@SqsListener(value = &quot;sample-standard-queue&quot;, deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void receivesqs(Employee message, @Headers Map&lt;String,Object&gt; headers, @Header(&quot;name&quot;) String name,Acknowledgment acknowledgment)  {
    // 决定何时确认
    acknowledgment.acknowledge().get();
}

根据您的可见性超时设置,消息将在可见性超时秒后重新出现在队列中。

英文:

As mentioned here SQS Listener Manual Acknowledgement

@SqsListener(value = &quot;sample-standard-queue&quot;, deletionPolicy = SqsMessageDeletionPolicy.NEVER) would not delete the messages, you can manually delete them by having a acknowledgment parameter.

 @SqsListener(value = &quot;sample-standard-queue&quot;, deletionPolicy = SqsMessageDeletionPolicy.NEVER)
    public void receivesqs(Employee message, @Headers Map&lt;String,Object&gt; headers, @Header(&quot;name&quot;) String name,Acknowledgment acknowledgment)  {
        // decide when to acknowledge
        acknowledgment.acknowledge().get();
    }

Based on your visibility timeout settings, message will reappear in the queue after the visibility timeout seconds.

huangapple
  • 本文由 发表于 2020年8月6日 06:00:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63274120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定