【问题标题】:How to change AWS spring cloud SQS deletion policy dynamically如何动态更改 AWS Spring Cloud SQS 删除策略
【发布时间】:2020-03-02 17:14:17
【问题描述】:
目前我有一个要求,我们正在收听一个 SQS,但基于开发或测试环境,我应该将删除策略更改为 on_SUCCESS 或 NEVER。
有什么方法可以让我们动态处理它。我正在使用 AWS 开发工具包。
@SqsListener(value = "${application.marketplace.aws.queueName}", deletionPolicy =SqsMessageDeletionPolicy.NEVER)
public void pollSQSMsgAscync(final String message) throws Exception {}
【问题讨论】:
标签:
amazon-web-services
spring-cloud
amazon-sqs
【解决方案1】:
你可以通过使用确认来做到这一点
@SqsListener(value = "${queue.res-notification}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void receiveMessage(@NotificationMessage String message, @NotificationSubject String subject, Acknowledgment acknowledgment, @Header("ApproximateReceiveCount") final int receiveCount) {
try {
if(<some condition>){
acknowledgment.acknowledge(); // this will delete the message
}else{
//do not acknowledge so that message will not be deleted and stays in the queue for new round of reading
}
} catch (Exception e) {
log.error("Error reading notification ", e);
}
}