SQS 队列中的确认消息
Acknowledge message in SQS queue
我正在使用 Amazon SQS 和 Amazon SQS-JMS java 库和 Java EE 7。我想要实现的是在收到消息后,根据应用程序的业务逻辑确认(使用)消息或再次将其重新发送到队列,并在 3 次重试失败后将其移至 DLQ。
我虽然在 JMS 中使用 CLIENT_Acknowledge 模式并且只确认已成功处理的消息,但这是来自他们的官方文档:
In this mode, when a message is acknowledged, all messages received before this message are implicitly acknowledged as well. For example, if 10 messages are received, and only the 10th message is acknowledged (in the order the messages are received), then all of the previous nine messages are also acknowledged.
对我来说,这是一种奇怪的行为,与我对 client_acknowledge 的期望相反。这里是否有比根据进程状态在整个代码中手动将消息发送到主 SQS 队列或 DLQ 更优雅的解决方案?
要处理这种情况,您可以为您创建的 DLQ 使用 RedrivePolicy
属性。这种情况的解决方案可以是:
- 创建一个 2 sqs Qs
my_q
和 my_q_dl
(后一个用于 DLQ)
- 使用
RedrivePolicy
将 DLQ my_q_dl
设置为 my_q
的 DLQ。
- 这里要注意指定
deadLetterTargetArn
和maxReceiveCount
。 maxReceiveCount
是您希望在将任何消息发送到 DLQ 之前在不确认的情况下处理任何消息的次数。如果你设置 maxReceiveCount
=3 那么,消息将保留在 my_q
中,直到消费者第三次拉取而没有确认。
这里有2个案例:
- 正常情况:一旦收到 ack,msg 就会被删除。
- 如果该消息直到第三次都没有确认(消息删除),则消息将从
my_q
中删除并推送到
my_q_dl
本身。
*RedrivePolicy - The string that includes the parameters for the deadletter queue functionality of the source queue.
deadLetterTargetArn - The Amazon Resource Name (ARN) of the dead-letter queue to which Amazon SQS moves messages after the value
of maxReceiveCount is exceeded.
maxReceiveCount - The number of times a message is delivered to the source queue before being moved to the dead-letter queue.
备注
FIFO 队列的死信队列也必须是 FIFO 队列。同样,标准队列的死信队列也必须是
标准队列。*
您可以使用:
UNORDERED_ACKNOWLEDGE
SQSSession.UNORDERED_ACKNOWLEDGE
它来自 'com.amazon.sqs.javamessaging;',正如它在文档中所述,它是 Client_Acknowledge 的变体,它只确认调用它的消息。
/**
* Non standard acknowledge mode. This is a variation of CLIENT_ACKNOWLEDGE
* where Clients need to remember to call acknowledge on message. Difference
* is that calling acknowledge on a message only acknowledge the message
* being called.
*/
依赖示例:
"com.amazonaws:amazon-sqs-java-messaging-lib:1.0.3"
我正在使用 Amazon SQS 和 Amazon SQS-JMS java 库和 Java EE 7。我想要实现的是在收到消息后,根据应用程序的业务逻辑确认(使用)消息或再次将其重新发送到队列,并在 3 次重试失败后将其移至 DLQ。
我虽然在 JMS 中使用 CLIENT_Acknowledge 模式并且只确认已成功处理的消息,但这是来自他们的官方文档:
In this mode, when a message is acknowledged, all messages received before this message are implicitly acknowledged as well. For example, if 10 messages are received, and only the 10th message is acknowledged (in the order the messages are received), then all of the previous nine messages are also acknowledged.
对我来说,这是一种奇怪的行为,与我对 client_acknowledge 的期望相反。这里是否有比根据进程状态在整个代码中手动将消息发送到主 SQS 队列或 DLQ 更优雅的解决方案?
要处理这种情况,您可以为您创建的 DLQ 使用 RedrivePolicy
属性。这种情况的解决方案可以是:
- 创建一个 2 sqs Qs
my_q
和my_q_dl
(后一个用于 DLQ) - 使用
RedrivePolicy
将 DLQmy_q_dl
设置为my_q
的 DLQ。 - 这里要注意指定
deadLetterTargetArn
和maxReceiveCount
。maxReceiveCount
是您希望在将任何消息发送到 DLQ 之前在不确认的情况下处理任何消息的次数。如果你设置maxReceiveCount
=3 那么,消息将保留在my_q
中,直到消费者第三次拉取而没有确认。 这里有2个案例:- 正常情况:一旦收到 ack,msg 就会被删除。
- 如果该消息直到第三次都没有确认(消息删除),则消息将从
my_q
中删除并推送到my_q_dl
本身。
*RedrivePolicy - The string that includes the parameters for the deadletter queue functionality of the source queue.
deadLetterTargetArn - The Amazon Resource Name (ARN) of the dead-letter queue to which Amazon SQS moves messages after the value of maxReceiveCount is exceeded.
maxReceiveCount - The number of times a message is delivered to the source queue before being moved to the dead-letter queue.
备注 FIFO 队列的死信队列也必须是 FIFO 队列。同样,标准队列的死信队列也必须是 标准队列。*
您可以使用:
UNORDERED_ACKNOWLEDGE
SQSSession.UNORDERED_ACKNOWLEDGE
它来自 'com.amazon.sqs.javamessaging;',正如它在文档中所述,它是 Client_Acknowledge 的变体,它只确认调用它的消息。
/**
* Non standard acknowledge mode. This is a variation of CLIENT_ACKNOWLEDGE
* where Clients need to remember to call acknowledge on message. Difference
* is that calling acknowledge on a message only acknowledge the message
* being called.
*/
依赖示例: "com.amazonaws:amazon-sqs-java-messaging-lib:1.0.3"