spring-integration-aws
的SqsMessageDrivenChannelAdapter遇到了一些非常糟糕的吞吐量问题,这是围绕spring-cloud-aws
的SimpleMessageListenerContainer
的Spring Integration抽象。
问题似乎是,SimpleMessageListenerContainer
一次只能请求10条消息(AWS限制),并且使这些请求的速度非常慢,尤其是我观察到大约15 tps,这是太慢了。
这是我的代码:
import com.amazonaws.services.sqs.AmazonSQSAsync;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.aws.messaging.listener.SqsMessageDeletionPolicy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.aws.inbound.SqsMessageDrivenChannelAdapter;
import org.springframework.integration.channel.ExecutorChannel;
import org.springframework.messaging.MessageChannel;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
public class SqsConfiguration {
// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html#API_ReceiveMessage_RequestParameters
private static final int SQS_RECEIVE_MAX_NUM_MESSAGES = 10;
@Autowired
private AmazonSQSAsync amazonSQSAsync;
@Bean
ThreadPoolTaskExecutor sqsThreadPoolTaskExecutor() {
final ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
return threadPoolTaskExecutor;
}
@Bean
MessageChannel receivedSqsMessageChannel() {
return new ExecutorChannel(sqsThreadPoolTaskExecutor());
}
@Bean
SqsMessageDrivenChannelAdapter sqsMessageDrivenChannelAdapter() {
final SqsMessageDrivenChannelAdapter sqsMessageDrivenChannelAdapter = new SqsMessageDrivenChannelAdapter(amazonSQSAsync, "...");
sqsMessageDrivenChannelAdapter.setMessageDeletionPolicy(SqsMessageDeletionPolicy.NEVER);
sqsMessageDrivenChannelAdapter.setOutputChannel(receivedSqsMessageChannel());
sqsMessageDrivenChannelAdapter.setMaxNumberOfMessages(SQS_RECEIVE_MAX_NUM_MESSAGES);
return sqsMessageDrivenChannelAdapter;
}
}
receivedSqsMessageChannel
是流的输入,最终以消息的Acknowledgement#acknowledge()
被调用结束。
鉴于SimpleMessageListenerContainer
(因此还有SqsMessageDrivenChannelAdapter
)尚无任何功能可以轮询多个线程(spring-cloud-aws
有discussions,但距离正在实施,然后让spring-integration-aws
对其提供支持),而我现在拥有的TPS是不可接受的,我认为目前最好的“解决方案”是进行多个SqsMessageDrivenChannelAdapter
轮询同时并输出到receivedSqsMessageChannel
。
我如何在春季实现这一目标?有没有一种方法可以让使用Spring注释的SqsMessageDrivenChannelAdapter
池同时运行?