英文:
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("/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.
答案1
得分: 1
以下是您要翻译的内容:
根据此处提到的SQS Listener Manual Acknowledgement
@SqsListener(value = "sample-standard-queue", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
不会删除消息,您可以通过使用确认参数来手动删除它们。
@SqsListener(value = "sample-standard-queue", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void receivesqs(Employee message, @Headers Map<String,Object> headers, @Header("name") String name,Acknowledgment acknowledgment) {
// 决定何时确认
acknowledgment.acknowledge().get();
}
根据您的可见性超时设置,消息将在可见性超时秒后重新出现在队列中。
英文:
As mentioned here SQS Listener Manual Acknowledgement
@SqsListener(value = "sample-standard-queue", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
would not delete the messages, you can manually delete them by having a acknowledgment parameter.
@SqsListener(value = "sample-standard-queue", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void receivesqs(Employee message, @Headers Map<String,Object> headers, @Header("name") 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论