Skip to content

Commit 9371f8c

Browse files
garyrussellartembilan
authored andcommitted
GH-1732: Fix Listener Container Parser
Resolves #1372 Do not split `queue-names` in the parser - leave it to Spring's type converter. Also support SpEL in `queues`; a more elegant solution would require major refactoring of the parser; so this is just a compromise. **cherry-pick to 2.3.x, 2.2.x**
1 parent 6628bcb commit 9371f8c

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

Diff for: spring-rabbit/src/main/java/org/springframework/amqp/rabbit/config/ListenerContainerParser.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -179,21 +179,21 @@ private void parseListener(Element listenerEle, Element containerEle, ParserCont
179179

180180
String queueNames = listenerEle.getAttribute(QUEUE_NAMES_ATTRIBUTE);
181181
if (StringUtils.hasText(queueNames)) {
182-
String[] names = StringUtils.commaDelimitedListToStringArray(queueNames);
183-
List<TypedStringValue> values = new ManagedList<TypedStringValue>();
184-
for (int i = 0; i < names.length; i++) {
185-
values.add(new TypedStringValue(names[i].trim()));
186-
}
187-
containerDef.getPropertyValues().add("queueNames", values);
182+
containerDef.getPropertyValues().add("queueNames", queueNames);
188183
}
189184
String queues = listenerEle.getAttribute(QUEUES_ATTRIBUTE);
190185
if (StringUtils.hasText(queues)) {
191-
String[] names = StringUtils.commaDelimitedListToStringArray(queues);
192-
List<RuntimeBeanReference> values = new ManagedList<RuntimeBeanReference>();
193-
for (int i = 0; i < names.length; i++) {
194-
values.add(new RuntimeBeanReference(names[i].trim()));
186+
if (queues.startsWith("#{")) {
187+
containerDef.getPropertyValues().add("queues", queues);
188+
}
189+
else {
190+
String[] names = StringUtils.commaDelimitedListToStringArray(queues);
191+
List<RuntimeBeanReference> values = new ManagedList<RuntimeBeanReference>();
192+
for (int i = 0; i < names.length; i++) {
193+
values.add(new RuntimeBeanReference(names[i].trim()));
194+
}
195+
containerDef.getPropertyValues().add("queues", values);
195196
}
196-
containerDef.getPropertyValues().add("queues", values);
197197
}
198198

199199
ManagedMap<String, TypedStringValue> args = new ManagedMap<String, TypedStringValue>();

Diff for: spring-rabbit/src/test/java/org/springframework/amqp/rabbit/config/ListenerContainerPlaceholderParserTests.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2019 the original author or authors.
2+
* Copyright 2010-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -82,4 +82,21 @@ public void testParseWithQueueNames() throws Exception {
8282
assertThat(Arrays.asList(container.getQueueNames()).toString()).isEqualTo("[foo, " + queue.getName() + "]");
8383
}
8484

85+
@Test
86+
public void commasInPropertyNames() {
87+
SimpleMessageListenerContainer container = this.context.getBean("commaProps1",
88+
SimpleMessageListenerContainer.class);
89+
assertThat(container.getQueueNames()).containsExactly("foo", "bar");
90+
}
91+
92+
@Test
93+
public void commasInPropertyQueues() {
94+
SimpleMessageListenerContainer container = this.context.getBean("commaProps2",
95+
SimpleMessageListenerContainer.class);
96+
String[] queueNames = container.getQueueNames();
97+
assertThat(queueNames).hasSize(2);
98+
assertThat(queueNames[0]).isEqualTo("foo");
99+
assertThat(queueNames[1]).startsWith("spring.gen");
100+
}
101+
85102
}

Diff for: spring-rabbit/src/test/resources/org/springframework/amqp/rabbit/annotation/rabbit-listener.properties

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ rabbit.listener.queue=queue1
44
rabbit.listener.priority=34
55
rabbit.listener.responseRoutingKey=routing-123
66
rabbit.listener.admin=rabbitAdmin
7+
8+
foo.and.bar=foo, bar

Diff for: spring-rabbit/src/test/resources/org/springframework/amqp/rabbit/config/ListenerContainerPlaceholderParserTests-context.xml

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<prop key="five">5</prop>
1212
<prop key="one">1</prop>
1313
<prop key="false">false</prop>
14+
<prop key="foo.and.bar">foo, bar</prop>
1415
</props>
1516
</property>
1617
</bean>
@@ -28,4 +29,13 @@
2829

2930
<bean id="testBean" class="org.springframework.amqp.rabbit.config.ListenerContainerParserTests$TestBean"/>
3031

32+
<rabbit:listener-container connection-factory="connectionFactory" auto-startup="false">
33+
<rabbit:listener id="commaProps1" queue-names="${foo.and.bar}" ref="testBean" method="handle"/>
34+
</rabbit:listener-container>
35+
36+
<rabbit:listener-container connection-factory="connectionFactory" auto-startup="false">
37+
<rabbit:listener id="commaProps2" queues="#{T(java.util.Arrays).asList(@foo, @bar).toArray()}"
38+
ref="testBean" method="handle"/>
39+
</rabbit:listener-container>
40+
3141
</beans>

0 commit comments

Comments
 (0)