Settable connection timeout in RabbitAutoConfiguration
See original GitHub issueWhat 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:
- Created 9 years ago
- Comments:5 (4 by maintainers)
When I saw this issue, I took a look and we do expose the most common attributes on the
CCF
(we recently addedrequestedHeartbeat
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 theCCF
.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.
@wjam Gary has kindly provided a pull-request, this should be fixed when #5791 is merged.