question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Settable connection timeout in RabbitAutoConfiguration

See original GitHub issue

What I need: I need to set the connection timeout of com.rabbitmq.client.ConnectionFactory Because I am running rabbit mq in a cluster, and the rabbit mq client has to opertunity to allow more than one host. I can with spring-boot configure more than one host, no problem. According to the documentation “connectionTimeout connection establishment timeout in milliseconds; zero for infinite” But unfortunately:

    /** The default connection timeout;
     *  zero means wait indefinitely */
    public static final int    DEFAULT_CONNECTION_TIMEOUT = 0;

By saying that: if host one, in my addresses config is not reachable it will wait indefinitely.

The com.rabbitmq.client.ConnectionFactory has the option to give it a timeout but spring-boot dosen’t uses it yet.

The only solution I know was by copy pasting the code from RabbitAutoConfiguration.RabbitConnectionFactoryCreator and initilize CachingConnectionFactory with my own com.rabbitmq.client.ConnectionFactory.

Example:

    @Bean
    public CachingConnectionFactory populateCachingConnectionFactory(final RabbitProperties config) {
        com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = new com.rabbitmq.client.ConnectionFactory();
//        soem timeout property here
        rabbitConnectionFactory.setConnectionTimeout(50);
        CachingConnectionFactory factory = new CachingConnectionFactory(rabbitConnectionFactory);
        String addresses = config.getAddresses();
        factory.setAddresses(addresses);
        if (config.getHost() != null) {
            factory.setHost(config.getHost());
            factory.setPort(config.getPort());
        }
        if (config.getUsername() != null) {
            factory.setUsername(config.getUsername());
        }
        if (config.getPassword() != null) {
            factory.setPassword(config.getPassword());
        }
        if (config.getVirtualHost() != null) {
            factory.setVirtualHost(config.getVirtualHost());
        }
        return factory;
    }

Instead of providing another property, could com.rabbitmq.client.ConnectionFactory just be another bean which is then configurable via InitializingBean.

Maybe there are more properties which I don’t know of yet…

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
garyrussellcommented, Apr 21, 2014

When I saw this issue, I took a look and we do expose the most common attributes on the CCF (we recently added requestedHeartbeat after several users requested it).

We do document that, for more advanced configuration, you can declare a rabbit ConnectionFactory and provide a reference to it to the CCF.

I opened a JIRA Issue; vote it up.

In the meantime, your work around should suffice.

I suggest we close this for now, and we can reopen it when Spring-AMQP supports it.

0reactions
philwebbcommented, Apr 25, 2016

@wjam Gary has kindly provided a pull-request, this should be fixed when #5791 is merged.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix Rabbitmq connection timeout in springboot - Stack Overflow
My spring boot application throws a connection timeout error, and it is never able to connect. Here is the code below that I...
Read more >
Spring AMQP
To cache connections, set the cacheMode to CacheMode. ... The RabbitTemplate now provides waitForConfirms(long timeout) and waitForConfirmsOrDie(long ...
Read more >
Messaging with RabbitMQ in Spring Boot Application
There are two common ways to set up RabbitMQ on your local machine. ... rabbitmq.reply.timeout: Timeout is enforced on consumer delivery ...
Read more >
Spring | The Art of Code
You can address this either by increasing the session timeout or by reducing the maximum size of batches returned in poll() with max.poll.records....
Read more >
Detecting Dead TCP Connections with Heartbeats ... - RabbitMQ
To configure the heartbeat timeout in the Java client, set it with ConnectionFactory#setRequestedHeartbeat before creating a connection: ConnectionFactory cf = ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found