Skip to content

Commit 7d9a14b

Browse files
committed
Merge branch '5.2.x-stable'
2 parents 2e5ecf4 + b71405c commit 7d9a14b

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

src/main/java/com/rabbitmq/client/ConnectionFactoryConfigurator.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141
* but also from the classpath, by using the <code>classpath:</code> prefix
4242
* in the location.
4343
*
44-
* If default client properties should be set, set the <code>use.default.client.properties</code>
45-
* key to <code>true</code>. Custom client properties can be set by using
46-
* the <code>client.properties.</code>, e.g. <code>client.properties.app.name</code>.
44+
* Client properties can be set by using
45+
* the <code>client.properties.</code> prefix, e.g. <code>client.properties.app.name</code>.
46+
* Default client properties and custom client properties are merged. To remove
47+
* a default client property, set its key to an empty value.
4748
*
4849
* @since 4.4.0
4950
* @see ConnectionFactory#load(String, String)
@@ -63,7 +64,6 @@ public class ConnectionFactoryConfigurator {
6364
public static final String CONNECTION_TIMEOUT = "connection.timeout";
6465
public static final String HANDSHAKE_TIMEOUT = "handshake.timeout";
6566
public static final String SHUTDOWN_TIMEOUT = "shutdown.timeout";
66-
public static final String USE_DEFAULT_CLIENT_PROPERTIES = "use.default.client.properties";
6767
public static final String CLIENT_PROPERTIES_PREFIX = "client.properties.";
6868
public static final String CONNECTION_RECOVERY_ENABLED = "connection.recovery.enabled";
6969
public static final String TOPOLOGY_RECOVERY_ENABLED = "topology.recovery.enabled";
@@ -169,17 +169,21 @@ public static void load(ConnectionFactory cf, Map<String, String> properties, St
169169
}
170170

171171
Map<String, Object> clientProperties = new HashMap<String, Object>();
172-
String useDefaultClientProperties = properties.get(prefix + USE_DEFAULT_CLIENT_PROPERTIES);
173-
if (useDefaultClientProperties != null && Boolean.valueOf(useDefaultClientProperties)) {
174-
clientProperties.putAll(AMQConnection.defaultClientProperties());
175-
}
172+
Map<String, Object> defaultClientProperties = AMQConnection.defaultClientProperties();
173+
clientProperties.putAll(defaultClientProperties);
176174

177175
for (Map.Entry<String, String> entry : properties.entrySet()) {
178176
if (entry.getKey().startsWith(prefix + CLIENT_PROPERTIES_PREFIX)) {
179-
clientProperties.put(
180-
entry.getKey().substring((prefix + CLIENT_PROPERTIES_PREFIX).length()),
181-
entry.getValue()
182-
);
177+
String clientPropertyKey = entry.getKey().substring((prefix + CLIENT_PROPERTIES_PREFIX).length());
178+
if (defaultClientProperties.containsKey(clientPropertyKey) && (entry.getValue() == null || entry.getValue().trim().isEmpty())) {
179+
// if default property and value is empty, remove this property
180+
clientProperties.remove(clientPropertyKey);
181+
} else {
182+
clientProperties.put(
183+
clientPropertyKey,
184+
entry.getValue()
185+
);
186+
}
183187
}
184188
}
185189
cf.setClientProperties(clientProperties);

src/test/java/com/rabbitmq/client/test/PropertyFileInitialisationTest.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,34 @@ public static Object[] data() {
9191
assertThat(cf.getPort(), is(5673));
9292
}
9393

94-
@Test public void propertyInitialisationIncludeDefaultClientProperties() {
94+
@Test public void propertyInitialisationIncludeDefaultClientPropertiesByDefault() {
95+
cf.load(new HashMap<String, String>());
96+
assertThat(cf.getClientProperties().entrySet(), hasSize(defaultClientProperties().size()));
97+
}
98+
99+
@Test public void propertyInitialisationAddCustomClientProperty() {
95100
cf.load(new HashMap<String, String>() {{
96-
put("rabbitmq.use.default.client.properties", "true");
97101
put("rabbitmq.client.properties.foo", "bar");
98102
}});
99103
assertThat(cf.getClientProperties().entrySet(), hasSize(defaultClientProperties().size() + 1));
100104
assertThat(cf.getClientProperties().get("foo").toString(), is("bar"));
101105
}
102106

103-
@Test public void propertyInitialisationDoNotIncludeDefaultClientProperties() {
107+
@Test public void propertyInitialisationGetRidOfDefaultClientPropertyWithEmptyValue() {
108+
final String key = defaultClientProperties().entrySet().iterator().next().getKey();
104109
cf.load(new HashMap<String, String>() {{
105-
put("rabbitmq.use.default.client.properties", "false");
106-
put("rabbitmq.client.properties.foo", "bar");
110+
put("rabbitmq.client.properties." + key, "");
107111
}});
108-
assertThat(cf.getClientProperties().entrySet(), hasSize(1));
109-
assertThat(cf.getClientProperties().get("foo").toString(), is("bar"));
112+
assertThat(cf.getClientProperties().entrySet(), hasSize(defaultClientProperties().size() - 1));
113+
}
114+
115+
@Test public void propertyInitialisationOverrideDefaultClientProperty() {
116+
final String key = defaultClientProperties().entrySet().iterator().next().getKey();
117+
cf.load(new HashMap<String, String>() {{
118+
put("rabbitmq.client.properties." + key, "whatever");
119+
}});
120+
assertThat(cf.getClientProperties().entrySet(), hasSize(defaultClientProperties().size()));
121+
assertThat(cf.getClientProperties().get(key).toString(), is("whatever"));
110122
}
111123

112124
@Test public void propertyInitialisationDoNotUseNio() throws Exception {

0 commit comments

Comments
 (0)