简体   繁体   English

使用 Spring Boot 在 RabbitMQ 上不断调用 MessageListener.onMessage

[英]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.

相关问题 Spring Boot - 使用 RabbitMQ 发送消息以连续监听消息队列 - Spring boot - Messaging with RabbitMQ to listen message queue continuously Spring Kafka消费者单元测试中未调用MessageListner的重写onMessage - Overridden onMessage of MessageListner not getting called in Spring Kafka Consumer Unit Test Spring boot rabbitmq消息没有被重新排队 - Spring boot rabbitmq message not getting requeued spring 启动未连接到 AWS 中的 RabbitMQ - spring boot not getting connected to RabbitMQ in AWS RabbitMQ 未在 Spring Boot 中自动配置 - RabbitMQ is not getting auto-configured in Spring Boot 在messageListener onMessage内访问connectionfactory - Access connectionfactory inside messageListener onMessage 为 JMS 创建 JUnit 测试以测试是否在 jms.convertAndSend() 之后调用 MessageListener onMessage() 方法 - Creating JUnit Test for JMS to test if MessageListener onMessage() method was called after jms.convertAndSend() Spring Boot Cloud + RabbitMQ - Spring Boot Cloud + RabbitMQ Spring Boot:BeanFactoryPostProcessor的postProcessBeanFactory没有被调用 - Spring Boot : BeanFactoryPostProcessor's postProcessBeanFactory not getting called 骆驼-RabbitMQ春季靴子 - Camel - RabbitMQ spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM