如何设置spring-rabbitmq监听器的接收消息速率

时间:2017-08-16 10:25:44

标签: java rabbitmq spring-rabbitmq

我正在使用spring-rabbitmq,我可以成功获取消息。 但是当我调试时,我发现侦听器创建了一个线程,它会问 对于每1秒的消息。我认为速率太高了。我想做的是将速率设置为1分钟或任何其他速率。 我搜索了很多但是没有用

我的springrabbit.xml:

<rabbit:listener-container connection-factory="connectionFactory"  message-converter="jsonMessageConverter" >
        <rabbit:listener queues="notification" ref="messageReceiver"/>
    </rabbit:listener-container>

我的java代码:

@Override
        public void onMessage(Message message)  { System.out.println("messagebody:   "+new String(message.getBody()));
            LOGGER.info(dateFormatUtil.getDateFormat(new Date())+new String(message.getBody()));
            boolean result=false;
            SendSingleEmailService sendSingleEmailService = new SendSingleEmailService();
            try {
                result =sendSingleEmailService.send(new String(message.getBody()));
            } catch (FileNotFoundException e) {
                LOGGER.error(dateFormatUtil.getDateFormat(new Date())+"[NOTIFICATION] [ERROR] message is null!");
                e.printStackTrace();
            }
            if(!result) {
                try{
                    throw new Exception();
                }catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }catch (Exception e) {
                    throw new RuntimeException(e);
                }finally {
                    LOGGER.error(dateFormatUtil.getDateFormat(new Date())+"[NOTIFICATION] [ERROR] Send Email failed!");
                }
            }


        }

部分调试结果如下:

[2017-08-16 18:23:08,595]DEBUG  4286[SimpleAsyncTaskExecutor-1] - org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.nextMessage(BlockingQueueConsumer.java:186) - Retrieving delivery for Consumer: tag=[amq.ctag-5AR22lnMjmLAj329LDpGbQ], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.203.151:5672/,1), acknowledgeMode=AUTO local queue size=0
[2017-08-16 18:23:09,600]DEBUG  5291[SimpleAsyncTaskExecutor-1] - org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.nextMessage(BlockingQueueConsumer.java:186) - Retrieving delivery for Consumer: tag=[amq.ctag-5AR22lnMjmLAj329LDpGbQ], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.203.151:5672/,1), acknowledgeMode=AUTO local queue size=0
[2017-08-16 18:23:10,602]DEBUG  6293[SimpleAsyncTaskExecutor-1] - org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.nextMessage(BlockingQueueConsumer.java:186) - Retrieving delivery for Consumer: tag=[amq.ctag-5AR22lnMjmLAj329LDpGbQ], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.203.151:5672/,1), acknowledgeMode=AUTO local queue size=0
[2017-08-16 18:23:11,603]DEBUG  7294[SimpleAsyncTaskExecutor-1] - org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.nextMessage(BlockingQueueConsumer.java:186) - Retrieving delivery for Consumer: tag=[amq.ctag-5AR22lnMjmLAj329LDpGbQ], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.203.151:5672/,1), acknowledgeMode=AUTO local queue size=0
[2017-08-16 18:23:12,609]DEBUG  8300[SimpleAsyncTaskExecutor-1] - org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.nextMessage(BlockingQueueConsumer.java:186) - Retrieving delivery for Consumer: tag=[amq.ctag-5AR22lnMjmLAj329LDpGbQ], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.203.151:5672/,1), acknowledgeMode=AUTO local queue size=0
[2017-08-16 18:23:13,612]DEBUG  9303[SimpleAsyncTaskExecutor-1] - org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.nextMessage(BlockingQueueConsumer.java:186) - Retrieving delivery for Consumer: tag=[amq.ctag-5AR22lnMjmLAj329LDpGbQ], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.203.151:5672/,1), acknowledgeMode=AUTO local queue size=0
[2017-08-16 18:23:14,615]DEBUG 10306[SimpleAsyncTaskExecutor-1] - org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.nextMessage(BlockingQueueConsumer.java:186) - Retrieving delivery for Consumer: tag=[amq.ctag-5AR22lnMjmLAj329LDpGbQ], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.203.151:5672/,1), acknowledgeMode=AUTO local queue size=0
[2017-08-16 18:23:15,617]DEBUG 11308[SimpleAsyncTaskExecutor-1] - org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.nextMessage(BlockingQueueConsumer.java:186) - Retrieving delivery for Consumer: tag=[amq.ctag-5AR22lnMjmLAj329LDpGbQ], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.203.151:5672/,1), acknowledgeMode=AUTO local queue size=0
[2017-08-16 18:23:16,618]DEBUG 12309[SimpleAsyncTaskExecutor-1] - org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.nextMessage(BlockingQueueConsumer.java:186) - Retrieving delivery for Consumer: tag=[amq.ctag-5AR22lnMjmLAj329LDpGbQ], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.203.151:5672/,1), acknowledgeMode=AUTO local queue size=0
[2017-08-16 18:23:17,619]DEBUG 13310[SimpleAsyncTaskExecutor-1] - org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.nextMessage(BlockingQueueConsumer.java:186) - Retrieving delivery for Consumer: tag=[amq.ctag-5AR22lnMjmLAj329LDpGbQ], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.203.151:5672/,1), acknowledgeMode=AUTO local queue size=0

1 个答案:

答案 0 :(得分:0)

您可以增加receiveTimeout - 请参阅Message Listener Container Configuration

但是,容器对stop()请求的响应速度较慢。

我认为你过分关注轮询率 - 轮询用于分阶段传递消息的内部队列的开销非常小。

如果只是日志&#34;噪音&#34;您要删除(在调试时)将org.springframework.amqp.rabbit.listener.BlockingQueueConsumer日志类别设置为INFOWARN

即将发布的2.0版本有一个新的DirectMessageListenerContainer,它不会轮询内部队列并消除此问题。 Info here

修改

  

监听器仍然每隔1s向rabbitmq询问消息

如果您仍然每1秒看到调试消息,则表示您尚未正确配置receiveTimeout;它不是&#34;问rabbitmq&#34;对于消息,线程在等待receiveTimeout之后醒来(并且发现兔子没有发送新消息),因此它可以对stop()作出反应;然后它会再次睡眠,直到新消息到达或再次超时。如果没有可用消息,则不与代理交互 - 消息由代理推送。

也许您误解了侦听器容器的用途。它适用于消息驱动的应用程序 - 您不能放慢速度&#34;消息到达的速率 - 它们由经纪人推送。

如果您希望每分钟只收到一封邮件,则应使用RabbitTemplate receive()(或receiveAndConvert())方法而不是邮件侦听器容器。