You can build Lambda functions that uses the Lambda runtime API. Then within that Lambda function use the SQS Java API. Look at retrieving the queue attributes using this method.
https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sqs/SqsClient.html#getQueueAttributes-software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest-
Look at the following attribute.
ApproximateNumberOfMessages - Returns the approximate number of visible messages in a queue
Here is a code example that uses the SQS Java V2 API to obtain number of messages.
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class GetQueueAttributes {
public static void main(String[] args) {
final String USAGE = "
" +
"Usage: AddQueueTags <queueName>
" +
"Where:
" +
" queueName - the name of the queue to which tags are applied.
";
if (args.length != 1) {
System.out.println(USAGE);
System.exit(1);
}
String queueName = args[0];
SqsClient sqsClient = SqsClient.builder()
.region(Region.US_WEST_2)
.build();
try {
GetQueueUrlResponse getQueueUrlResponse =
sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build());
String queueUrl = getQueueUrlResponse.queueUrl();
// Specify the attributes to retrieve.
List<QueueAttributeName> atts = new ArrayList();
atts.add(QueueAttributeName.APPROXIMATE_NUMBER_OF_MESSAGES);
GetQueueAttributesRequest attributesRequest= GetQueueAttributesRequest.builder()
.queueUrl(queueUrl)
.attributeNames(atts)
.build();
GetQueueAttributesResponse response = sqsClient.getQueueAttributes(attributesRequest);
Map<String,String> queueAtts = response.attributesAsStrings();
for (Map.Entry<String,String> queueAtt : queueAtts.entrySet())
System.out.println("Key = " + queueAtt.getKey() +
", Value = " + queueAtt.getValue());
} catch (SqsException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
}
What is nice is you can create different Lambda functions and then hook each Lambda function as a workflow step by using Step Functions. This is a good way to build workflows and have each step in that workflow use the AWS Java API to perform a specific AWS Service operation.
To learn how to hook in Lambda functions into a workflow, see this tutorial.
Create AWS serverless workflows by using the AWS SDK for Java
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…