我的应用程序使用SendMessageBatchRequest向AWS-SQS发送每条请求10条消息。每条消息的大小小于250个字节。该申请预计每天发布约一百万条记录。但是,发布消息的速度非常缓慢。
AmazonSQS sqs = null;
sqs = new AmazonSQSClient(new BasicAWSCredentials("accessKey", "SecretKey"));
//the below entries are added via a for loop picking the messages from a queue
//the typical queue size will be 20k messages
//AmazonSQSClient is created once and is used until the code comes out of the for loop
List<SendMessageBatchRequestEntry> entries = new ArrayList<SendMessageBatchRequestEntry>();
entries.add(new SendMessageBatchRequestEntry(messageBatchRequestId, "250kbJSONString"));
//Once the batch count reaches 10,the following code is called
CreateQueueRequest createQueueRequest = new CreateQueueRequest("CLIENT-TEST");
String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();
SendMessageBatchRequest sendMessageBatchRequest = new SendMessageBatchRequest(myQueueUrl);
sendMessageBatchRequest.setEntries(entries);
long timeBeforePost = System.currentTimeMillis();
SendMessageBatchResult smbResult = sqs.sendMessageBatch(sendMessageBatchRequest);
long afterPost = System.currentTimeMillis();
log.trace("time taken %s ",(afterPost-timeBeforePost));
捕获每个帖子所用的时间(以毫秒为单位),这是典型的输出
time taken 57
time taken 37
time taken 31
time taken 322
time taken 48
time taken 153
time taken 278
time taken 86
time taken 35
time taken 28
time taken 30
time taken 57
time taken 43
time taken 314
time taken 5587
time taken 42
time taken 4789
time taken 4831
time taken 66
time taken 6239
time taken 50
time taken 38
time taken 4833
time taken 40
time taken 4896
time taken 27
time taken 16634
这是正常的有一些帖子需要7,16等秒返回,我有一些300秒的帖子也从方法调用返回。我在这里遗漏了一些东西,我的应用程序目前是单线程的,我是计划使其多线程以并行处理消息但是一些消息需要几秒钟而一些消息需要花费几十/几百秒才是严重错误的。请帮忙。
感谢Ton
答案 0 :(得分:3)
我的第一个猜测就是你的本地机器和SQS之间的延迟问题,SQS当然可以比你看到的数字快得多地处理收到的消息。
也许最好的办法是将EC2实例旋转几个小时,大小与本地机器相似,并进行相同的测试以查看问题是否消失。
答案 1 :(得分:0)
请考虑创建一个AmazonSQS客户端,并将其重新用于每个请求。客户端提供连接池并保持连接打开以优化重用。它还可能正在查找区域以找到要连接的端点。最好在整个应用程序中重用AWS客户端,而不是在每个请求中重用。
SDK中的服务客户端是线程安全的,为了获得最佳性能,您应该将它们视为长期对象。每个客户端都有自己的连接池资源,该客户端在进行垃圾回收时会关闭。
https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/creating-clients.html