2
votes

I'm using Java RabbitMQ Client. I publish a message (basicPublish) and then I close channel. In Consumer, channel.basicAck throw exception:

com.rabbitmq.client.AlreadyClosedException: connection is already closed due to connection error; cause: java.util.concurrent.RejectedExecutionException: Task com.rabbitmq.client.impl.ConsumerWorkService$WorkPoolRunnable@665de7c7 rejected from java.util.concurrent.ThreadPoolExecutor@35f53993[Running, pool size = 5, active threads = 5, queued tasks = 0, completed tasks = 5]

If delete channel.close(), the error doesn't reproduced. Why connection closed, when I close the channel?

Sent messages to exchange:

Channel channel = connection.createChannel();
Set<String> expectedMessages = new HashSet<>(MESSAGES_COUNT);
for (int i = 0; i < MESSAGES_COUNT; i++) {
    String message = Integer.toString(i);
    channel.basicPublish(
        TEST_EXCHANGE,
        ROUTE_KEY,
        TEXT_PLAIN,
        message.getBytes("UTF-8")
    );
    expectedMessages.add(message);
}
channel.close();

Consumer:

try {
    channel.basicAck(deliveryTag, false);
} catch (Exception e) {
    log.error("Error during message handling: " + consumerTag, e);
    channel.basicNack(deliveryTag, false, true);
}
1
When you close channel you don't close the connection, you maybe have another error in your code.Gabriele Santomaggio
What could be the reason for such behavior? Can I not close the channel?Dmitriy
whitout code it is hard to know!Gabriele Santomaggio

1 Answers

0
votes

The problem was in the wrong connectionFactory settings. Incorrect:

    executorService = new ThreadPoolExecutor(
            properties.minThreads, properties.maxThreads,
            properties.maxThreadIdle, TimeUnit.SECONDS,
            new SynchronousQueue<>()
    );
    connectionFactory.setSharedExecutor(executorService);

For correct work:

    executorService = new ThreadPoolExecutor(
            properties.minThreads, properties.maxThreads,
            properties.maxThreadIdle, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>()
    );
    connectionFactory.setSharedExecutor(executorService);