Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

amazon web services - Best way to make Step Functions wait for the SQS to be empty

What would be the best way to make a step function workflow wait for an sqs topic to be empty and then continue the workflow. Is there a way to check a topics length so i can react to in within a step function workflow? And would this really be the proper way to tackle this problem ? Or would i just create a lambda that would do exactly that and trigger it in my step function workflow

Thanks in advance :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...