英文:
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<Message> 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<Message> 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<Message> getMessages() {
final List<Message> messages = new ArrayList<>();
...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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论