'Pause spring SqsListener to pull messages from queue
Currently I have a SQS queue that move the messages to a DLQ after 5 retries, I'm using spring SqsListener to read the queue:
@SqsListener(
value = ["\${messaging.queue.orders}"],
deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS
)
fun handleOrders(message: String) {
// process order
}
Now I'm trying to use a feature toggle to pause the listener, but I could not find a way to do that, I tried to use the Acknowledgment manually, but the message is move to the DLQ if I don't send the success ack event:
@SqsListener(
value = ["\${messaging.queue.orders}"],
deletionPolicy = SqsMessageDeletionPolicy.NEVER
)
fun handleOrders(message: String, ack: Acknowledgment) {
if(toggle.orders.enabled) {
// process order
ack.acknowledge()
}
// else don not process or ack the message
}
As I said, the problem with the solution above is that if I don't execute the ack.acknowledge() the message is moved to the DLQ after the fifth time, so I would need to pause/stop the SqsListener to pull messages from the Queue if the toggle is disabled, and start/resume if it's enabled again.
Solution 1:[1]
You can toggle creation of SqsListener using @ConditionalOnExpression
Like:
@SqsListener(
value = ["\${messaging.queue.orders}"],
deletionPolicy = SqsMessageDeletionPolicy.NEVER
)
@ConditionalOnExpression("\${toggle.orders.enabled:false}")
fun handleOrders(message: String) {
// process order
}
And adding following to application.yaml
toggle:
orders:
enabled: true
if you change the property, you will have to restart the server unless you are using spring cloud for configuration
You can also use @ConditionalOnProperty(prefix = "toggle", name = "orders.enabled")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DragneelFPS |
