Spring AMQP manual container is unusually slow

Goran Malić :

When using Spring AMQP DSL and manually creating a MessageListenerContainer, its throughput is A LOT slower than using @RabbitListener approach. From my understanding, when using @RabbitListener, a container is created by a BeanPostProcessor and what I am doing should be the same, but for some reason is not. I have added both of these approaches below to show what I mean.

Throughput of my approach is VERY slow, around 30/s when using the configuration below, while using the annotation, throughput is extremely fast. What am I doing wrong?

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() {

    }*/
}
Gary Russell :

EDIT

The problem is you are using an inbound gateway and the consumer thread is awaiting a reply (1 second by default), which will never arrive.

Use Amqp.inboundAdapter instead (or set the replyTimeout on the gateway to zero.

PRE_EDIT

I am not sure why, yet, but the problem seems to be in Spring Integration, not the container:

@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();
        };
    }

}

and

listener done @10309.278350515464/sec

manual done @11111.111111111111/sec

integration done @15.578629236413487/sec

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161291&siteId=1