Eliminate Duplicate RabbitMQ Configuration Options

Fixes #447

Some Rabbit MQ configuration properties duplicate options already found in Spring Boot.
This commit is contained in:
Marius Bogoevici
2016-03-24 13:19:51 -04:00
committed by Gary Russell
parent b5099b610e
commit ba371ab67a
7 changed files with 34 additions and 120 deletions

View File

@@ -59,6 +59,7 @@ import org.springframework.amqp.support.postprocessor.DelegatingDecompressingPos
import org.springframework.amqp.support.postprocessor.GZipPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.cloud.stream.binder.AbstractBinder;
import org.springframework.cloud.stream.binder.Binding;
import org.springframework.cloud.stream.binder.DefaultBinding;
@@ -69,7 +70,6 @@ import org.springframework.cloud.stream.binder.MessageValues;
import org.springframework.cloud.stream.binder.PartitionHandler;
import org.springframework.context.Lifecycle;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -130,35 +130,27 @@ public class RabbitMessageChannelBinder extends AbstractBinder<MessageChannel, E
private final GenericApplicationContext autoDeclareContext = new GenericApplicationContext();
private final RabbitProperties rabbitProperties;
private ConnectionFactory connectionFactory;
private MessagePostProcessor decompressingPostProcessor = new DelegatingDecompressingPostProcessor();
private MessagePostProcessor compressingPostProcessor = new GZipPostProcessor();
private volatile String[] addresses;
private volatile String[] adminAddresses;
private volatile String[] nodes;
private String username;
private String password;
private String vhost;
private boolean useSSL;
private Resource sslPropertiesLocation;
private volatile boolean clustered;
private RabbitExtendedBindingProperties extendedBindingProperties = new RabbitExtendedBindingProperties();
public RabbitMessageChannelBinder(ConnectionFactory connectionFactory) {
public RabbitMessageChannelBinder(ConnectionFactory connectionFactory, RabbitProperties rabbitProperties) {
Assert.notNull(connectionFactory, "connectionFactory must not be null");
Assert.notNull(rabbitProperties, "rabbitProperties must not be null");
this.connectionFactory = connectionFactory;
this.rabbitProperties = rabbitProperties;
this.rabbitAdmin = new RabbitAdmin(connectionFactory);
this.autoDeclareContext.refresh();
this.rabbitAdmin.setApplicationContext(this.autoDeclareContext);
@@ -184,11 +176,6 @@ public class RabbitMessageChannelBinder extends AbstractBinder<MessageChannel, E
this.compressingPostProcessor = compressingPostProcessor;
}
public void setAddresses(String[] addresses) {
this.addresses = Arrays.copyOf(addresses, addresses.length);
}
public void setAdminAddresses(String[] adminAddresses) {
this.adminAddresses = Arrays.copyOf(adminAddresses, adminAddresses.length);
}
@@ -198,39 +185,23 @@ public class RabbitMessageChannelBinder extends AbstractBinder<MessageChannel, E
this.clustered = nodes.length > 1;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setExtendedBindingProperties(RabbitExtendedBindingProperties extendedBindingProperties) {
this.extendedBindingProperties = extendedBindingProperties;
}
public void setVhost(String vhost) {
this.vhost = vhost;
}
public void setUseSSL(boolean useSSL) {
this.useSSL = useSSL;
}
public void setSslPropertiesLocation(Resource sslPropertiesLocation) {
this.sslPropertiesLocation = sslPropertiesLocation;
}
@Override
public void onInit() {
if (this.clustered) {
Assert.state(this.addresses.length == this.adminAddresses.length
&& this.addresses.length == this.nodes.length,
String[] addresses = StringUtils.commaDelimitedListToStringArray(this.rabbitProperties.getAddresses());
Assert.state(addresses.length == this.adminAddresses.length
&& addresses.length == this.nodes.length,
"'addresses', 'adminAddresses', and 'nodes' properties must have equal length");
this.connectionFactory = new LocalizedQueueConnectionFactory(this.connectionFactory, this.addresses,
this.adminAddresses, this.nodes, this.vhost, this.username, this.password, this.useSSL,
this.sslPropertiesLocation);
this.connectionFactory = new LocalizedQueueConnectionFactory(this.connectionFactory, addresses,
this.adminAddresses, this.nodes, rabbitProperties.getVirtualHost(),
this.rabbitProperties.getUsername(), this.rabbitProperties.getPassword(),
this.rabbitProperties.getSsl().isEnabled(), this.rabbitProperties.getSsl().getKeyStore(),
this.rabbitProperties.getSsl().getTrustStore(), this.rabbitProperties.getSsl().getKeyStorePassword(),
this.rabbitProperties.getSsl().getTrustStorePassword());
}
}

View File

@@ -43,14 +43,6 @@ class RabbitBinderConfigurationProperties {
private int compressionLevel;
public String[] getAddresses() {
return addresses;
}
public void setAddresses(String[] addresses) {
this.addresses = addresses;
}
public String[] getAdminAdresses() {
return adminAdresses;
}
@@ -67,46 +59,6 @@ class RabbitBinderConfigurationProperties {
this.nodes = nodes;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getVhost() {
return vhost;
}
public void setVhost(String vhost) {
this.vhost = vhost;
}
public boolean isUseSSL() {
return useSSL;
}
public void setUseSSL(boolean useSSL) {
this.useSSL = useSSL;
}
public Resource getSslPropertiesLocation() {
return sslPropertiesLocation;
}
public void setSslPropertiesLocation(Resource sslPropertiesLocation) {
this.sslPropertiesLocation = sslPropertiesLocation;
}
public int getCompressionLevel() {
return compressionLevel;
}

View File

@@ -22,6 +22,7 @@ import org.springframework.amqp.support.postprocessor.DelegatingDecompressingPos
import org.springframework.amqp.support.postprocessor.GZipPostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.stream.binder.rabbit.RabbitExtendedBindingProperties;
import org.springframework.cloud.stream.binder.rabbit.RabbitMessageChannelBinder;
@@ -49,6 +50,9 @@ public class RabbitMessageChannelBinderConfiguration {
@Autowired
private ConnectionFactory rabbitConnectionFactory;
@Autowired
private RabbitProperties rabbitProperties;
@Autowired
private RabbitBinderConfigurationProperties rabbitBinderConfigurationProperties;
@@ -57,18 +61,12 @@ public class RabbitMessageChannelBinderConfiguration {
@Bean
RabbitMessageChannelBinder rabbitMessageChannelBinder() {
RabbitMessageChannelBinder binder = new RabbitMessageChannelBinder(rabbitConnectionFactory);
RabbitMessageChannelBinder binder = new RabbitMessageChannelBinder(rabbitConnectionFactory, rabbitProperties);
binder.setCodec(codec);
binder.setAddresses(rabbitBinderConfigurationProperties.getAddresses());
binder.setAdminAddresses(rabbitBinderConfigurationProperties.getAdminAdresses());
binder.setCompressingPostProcessor(gZipPostProcessor());
binder.setDecompressingPostProcessor(deCompressingPostProcessor());
binder.setNodes(rabbitBinderConfigurationProperties.getNodes());
binder.setPassword(rabbitBinderConfigurationProperties.getPassword());
binder.setSslPropertiesLocation(rabbitBinderConfigurationProperties.getSslPropertiesLocation());
binder.setUsername(rabbitBinderConfigurationProperties.getUsername());
binder.setUseSSL(rabbitBinderConfigurationProperties.isUseSSL());
binder.setVhost(rabbitBinderConfigurationProperties.getVhost());
binder.setExtendedBindingProperties(rabbitExtendedBindingProperties);
return binder;
}

View File

@@ -54,7 +54,7 @@ public class LocalizedQueueConnectionFactoryIntegrationTests {
String username = "guest";
String password = "guest";
this.lqcf = new LocalizedQueueConnectionFactory(defaultConnectionFactory, addresses,
adminAddresses, nodes, vhost, username, password, false, null);
adminAddresses, nodes, vhost, username, password, false, null, null, null, null);
}
@Test

View File

@@ -54,6 +54,7 @@ import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.amqp.support.postprocessor.DelegatingDecompressingPostProcessor;
import org.springframework.amqp.utils.test.TestUtils;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.cloud.stream.binder.Binding;
import org.springframework.cloud.stream.binder.ExtendedConsumerProperties;
import org.springframework.cloud.stream.binder.ExtendedProducerProperties;
@@ -93,7 +94,7 @@ public class RabbitBinderTests extends PartitionCapableBinderTests<RabbitTestBin
@Override
protected RabbitTestBinder getBinder() {
if (testBinder == null) {
testBinder = new RabbitTestBinder(rabbitAvailableRule.getResource());
testBinder = new RabbitTestBinder(rabbitAvailableRule.getResource(), new RabbitProperties());
}
return testBinder;
}
@@ -632,7 +633,7 @@ public class RabbitBinderTests extends PartitionCapableBinderTests<RabbitTestBin
public void testLateBinding() throws Exception {
RabbitTestSupport.RabbitProxy proxy = new RabbitTestSupport.RabbitProxy();
CachingConnectionFactory cf = new CachingConnectionFactory("localhost", proxy.getPort());
RabbitMessageChannelBinder rabbitBinder = new RabbitMessageChannelBinder(cf);
RabbitMessageChannelBinder rabbitBinder = new RabbitMessageChannelBinder(cf, new RabbitProperties());
RabbitTestBinder binder = new RabbitTestBinder(cf, rabbitBinder);
ExtendedProducerProperties<RabbitProducerProperties> properties = createProducerProperties();

View File

@@ -21,6 +21,7 @@ import java.util.Set;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.cloud.stream.binder.AbstractTestBinder;
import org.springframework.cloud.stream.binder.Binding;
import org.springframework.cloud.stream.binder.ExtendedConsumerProperties;
@@ -49,8 +50,8 @@ public class RabbitTestBinder extends AbstractTestBinder<RabbitMessageChannelBin
private final Set<String> exchanges = new HashSet<String>();
public RabbitTestBinder(ConnectionFactory connectionFactory) {
this(connectionFactory, new RabbitMessageChannelBinder(connectionFactory));
public RabbitTestBinder(ConnectionFactory connectionFactory, RabbitProperties rabbitProperties) {
this(connectionFactory, new RabbitMessageChannelBinder(connectionFactory, rabbitProperties));
}
public RabbitTestBinder(ConnectionFactory connectionFactory, RabbitMessageChannelBinder binder) {

View File

@@ -584,26 +584,17 @@ implementations.
==== Rabbit MQ Binder properties
The binder supports the all Spring Boot properties for Rabbit MQ configuration.
By default, the binder uses the Spring Boot `ConnectionFactory` and therefore it supports all the Spring Boot
configuration options for Rabbit MQ.
For reference, consult the [Spring Boot documentation](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties).
Rabbit MQ configuration options use the `spring.rabbitmq` prefix.
In addition to that, it also supports the following properties:
spring.cloud.stream.rabbit.binder.addresses::
A comma-separated list of RabbitMQ server addresses (used only for clustering and in conjunction with `nodes`). Default empty.
spring.cloud.stream.rabbit.binder.adminAddresses. Default empty.
A comma-separated list of RabbitMQ management plugin URLs - only used when nodes contains more than one entry. Entries in this list must correspond to the corresponding entry in addresses. Default empty.
A comma-separated list of RabbitMQ management plugin URLs - only used when nodes contains more than one entry. Each entry in this list must have a corresponding entry in `spring.rabbitmq.addresses`. Empty by default.
spring.cloud.stream.rabbit.binder.nodes::
A comma-separated list of RabbitMQ node names; when more than one entry, used to locate the server address where a queue is located. Entries in this list must correspond to the corresponding entry in addresses. Default empty.
spring.cloud.stream.rabbit.rabbit.username::
The user name. Default `null`.
spring.cloud.stream.rabbit.binder.password::
The password. Default `null`.
spring.cloud.stream.rabbit.binder.vhost::
The virtual host. Default `null`.
spring.cloud.stream.rabbit.binder.useSSL::
True if Rabbit MQ should use SSL.
spring.cloud.stream.rabbit.binder.sslPropertiesLocation::
The location of the SSL properties file, when certificate exchange is used.
A comma-separated list of RabbitMQ node names; when more than one entry, used to locate the server address where a queue is located. Each entry in this list must have a corresponding entry in `spring.rabbitmq.addresses`. Empty by default.
spring.cloud.stream.rabbit.binder.compressionLevel::
Compression level for compressed bindings. Defaults to `1` (BEST_LEVEL). See `java.util.zip.Deflater`.
@@ -982,4 +973,4 @@ hello world 1458595077732
hello world 1458595078733
hello world 1458595079734
hello world 1458595080735
----
----