[英]MessageListener.onMessage is getting called continuously on RabbitMQ with Spring Boot
I have MessageListener.onMessage with a thread sleep.我有 MessageListener.onMessage 线程睡眠。 I'm simulating actual processing time the onMessage method will take by the above mentioned Thread sleep.我正在模拟上面提到的线程睡眠 onMessage 方法将花费的实际处理时间。 However what I have noticed is that it is getting called multiple times consecutively for the remaining messages till they get processed by the onMessage method.但是,我注意到的是,对于剩余的消息,它会被连续多次调用,直到它们被 onMessage 方法处理为止。 I see this as an inefficiency.我认为这是一种低效率。
Actual message count in to queue : 1000队列中的实际消息数:1000
Output of running number for hits命中数的输出
onMessage<<15656
onMessage<<15657
onMessage<<15658
onMessage<<15659
onMessage<<15660
onMessage<<15661
onMessage<<15662
onMessage<<15663
Code block代码块
@Service
class ThreadPooledMessageListener implements MessageListener {
@Autowired
TaskExecutor threadPoolTaskExecutor;
AtomicInteger processedCount = new AtomicInteger();
@Override
public void onMessage(Message message) {
System.out.println("onMessage<<" + processedCount.incrementAndGet());
threadPoolTaskExecutor.execute(new MessageProcessor(message));
}
}
class MessageProcessor implements Runnable {
Message processingMessage;
public MessageProcessor(Message message) {
this.processingMessage = message;
}
@Override
public void run() {
System.out.println("================================"+ Thread.currentThread().getName());
System.out.println(processingMessage);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("================================");
}
}
What are the possible fixes for this.对此有哪些可能的解决方法。
As @Gary Russell has pointed out;正如@Gary Russell 指出的那样; Issue was that I have used non-spring managed container SimpleMessageListenerContainer in my code.问题是我在我的代码中使用了非 Spring 管理的容器 SimpleMessageListenerContainer。 Fixed it with spring managed bean and defined concurrency there.使用 spring 管理的 bean 修复它并在那里定义并发。 Works as expected.按预期工作。 Fixed code segment固定代码段
@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
container.setQueues(queue);
container.setMessageListener(threadPooledMessageListener);
container.setConcurrentConsumers(4);
container.start();
return container;
}
>I see this as an inefficiency.
It's not clear what you mean.不清楚你的意思。 Since you are handing off the processing of a message to another thread, the listener exits immediately and, of course, the next message is delivered.由于您将消息处理移交给另一个线程,因此侦听器立即退出,当然,下一条消息将被传递。
This will risk message loss in the event of a failure.如果发生故障,这将带来消息丢失的风险。
If you are trying to achieve concurrency;如果您正在尝试实现并发; it's better to set the container concurrentConsumers
property and not do your own thread management in the listener.最好设置容器concurrentConsumers
属性,而不是在侦听器中进行自己的线程管理。 The container will manage the consumers for you.容器将为您管理消费者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.