简体   繁体   中英

Some attempts to connect to RabbitMQ in the same machine cause TimeoutException

I have two Java applications running on CentOS Linux which repeatedly publish messages to queues in a RabbitMQ instance installed in the same machine as them.

For each new message they connect to the queue, publish it, then disconnect.

Sometimes an attempt to establish a connection throws a TimeoutException :

br.com.projectname.ProcessStoppingException: java.util.concurrent.TimeoutException
    at br.com.projectname.EscritorDaFilaDoProcessadorDePacotes.escrever(EscritorDaFilaDoProcessadorDePacotes.java:55)
    (...)
    at br.com.projectname.ViaDeComunicacao$1.run(ViaDeComunicacao.java:39)
Caused by: java.util.concurrent.TimeoutException
    at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:77)
    at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:111)
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:37)
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:367)
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:293)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:678)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:722)

It is possible to reproduce the issue by performing a mass publication, but sometimes it also occurs when the applications are idle.

From what I've read increasing the 5-10 seconds default connection timeout is not a good idea because such an exception suggests a configuration or networking issue somewhere which I should investigate.

However, the RabbitMQ networking guide didn't offer much insight. Following the orientation in its last part, I'm using 127.0.0.1 as host so I suppose DNS lookups and reverse lookups aren't taking place. I have also increased the Erlang VM I/O Thread Pool (the only configuration I thought could be related to the issue) from the default 30 to 60 (ie 15 times the number of available cores which is 4) to no avail.

There is a third application consuming a queue in a rather sub-optimal way, by constantly polling it via Channel#basicGet() , but I suppose it is not contributing to the issue since it keeps happening even if that application is not running.

Any ideas? The timeout does seem to occur in less than 5 seconds so maybe I should try increasing it after all.

EDIT

The code is quite straightforward:

public void escrever(JSONObject json) throws ProcessStoppingException {
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);
        factory.setPort(porta);
        factory.setUsername(usuario);
        factory.setPassword(senha);
        factory.setVirtualHost(virtualHost);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(fila, true, false, false, null);
        channel.basicPublish(exchange, routingKey, MessageProperties.PERSISTENT_BASIC, json.toString().getBytes(StandardCharsets.UTF_8));
        channel.close();
        connection.close();
    } catch (IOException | TimeoutException e) {
        throw new ProcessStoppingException(e);
    }

Solved it by keeping a connection constantly open and only performing create channel, publish, and close channel for each message.

It did not diminish my suspicion over some unhandled configuration issue but at least it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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