I’m trying to run a pretty basic application utilizing RabbitMQ in Java. I’d simple like to consume messages concurrently using a Java ExecutorService. My project is using Spring, so I’ve defined my ThreadPoolExecutorFactoryBean like so:
<bean class="org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean"
destroy-method="destroy">
<property name="corePoolSize" value="8"/>
<property name="keepAliveSeconds" value="600"/>
<property name="maxPoolSize" value="16"/>
<property name="threadGroupName" value="CallbackQueue-Group"/>
<property name="threadNamePrefix" value="CallbackQueue-Worker-"/>
</bean>
I’m injecting this bean into my main mesasge queue utilizing class which is doing something like this:
this.connection = getConnectionFactory().newConnection(getQueueExecutor());
this.channel = this.connection.createChannel();
this.channel.queueDeclare(getQueueName(), true, false, false, null);
this.channel.basicConsume(getQueueName(), false, new DefaultConsumer(this.channel) {
@Override public void handleDelivery(String consumerTag, Envelope envelope,
BasicProperties properties, byte[] body) throws IOException {
logger.debug("Received message {}", properties.getCorrelationId());
try { Thread.sleep(3000); } catch (InterruptedException e) {};
getChannel().basicAck(envelope.getDeliveryTag(), false);
}
});
Simply put, when I post multiple messages to the queue, I should see the log statements happen fairly close together, even though the tasks take a while to execute. However, I’m seeing that my consumer is only processing one task at a time, in spite of the ExecutorService! What’s even weirder is that I’m actually seeing different threads in the pool service the queue, albeit never at the same time:
12:43:40.650 [CallbackQueue-Worker-2] DEBUG MyApplication - Received message 65bfbba29b4965eb0674c082c73dad7c
12:43:43.737 [CallbackQueue-Worker-3] DEBUG MyApplication - Received message 2a0b29012b13857c5a0ae8060f66dbaa
12:43:46.755 [CallbackQueue-Worker-3] DEBUG MyApplication - Received message 3c0742f9a284ac9c6b602200254c70db
12:43:49.769 [CallbackQueue-Worker-3] DEBUG MyApplication - Received message a462236fab19d51ba4bfea1582410a64
12:43:52.783 [CallbackQueue-Worker-3] DEBUG MyApplication - Received message 1a4713e1066dfc9e4ec1302098450a1f
What am I doing wrong here? Is there some additional configuration which I missed in either my ThreadPoolExecutorFactoryBean or in my RabbitMQ code?
From description of com.rabbitmq.client.Channel:
Could this be a reason? Your log shows that different workers are used (we see 2 and 3), but only one worker a time.