-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Java V2: Added the follow for sending and receiving messages in batches for SQS #7508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tejasgn1
wants to merge
4
commits into
awsdocs:main
Choose a base branch
from
tejasgn1:sqs-java-scenario
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+488
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
338 changes: 338 additions & 0 deletions
338
javav2/example_code/sqs/src/main/java/com/example/sqs/SendRecvBatch.java
This file contains hidden or 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,338 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.example.sqs; | ||
|
||
// snippet-start:[sqs.java2.sendRecvBatch.main] | ||
// snippet-start:[sqs.java2.sendRecvBatch.import] | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.sqs.SqsClient; | ||
import software.amazon.awssdk.services.sqs.model.*; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
// snippet-end:[sqs.java2.sendRecvBatch.import] | ||
|
||
|
||
/** | ||
* Before running this Java V2 code example, set up your development | ||
* environment, including your credentials. | ||
* | ||
* For more information, see the following documentation topic: | ||
* | ||
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html | ||
*/ | ||
|
||
/** | ||
* This code demonstrates basic message operations in Amazon Simple Queue Service (Amazon SQS). | ||
*/ | ||
|
||
public class SendRecvBatch { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(SendRecvBatch.class); | ||
|
||
private static final SqsClient sqsClient = SqsClient.builder() | ||
.build(); | ||
|
||
// snippet-start:[sqs.java2.sendRecvBatch.sendBatch] | ||
/** | ||
* Send a batch of messages in a single request to an SQS queue. | ||
* This request may return overall success even when some messages were not sent. | ||
* The caller must inspect the Successful and Failed lists in the response and | ||
* resend any failed messages. | ||
* | ||
* @param queueUrl The URL of the queue to receive the messages. | ||
* @param messages The messages to send to the queue. Each message contains a body and attributes. | ||
* @return The response from SQS that contains the list of successful and failed messages. | ||
*/ | ||
public static SendMessageBatchResponse sendMessages( | ||
String queueUrl, List<MessageEntry> messages) { | ||
|
||
try { | ||
List<SendMessageBatchRequestEntry> entries = new ArrayList<>(); | ||
|
||
for (int i = 0; i < messages.size(); i++) { | ||
MessageEntry message = messages.get(i); | ||
entries.add(SendMessageBatchRequestEntry.builder() | ||
.id(String.valueOf(i)) | ||
.messageBody(message.getBody()) | ||
.messageAttributes(message.getAttributes()) | ||
.build()); | ||
} | ||
|
||
SendMessageBatchRequest sendBatchRequest = SendMessageBatchRequest.builder() | ||
.queueUrl(queueUrl) | ||
.entries(entries) | ||
.build(); | ||
|
||
SendMessageBatchResponse response = sqsClient.sendMessageBatch(sendBatchRequest); | ||
|
||
if (!response.successful().isEmpty()) { | ||
int successCount = 0; | ||
for (SendMessageBatchResultEntry resultEntry : response.successful()) { | ||
successCount++; | ||
} | ||
logger.info("Successfully sent " + successCount + " messages to queue: " + queueUrl); | ||
} | ||
|
||
if (!response.failed().isEmpty()) { | ||
for (BatchResultErrorEntry errorEntry : response.failed()) { | ||
logger.warn("Failed to send: " + errorEntry.id() + ": " + | ||
messages.get(Integer.parseInt(errorEntry.id())).getBody()); | ||
} | ||
} | ||
|
||
return response; | ||
|
||
} catch (SqsException e) { | ||
logger.error("Send messages failed to queue: " + queueUrl, e); | ||
throw e; | ||
} | ||
} | ||
// snippet-end:[sqs.java2.sendRecvBatch.sendBatch] | ||
|
||
// snippet-start:[sqs.java2.sendRecvBatch.recvBatch] | ||
/** | ||
* Receive a batch of messages in a single request from an SQS queue. | ||
* | ||
* @param queueUrl The URL of the queue from which to receive messages. | ||
* @param maxNumber The maximum number of messages to receive. The actual number | ||
* of messages received might be less. | ||
* @param waitTime The maximum time to wait (in seconds) before returning. When | ||
* this number is greater than zero, long polling is used. This | ||
* can result in reduced costs and fewer false empty responses. | ||
* @return The list of Message objects received. These each contain the body | ||
* of the message and metadata and custom attributes. | ||
*/ | ||
public static List<Message> receiveMessages(String queueUrl, int maxNumber, int waitTime) { | ||
try { | ||
ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder() | ||
.queueUrl(queueUrl) | ||
.maxNumberOfMessages(maxNumber) | ||
.waitTimeSeconds(waitTime) | ||
.messageAttributeNames("All") | ||
.build(); | ||
|
||
List<Message> messages = sqsClient.receiveMessage(receiveRequest).messages(); | ||
|
||
int receivedCount = 0; | ||
for (Message message : messages) { | ||
receivedCount++; | ||
} | ||
|
||
if (receivedCount > 0) { | ||
logger.info("Received " + receivedCount + " messages from queue: " + queueUrl); | ||
} | ||
return messages; | ||
|
||
} catch (SqsException e) { | ||
logger.error("Couldn't receive messages from queue: " + queueUrl, e); | ||
throw e; | ||
} | ||
} | ||
// snippet-end:[sqs.java2.sendRecvBatch.recvBatch] | ||
|
||
// snippet-start:[sqs.java2.sendRecvBatch.delBatch] | ||
/** | ||
* Delete a batch of messages from a queue in a single request. | ||
* | ||
* @param queueUrl The URL of the queue from which to delete the messages. | ||
* @param messages The list of messages to delete. | ||
* @return The response from SQS that contains the list of successful and failed | ||
* message deletions. | ||
*/ | ||
public static DeleteMessageBatchResponse deleteMessages(String queueUrl, List<Message> messages) { | ||
try { | ||
List<DeleteMessageBatchRequestEntry> entries = new ArrayList<>(); | ||
|
||
for (int i = 0; i < messages.size(); i++) { | ||
entries.add(DeleteMessageBatchRequestEntry.builder() | ||
.id(String.valueOf(i)) | ||
.receiptHandle(messages.get(i).receiptHandle()) | ||
.build()); | ||
} | ||
|
||
DeleteMessageBatchRequest deleteRequest = DeleteMessageBatchRequest.builder() | ||
.queueUrl(queueUrl) | ||
.entries(entries) | ||
.build(); | ||
|
||
DeleteMessageBatchResponse response = sqsClient.deleteMessageBatch(deleteRequest); | ||
|
||
if (!response.successful().isEmpty()) { | ||
int deletedCount = 0; | ||
for (DeleteMessageBatchResultEntry resultEntry : response.successful()) { | ||
deletedCount++; | ||
} | ||
logger.info("Successfully deleted " + deletedCount + " messages from queue: " + queueUrl); | ||
} | ||
|
||
if (!response.failed().isEmpty()) { | ||
for (BatchResultErrorEntry errorEntry : response.failed()) { | ||
logger.warn("Could not delete " + messages.get(Integer.parseInt(errorEntry.id())).receiptHandle()); | ||
} | ||
} | ||
|
||
return response; | ||
|
||
} catch (SqsException e) { | ||
logger.error("Couldn't delete messages from queue " + queueUrl, e); | ||
throw e; | ||
} | ||
} | ||
// snippet-end:[sqs.java2.sendRecvBatch.delBatch] | ||
|
||
// snippet-start:[sqs.java2.sendRecvBatch.scenario] | ||
/** | ||
* Helper class to represent a message with body and attributes. | ||
*/ | ||
public static class MessageEntry { | ||
private final String body; | ||
private final Map<String, MessageAttributeValue> attributes; | ||
|
||
public MessageEntry(String body, Map<String, MessageAttributeValue> attributes) { | ||
this.body = body; | ||
this.attributes = attributes != null ? attributes : new HashMap<>(); | ||
} | ||
|
||
public String getBody() { | ||
return body; | ||
} | ||
|
||
public Map<String, MessageAttributeValue> getAttributes() { | ||
return attributes; | ||
} | ||
} | ||
|
||
/** | ||
* Shows how to: | ||
* * Read the lines from this Java file and send the lines in | ||
* batches of 10 as messages to a queue. | ||
* * Receive the messages in batches until the queue is empty. | ||
* * Reassemble the lines of the file and verify they match the original file. | ||
*/ | ||
public static void usageDemo() { | ||
System.out.println("-".repeat(88)); | ||
System.out.println("Welcome to the Amazon Simple Queue Service (Amazon SQS) demo!"); | ||
System.out.println("-".repeat(88)); | ||
|
||
// Create a queue for the demo. | ||
String queueName = "sqs-usage-demo-message-wrapper-"+System.currentTimeMillis(); | ||
CreateQueueRequest createRequest = CreateQueueRequest.builder() | ||
.queueName(queueName) | ||
.build(); | ||
String queueUrl = sqsClient.createQueue(createRequest).queueUrl(); | ||
System.out.println("Created queue: " + queueUrl); | ||
|
||
try { | ||
// Read the lines from this Java file. | ||
Path projectRoot = Paths.get(System.getProperty("user.dir")); | ||
Path filePath = projectRoot.resolve("src/main/java/com/example/sqs/SendRecvBatch.java"); | ||
List<String> lines = Files.readAllLines(filePath); | ||
|
||
|
||
// Send file lines in batches. | ||
int batchSize = 10; | ||
System.out.println("Sending file lines in batches of " + batchSize + " as messages."); | ||
|
||
for (int i = 0; i < lines.size(); i += batchSize) { | ||
List<MessageEntry> messageBatch = new ArrayList<>(); | ||
|
||
for (int j = i; j < Math.min(i + batchSize, lines.size()); j++) { | ||
String line = lines.get(j); | ||
if (line == null || line.trim().isEmpty()) { | ||
continue; // Skip empty lines. | ||
} | ||
|
||
Map<String, MessageAttributeValue> attributes = new HashMap<>(); | ||
attributes.put("path", MessageAttributeValue.builder() | ||
.dataType("String") | ||
.stringValue(filePath.toString()) | ||
.build()); | ||
attributes.put("line", MessageAttributeValue.builder() | ||
.dataType("String") | ||
.stringValue(String.valueOf(j)) | ||
.build()); | ||
|
||
messageBatch.add(new MessageEntry(lines.get(j), attributes)); | ||
} | ||
|
||
sendMessages(queueUrl, messageBatch); | ||
System.out.print("."); | ||
System.out.flush(); | ||
} | ||
|
||
System.out.println("\nDone. Sent " + lines.size() + " messages."); | ||
|
||
// Receive and process messages. | ||
System.out.println("Receiving, handling, and deleting messages in batches of " + batchSize + "."); | ||
String[] receivedLines = new String[lines.size()]; | ||
boolean moreMessages = true; | ||
|
||
while (moreMessages) { | ||
List<Message> receivedMessages = receiveMessages(queueUrl, batchSize, 5); | ||
System.out.print("."); | ||
System.out.flush(); | ||
|
||
for (Message message : receivedMessages) { | ||
int lineNumber = Integer.parseInt(message.messageAttributes().get("line").stringValue()); | ||
receivedLines[lineNumber] = message.body(); | ||
} | ||
|
||
if (!receivedMessages.isEmpty()) { | ||
deleteMessages(queueUrl, receivedMessages); | ||
} else { | ||
moreMessages = false; | ||
} | ||
} | ||
|
||
System.out.println("\nDone."); | ||
|
||
// Verify all lines were received correctly. | ||
boolean allLinesMatch = true; | ||
for (int i = 0; i < lines.size(); i++) { | ||
String originalLine = lines.get(i); | ||
String receivedLine = receivedLines[i] == null ? "" : receivedLines[i]; | ||
|
||
if (!originalLine.equals(receivedLine)) { | ||
allLinesMatch = false; | ||
break; | ||
} | ||
} | ||
|
||
if (allLinesMatch) { | ||
System.out.println("Successfully reassembled all file lines!"); | ||
} else { | ||
System.out.println("Uh oh, some lines were missed!"); | ||
} | ||
|
||
} catch (IOException e) { | ||
logger.error("Error reading file", e); | ||
} finally { | ||
// Clean up by deleting the queue. | ||
DeleteQueueRequest deleteQueueRequest = DeleteQueueRequest.builder() | ||
.queueUrl(queueUrl) | ||
.build(); | ||
sqsClient.deleteQueue(deleteQueueRequest); | ||
System.out.println("Deleted queue: " + queueUrl); | ||
} | ||
|
||
System.out.println("Thanks for watching!"); | ||
System.out.println("-".repeat(88)); | ||
} | ||
|
||
public static void main(String[] args) { | ||
usageDemo(); | ||
} | ||
} | ||
// snippet-end:[sqs.java2.sendRecvBatch.scenario] | ||
// snippet-end:[sqs.java2.sendRecvBatch.main] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
user.dir
is not recommended because it's unreliable depending on where the process was started.Moreover, this failed for me:
Because:
projectRoot
variable resolved to:/Users/tkhill/IdeaProjects/aws-doc-sdk-examples
filePath
variable resolved to:/Users/tkhill/IdeaProjects/aws-doc-sdk-examples/src/main/java/com/example/sqs/SendRecvBatch.java
But should have resolved to:
/Users/tkhill/IdeaProjects/aws-doc-sdk-examples/javav2/example_code/sqs/src/main/java/com/example/sqs/SendRecvBatch.java
(Note the missingjavav2/example_code/sqs
path elements).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh got it. I was using the relative path. Instead, I should have used the absolute path of the file. Do I fix this by pushing a new commit?