Are there any public utility methods available in Spring AMQP/Rabbit to create a custom Rabbit Connection Factory. As of now, I am using the following code
@Bean(name = "rp1")
@ConfigurationProperties(prefix = "app.custom")
public RabbitProperties rp1() {
return new RabbitProperties();
}
public ConnectionFactory cf1(
@Qualifier("rp1") RabbitProperties rp1) {
final CachingConnectionFactory cf1 = new CachingConnectionFactory();
connectionFactory.setHost(rp1.getHost());
//remaing code left off
}
@Bean(name = "rp2")
@ConfigurationProperties(prefix = "app.custom1")
public RabbitProperties rp2() {
return new RabbitProperties();
}
public ConnectionFactory cf2(
@Qualifier("rp2") RabbitProperties rp2) {
final CachingConnectionFactory cf2 = new CachingConnectionFactory();
connectionFactory.setHost(rp2.getHost());
//remaing code left off
}
In application.properties, the following values are passed
Sample properties
Is there a better way to do this like, passing a custom RabbitProperties and getting a Connection Factory back by setting all the properties? Basically something similar to rabbitConnectionFactory bean in RabbitAutoConfiguration RabbitConnectionFactoryCreator class. I need st support multiple rabbit clusters in my application. Hence cannot use the Autoconfiguration, since it supports only 1 cluster
The maximum what we can suggest is a RabbitConnectionFactoryBean
. It is not recommended to use out-of-the-box @ConfigurationProperties
in the target projects. They are mostly internal for Spring Boot and are subject for often changes.
You definitely can borrow a configuration idea from the mentioned auto-configuration and build your own CachingConnectionFactory
object.
UPDATE
According to your it sounds like you really can extract a common method to be called with a specific properties instance:
@Bean(name = "rp1")
@ConfigurationProperties(prefix = "app.custom")
public RabbitProperties rp1() {
return new RabbitProperties();
}
@Bean
public ConnectionFactory cf1(
@Qualifier("rp1") RabbitProperties rp1) {
return createConnectionFactoryByProperties(rp1);
}
@Bean(name = "rp2")
@ConfigurationProperties(prefix = "app.custom1")
public RabbitProperties rp2() {
return new RabbitProperties();
}
@Bean
public ConnectionFactory cf2(
@Qualifier("rp2") RabbitProperties rp2) {
return createConnectionFactoryByProperties(rp2);
}
private CachingConnectionFactory createConnectionFactoryByProperties(RabbitProperties rp) {
final CachingConnectionFactory cf = new CachingConnectionFactory();
connectionFactory.setHost(rp.getHost());
//remaing code left off
}