I am working on an application that uses Micronaut and needs to consume message from a RabbitMQ queue. The matter is that the queue can provide a lot of messages so I would like to have a parameterized number of instance of the consumer.
For example the following code auto generate one instance of the consumer :
@RabbitListener
public class TestConsumer {
@Queue("TestQueue")
public void handleMessage(String message) {
// some treatment
}
}
Is there a way to tell micronaut to instantiate more than one instance ?
If I understand you correctly you want to configure how many threads consume messages from the queue. You can do this via an executor that you configure in the application.yml
See the following example from micronaut-rabbitmq docu.
@RabbitListener
public class ProductListener {
List<String> messageLengths = Collections.synchronizedList(new ArrayList<>());
@Queue(value = "product", executor = "product-listener")
public void receive(byte[] data) {
messageLengths.add(new String(data));
System.out.println("Java received " + data.length + " bytes from RabbitMQ");
}
}
With application.yml
micronaut:
executors:
product-listener:
type: fixed
nThreads: 25