Camel AWS-SQS:动态设置 visibilityTimeout

Camel AWS-SQS: Set visibilityTimeout dynamically

我有一条骆驼路线,大致定义如下:

from("aws-sqs://my-queue")
.process(myProcessor)
.to(myEndpoint);

然而,myProcessor 依赖于有时变得不可用的上游服务或 returns "pending" 响应,因此处理器抛出异常并且交换留在队列中。然后 Camel 轮询并连续失败,直到消息被 DLQed。

我想做的是动态配置 visibilityTimeout 以实现指数退避,直到上游服务再次可用。因此,我会在我的路由和内部注册一个异常处理程序,而不是 from("aws-sqs://my-queue?visibilityTimeout=30"),做一些类似的事情:

sqsClient.setVisibilityTimeout(Math.pow(2 * visibilityTimeout));

这可能吗?作为奖励,是否可以为特定消息 ID 配置可见性超时(例如,它不会影响其他消息的可见性超时)?

是的,可以更改正在处理的消息的 VisibilityTimeout。

来自AWS docs

When you receive a message for a queue and begin to process it, the visibility timeout for the queue may be insufficient (for example, you might need to process and delete a message). You can shorten or extend a message's visibility by specifying a new timeout value using the ChangeMessageVisibility action.

For example, if the timeout for a queue is 60 seconds, 15 seconds have elapsed, and you send a ChangeMessageVisibility call with VisibilityTimeout set to 10 seconds, the total timeout value will be the elapsed time (15 seconds) plus the new timeout value (10 seconds), a total of 25 seconds. Sending a call after 25 seconds will result in an error.

Note

The new timeout period will take effect from the time you call the ChangeMessageVisibility action. In addition, the new timeout period will apply only to the particular receipt of the message. The ChangeMessageVisibility action does not affect the timeout of later receipts of the message or later queues.

A​​WS SQS 提供了通过调用 "ChangeMessageVisibility" 方法在消息级别(处理消息时)更改 visibilityTimeout 的灵活性。

但是无法检索消息的当前可见性超时以执行所需的操作 (Math.pow(2 * visibilityTimeout));

ChangeMes​​sageVisibility 方法调用:

sqsClient.ChangeMessageVisibility(myEndpoint, myMessage.getReceiptHandle(), 60);

以上代码将 60 秒的 visibilityTimeout 添加到消息的超时时间。

(即)如果消息的默认队列级别 visibilityTimeout 还剩 30 秒,则上面的代码会向其添加 60 秒,并将消息对其他消费者的不可用性增加到 90 秒。