[英]How can I set the spring-rabbitmq listener's rate of receive message
I am using spring-rabbitmq ,and I can get messages successfully. 我正在使用spring-rabbitmq,并且可以成功获取消息。 but when I debug I found that the listener creates a thread and it will ask for messages every 1 second .I think the rate is too high .what I want to do is to set the rate 1 min or any other.
但是当我调试时,我发现侦听器创建了一个线程,它将每1秒询问一次消息。我认为速率太高。我想将速率设置为1分钟或任何其他速率。 I search a lot but does not works
我搜索了很多但没有用
My springrabbit.xml: 我的springrabbit.xml:
<rabbit:listener-container connection-factory="connectionFactory" message-converter="jsonMessageConverter" >
<rabbit:listener queues="notification" ref="messageReceiver"/>
</rabbit:listener-container>
my java code : 我的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!");
}
}
}
Some of debug results are as follows: 一些调试结果如下:
[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
You can increase the receiveTimeout
- see Message Listener Container Configuration . 您可以增加
receiveTimeout
请参阅消息侦听器容器配置 。
However, the container will be less responsive to stop()
requests. 但是,容器将对
stop()
请求的响应变慢。
I think you are overly concerned about the polling rate - there is very little overhead to poll the internal queue used to stage delivered messages. 我认为您过于担心轮询速率-轮询用于暂存已传递邮件的内部队列的开销很少。
If it's just the log "noise" you want to remove (when debugging) set the org.springframework.amqp.rabbit.listener.BlockingQueueConsumer
log category to INFO
or WARN
. 如果只是要删除的日志(调试时),请将
org.springframework.amqp.rabbit.listener.BlockingQueueConsumer
日志类别设置为INFO
或WARN
。
The upcoming 2.0 release has a new DirectMessageListenerContainer
which does not poll an internal queue and eliminates this issue. 即将发布的2.0版本具有一个新的
DirectMessageListenerContainer
,它不会轮询内部队列并消除了此问题。 Info here . 信息在这里 。
EDIT 编辑
listener still ask rabbitmq for messages every 1s
侦听器仍然每1秒询问Rabbitmq消息
If you are still seeing the debug message every 1s, you have not configured the receiveTimeout
properly; 如果仍然每隔1s看到一次调试消息,则说明您没有正确配置
receiveTimeout
; receiveTimeout
,请执行receiveTimeout
。 IT IS NOT "asking rabbitmq" for a message, the thread is waking up after waiting for receiveTimeout
(and found that rabbit has not sent a new message) so it can react to a stop()
; 它不是“请求Rabbitmq”消息,线程在等待
receiveTimeout
之后唤醒(并发现receiveTimeout
没有发送新消息),因此它可以对stop()
做出反应; then it sleeps again until a new message arrives or it times out again. 然后它再次休眠,直到收到新消息或它再次超时。 There is no interaction with the broker if no messages are available - messages are pushed by the broker.
如果没有可用消息,则与代理不存在交互-消息由代理推送。
Perhaps you are misunderstanding the purpose of the listener container. 也许您误解了侦听器容器的目的。 It is for message-driven applications - you can't "slow down" the rate that messages arrive - they are pushed by the broker.
它用于消息驱动的应用程序-您不能“降低”消息到达的速度-它们是由代理推送的。
If you want to receive a message only once a minute, you should use a RabbitTemplate
receive()
(or receiveAndConvert()
) method instead of a message listener container. 如果您希望每分钟仅接收一次消息,则应使用
RabbitTemplate
receive()
(或receiveAndConvert()
)方法代替消息侦听器容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.