Testing SQS, Lambda integration locally with large number of messages

huangapple go评论67阅读模式
英文:

Testing SQS,Lambda integration locally with large number of messages

问题

以下是翻译好的内容:

什么是使用Java模拟在AWS SQS中大约有100条消息的最佳方法。
我的目标是在队列中大约有100条消息时测试Lambda和DLQ功能,以查看限流和故障处理是如何工作的。

英文:

What is the best way to simulate having around 100 messages in AWS SQS using Java.
My goal is to test lambda and DLQ functionality when there are around 100 messages in my Queue,to see how throttling and failures work.

答案1

得分: 1

写一个接口暴露获取消息并正确处理消息所需的方法并编写使用该接口消耗消息的代码

interface SqsService {
    List<Message> getMessages();
    void deleteMessage(Message message);
}

然后创建一个使用AWS SDK的实现在正常情况下您将使用这个实现

class SqsMessageService implements MessageService {

    private String queueUrl;

    @Override
    List<Message> getMessages() {
        return sqs.receiveMessage(queueUrl).getMessages();
    }

    @Override
    void deleteMessage(Message message) {
        sqs.deleteMessage(queueUrl, message.getReceiptHandle());
    }
}

然后编写另一个实现它不与AWS通信而是返回一些消息

class FakeMessageService implements MessageService {
    
    @Override
    List<Message> getMessages() {
        final List<Message> messages = new ArrayList<>();
        ...生成100条消息的代码...
        return messages;
    }

    @Override
    void deleteMessage(Message message) {
        return;
    }
}

这样您可以在不改变大量代码的情况下轻松切换实现我假设您实际上不想发送100条或更多的SQS消息因为那是一个显而易见的答案但您可能需要为此付费如果您想要对应用程序的其他部分以及其与AWS的集成进行基准测试那么实际上并不需要发送大量的消息
英文:

Write an interface exposing methods needed to get the messages and properly handle them and write the code that needs to consume messages using that interface.

interface SqsService {
    List&lt;Message&gt; getMessages();
    void deleteMessage(Message message);
}

Then create one implementation using the AWS SDK. You will use that under normal conditions.

class SqsMessageService implements MessageService {

    private String queueUrl;

    @Override
    List&lt;Message&gt; getMessages() {
        return sqs.receiveMessage(queueUrl).getMessages();
    }

    @Override
    void deleteMessage(Message message) {
        sqs.deleteMessage(queueUrl, message.getReceiptHandle());
    }
}

Then write another implementation that doesn't talk to AWS but instead returns a number of messages.

class FakeMessageService implements MessageService {
    
    @Override
    List&lt;Message&gt; getMessages() {
        final List&lt;Message&gt; messages = new ArrayList&lt;&gt;();
        ...code to generate 100 messages...
        return messages;
    }

    @Override
    void deleteMessage(Message message) {
        return;
    }
}

This way you can easily switch between your implementations without changing a lot of code. Also I assume you don't want to actually send 100 or more SQS messages since that is an obvious answer but you would have to (probably) pay for that. And you don't really need it if you want to benchmark the other part of your app and how it integrates with AWS.

huangapple
  • 本文由 发表于 2020年9月21日 11:57:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63986014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定