This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use MessageWrapper to sync acknowledge and timeout extender (#23)
use MessageWrapper to sync acknowledge and timeout extender
- Loading branch information
1 parent
c746611
commit 36ee1eb
Showing
10 changed files
with
125 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/mercateo/sqs/utils/message/handling/MessageWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.mercateo.sqs.utils.message.handling; | ||
|
||
import com.amazonaws.services.sqs.AmazonSQS; | ||
import com.amazonaws.services.sqs.model.ChangeMessageVisibilityRequest; | ||
|
||
import io.awspring.cloud.messaging.listener.Acknowledgment; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
|
||
import org.springframework.messaging.Message; | ||
|
||
@RequiredArgsConstructor | ||
public class MessageWrapper<I> { | ||
|
||
@NonNull | ||
@Getter | ||
private final Message<I> message; | ||
|
||
private boolean acknowledged = false; | ||
|
||
public String getMessageId() { | ||
return message.getHeaders().get("MessageId", String.class); | ||
} | ||
|
||
public String getReceiptHandle() { | ||
return message.getHeaders().get("ReceiptHandle", String.class); | ||
} | ||
|
||
@SneakyThrows | ||
public synchronized void acknowledge() { | ||
Acknowledgment acknowledgment = message.getHeaders().get("Acknowledgment", Acknowledgment.class); | ||
if (acknowledgment == null) { | ||
throw new NullPointerException("there is no \"Acknowledgment\" in the message headers"); | ||
} | ||
acknowledgment.acknowledge().get(2, TimeUnit.MINUTES); | ||
acknowledged = true; | ||
} | ||
|
||
public synchronized void changeMessageVisibility(AmazonSQS sqsClient, ChangeMessageVisibilityRequest request) { | ||
if (acknowledged) { | ||
return; | ||
} | ||
sqsClient.changeMessageVisibility(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.