简体   繁体   English

消费者中的Java Spring Rabbit线程池

[英]java spring rabbit thread pool in consumer

I have a rabbit consumer, and inside I have a thread pool. 我有一个兔子消费者,并且内部有一个线程池。 I decided to have a thread pool because I need to wait for calculations to complete. 我决定拥有一个线程池,因为我需要等待计算完成。 But as I noticed, TP usage causes weird effects like freezing and so on. 但是,正如我注意到的那样,TP的使用会产生诸如冻结之类的怪异效果。 So I want to ask, is it correct to use TP inside rabbit consumer? 所以我想问,兔子用户内部使用TP是否正确? Is it possible to achieve the same functionality using spring rabbit tools? 使用Spring Rabbit工具能否实现相同的功能?

...
ThreadPoolExecutor pool = new ThreadPoolExecutor(cores, 50, 30L,  TimeUnit.SECONDS, new ArrayBlockingQueue<>(3000));

public void onMessage(){

   pool.execute(()->{
     //do something
     handleMessage(...);//return to some output queue
   });

}

or 要么

    public void onMessage(){
         //do something
         handleMessage(...);//return to some output queue
    }

It is generally better to simply increase the concurrentConsumers in the listener container than to hand off to your own thread-pool. 通常,最好只在侦听器容器中增加concurrentConsumers ,而不要移交给自己的线程池。

Your code needs to be thread-safe either way. 无论哪种方式,您的代码都必须是线程安全的。

With your current solution, you risk message loss since the message is acknowledged when the listener exits. 使用当前的解决方案,您会冒着丢失消息的风险,因为在侦听器退出时会确认消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM