Search code examples
javaspringrabbitmqspring-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. So I want to ask, is it correct to use TP inside rabbit consumer? Is it possible to achieve the same functionality using spring rabbit tools?

...
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
    }

Solution

  • It is generally better to simply increase the concurrentConsumers in the listener container than to hand off to your own thread-pool.

    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.