Description
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...