使用Spring AMQP DSL并手动创建MessageListenerContainer
时,其吞吐量比使用@RabbitListener
方法慢很多。据我了解,使用@RabbitListener
时,容器是由BeanPostProcessor
创建的,我所做的应该是相同的,但由于某些原因,它不是。我在下面添加了这两种方法,以显示我的意思。
我的方法的吞吐量非常慢,使用以下配置时大约30 / s,而使用注释时,吞吐量非常快。我在做什么错了?
application.properties
spring.rabbitmq.listener.type=direct
spring.rabbitmq.listener.direct.default-requeue-rejected=false
spring.rabbitmq.listener.direct.consumers-per-queue=50
spring.rabbitmq.listener.direct.prefetch=10
DemoConfiguration.java
@Configuration
@EnableRabbit
public class DemoConfiguration {
@Bean
// manually creating the container, VERY slow
public DirectMessageListenerContainer container(final DirectRabbitListenerContainerFactory containerFactory) {
final DirectMessageListenerContainer listenerContainer = containerFactory.createListenerContainer();
listenerContainer.setQueueNames("in_queue"); // has 2.000 messages before starting the application
listenerContainer.setListenerId("listener_in_queue");
return listenerContainer;
}
@Bean
public IntegrationFlow demoFlow(final DirectMessageListenerContainer container) {
return IntegrationFlows.from(Amqp.inboundGateway(container))
// EXTREMELY slow
.nullChannel();
}
// this is working very fast, 1000 messages per second
/*@RabbitListener(queues = "in_queue")
public void consume() {
}*/
}
答案 0 :(得分:0)
编辑
问题是您正在使用入站网关,并且使用者线程正在等待答复(默认情况下为1秒),该答复永远不会到达。
请改用Amqp.inboundAdapter
(或将网关上的replyTimeout
设置为零。
PRE_EDIT
我不确定为什么,但是问题似乎出在Spring Integration中,而不是容器中:
@SpringBootApplication
public class So54365437Application {
private static final Logger logger = LoggerFactory.getLogger(So54365437Application.class);
public static void main(String[] args) {
SpringApplication.run(So54365437Application.class, args).close();
}
private final AtomicInteger lCount = new AtomicInteger();
private final AtomicInteger mCount = new AtomicInteger();
private final AtomicInteger iCount = new AtomicInteger();
private final AtomicLong t0 = new AtomicLong();
@RabbitListener(queues = "foo")
public void listener(Integer in) {
int n = lCount.incrementAndGet();
if (n % 100 == 0) {
logger.info("listener @" + n);
}
if (n == 2000) {
logger.info("listener done @" + rate());
}
}
@Bean
public DirectMessageListenerContainer container(final DirectRabbitListenerContainerFactory containerFactory) {
final DirectMessageListenerContainer listenerContainer = containerFactory.createListenerContainer();
listenerContainer.setQueueNames("bar");
listenerContainer.setAutoStartup(false);
listenerContainer.setMessageListener(m -> {
int n = mCount.incrementAndGet();
if (n % 100 == 0) {
logger.info("manual @" + n);
}
if (n == 2000) {
logger.info("manual done @" + rate());
}
});
return listenerContainer;
}
@Bean
public DirectMessageListenerContainer integrationContainer(final DirectRabbitListenerContainerFactory containerFactory) {
final DirectMessageListenerContainer listenerContainer = containerFactory.createListenerContainer();
listenerContainer.setQueueNames("baz");
listenerContainer.setAutoStartup(false);
return listenerContainer;
}
@Bean
public IntegrationFlow demoFlow(final DirectMessageListenerContainer integrationContainer) {
return IntegrationFlows.from(Amqp.inboundGateway(integrationContainer).autoStartup(false))
.handle(p -> {
int n = iCount.incrementAndGet();
if (n % 100 == 0) {
logger.info("integration @" + n);
}
if (n == 2000) {
logger.info("integration done @" + rate());
}
})
.get();
}
private String rate() {
return "" + 2000000.0 / ((System.currentTimeMillis() - t0.get())) + "/sec";
}
@Bean
public ApplicationRunner runner(RabbitTemplate template, RabbitListenerEndpointRegistry registry,
DirectMessageListenerContainer container, DirectMessageListenerContainer integrationContainer) {
return args -> {
IntStream.range(0, 2000)
.forEach(i -> {
switch(args.getSourceArgs()[1]) {
case "listener":
template.convertAndSend("foo", i);
break;
case "manual":
template.convertAndSend("bar", i);
break;
case "integration":
template.convertAndSend("baz", i);
break;
}
});
logger.info("All sent; starting container");
t0.set(System.currentTimeMillis());
switch(args.getSourceArgs()[1]) {
case "listener":
registry.start();
break;
case "manual":
container.start();
break;
case "integration":
integrationContainer.start();
break;
}
System.in.read();
};
}
}
和
listener done @10309.278350515464/sec
manual done @11111.111111111111/sec
integration done @15.578629236413487/sec