我正在尝试从SQS队列中读取一堆消息(~2mil),但是我遇到了如何设置线程模型以获得不错的性能的麻烦。
我试过了:
ExecutorService es = Executors.newFixedThreadPool(16);
//getApproximateNumberOfMessages() - uses sqs regular (not async) client to call getQueueAttributes, etc
final AtomicInteger approximateNumberOfMessages = new AtomicInteger(getApproximateNumberOfMessages());
while(approximateNumberOfMessages.get() > 0) {
es.execute(new Runnable() {
@Override
public void run() {
//getMessages uses sqs regular (not async) client to call receiveMessage()
List<Message> messages = getMessages();
for (Message message : messages) {
handleSingleMessage(message);
}
approximateNumberOfMessages.decrementAndGet();
}
});
}
但是,我很快就会出现内存异常。我尝试按照建议here切换到ThreadPoolExecutor
,但是当我这样做时,过程似乎只是锁定。
有没有办法异步处理消息而不会耗尽内存?