Created
June 29, 2018 21:15
-
-
Save gustavoamigo/aa33b65d0167cfb47d1a71d7e9cc86de to your computer and use it in GitHub Desktop.
Reprocess SQS Dead Letter Messages AWS
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
package script; | |
import com.amazonaws.services.sqs.AmazonSQS; | |
import com.amazonaws.services.sqs.AmazonSQSClientBuilder; | |
import com.amazonaws.services.sqs.model.Message; | |
import com.amazonaws.services.sqs.model.ReceiveMessageRequest; | |
import com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry; | |
import java.util.List; | |
import java.util.UUID; | |
import java.util.stream.Collectors; | |
public class ReprocessDeadLetter { | |
public static void main(String[] args) { | |
String deadLetterQueueUrl = ""; | |
String mainQueueUrl = ""; | |
AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient(); | |
boolean cont = true; | |
int messagesSent = 0; | |
while(cont) { | |
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest().withQueueUrl(deadLetterQueueUrl).withMaxNumberOfMessages(10); | |
List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); | |
if(messages.size() >= 1) { | |
List<SendMessageBatchRequestEntry> messagesToSend = messages.stream().map(m -> { | |
SendMessageBatchRequestEntry sendMessageRequest = new SendMessageBatchRequestEntry().withMessageBody(m.getBody()).withId(UUID.randomUUID().toString()); | |
return sendMessageRequest; | |
}).collect(Collectors.toList()); | |
sqs.sendMessageBatch(mainQueueUrl, messagesToSend); | |
messagesSent += messages.size(); | |
for (Message m : messages) { | |
sqs.deleteMessage(deadLetterQueueUrl, m.getReceiptHandle()); | |
} | |
} else { | |
cont = false; | |
} | |
} | |
System.out.println("Messages sent to reprocess messagesSent=" + messagesSent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment