问题的根本原因是该sdk使用的是阻塞式的I/O操作,而Node.js的事件循环机制是基于非阻塞式I/O操作的。因此,在使用该sdk进行异步调用时会阻塞事件循环,导致应用程序的性能下降。
解决方法是使用AWS SDK for Node.js v3代替AWS SDK for Java。该版本的SDK支持异步调用,可以避免阻塞事件循环,提高应用程序的性能。示例代码如下:
const { SQSClient, SendMessageCommand } = require("@aws-sdk/client-sqs"); const client = new SQSClient({ region: "us-west-2" });
async function sendMessage() { const command = new SendMessageCommand({ QueueUrl: "https://sqs.us-west-2.amazonaws.com/123456789012/queue-name", MessageBody: "Hello world", });
try { const response = await client.send(command); console.log("Message sent:", response.MessageId); } catch (err) { console.error(err); } }
sendMessage();