Skip to content

Commit df36606

Browse files
davsclausclaude
andauthored
CAMEL-24071: camel-spring-rabbitmq - Trim queue names and reject multi-queue polling
Trim each queue name after splitting on comma in DefaultListenerContainerFactory, so "q1, q2" no longer creates a listener for " q2" (with leading space) while declaring "q2" — fixing a mismatch at runtime. Also reject multiple comma-separated queues in SpringRabbitPollingConsumer.doInit() since RabbitTemplate.receive() only accepts a single queue name. Closes #24741 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ae6ec8c commit df36606

4 files changed

Lines changed: 95 additions & 3 deletions

File tree

components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/DefaultListenerContainerFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.camel.component.springrabbit;
1818

19+
import java.util.Arrays;
20+
1921
import org.springframework.amqp.core.AmqpAdmin;
2022
import org.springframework.amqp.rabbit.config.RetryInterceptorBuilder;
2123
import org.springframework.amqp.rabbit.core.RabbitAdmin;
@@ -39,7 +41,8 @@ public AbstractMessageListenerContainer createListenerContainer(SpringRabbitMQEn
3941
}
4042

4143
if (endpoint.getQueues() != null) {
42-
listener.setQueueNames(endpoint.getQueues().split(","));
44+
listener.setQueueNames(
45+
Arrays.stream(endpoint.getQueues().split(",")).map(String::trim).toArray(String[]::new));
4346
}
4447
listener.setAcknowledgeMode(endpoint.getAcknowledgeMode());
4548
listener.setExclusive(endpoint.isExclusive());

components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitPollingConsumer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ public Exchange receive() {
5151
@Override
5252
public Exchange receive(long timeout) {
5353
try {
54+
String queue = jmsEndpoint.getQueues().trim();
5455
Message message;
5556
if (timeout == 0) {
56-
message = template.receive(jmsEndpoint.getQueues());
57+
message = template.receive(queue);
5758
} else {
58-
message = template.receive(jmsEndpoint.getQueues(), timeout);
59+
message = template.receive(queue, timeout);
5960
}
6061
if (message != null) {
6162
return getEndpoint().createExchange(message);
@@ -71,6 +72,11 @@ protected void doInit() throws Exception {
7172
if (getEndpoint().getQueues() == null) {
7273
throw new IllegalArgumentException("Queues must be configured when using PollingConsumer");
7374
}
75+
if (getEndpoint().getQueues().contains(",")) {
76+
throw new IllegalArgumentException(
77+
"PollingConsumer does not support multiple queues. Configure a single queue name instead of: "
78+
+ getEndpoint().getQueues());
79+
}
7480
}
7581

7682
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.component.springrabbit;
18+
19+
import org.apache.camel.test.junit6.CamelTestSupport;
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
22+
import org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer;
23+
24+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
25+
26+
public class DefaultListenerContainerFactoryTest extends CamelTestSupport {
27+
28+
@Test
29+
void queueNamesShouldBeTrimmedAfterSplit() throws Exception {
30+
SpringRabbitMQEndpoint endpoint
31+
= context.getEndpoint("spring-rabbitmq:default?queues=myqueue, myotherqueue", SpringRabbitMQEndpoint.class);
32+
endpoint.setConnectionFactory(new CachingConnectionFactory("localhost"));
33+
34+
DefaultListenerContainerFactory factory = new DefaultListenerContainerFactory();
35+
AbstractMessageListenerContainer listener = factory.createListenerContainer(endpoint);
36+
37+
assertArrayEquals(new String[] { "myqueue", "myotherqueue" }, listener.getQueueNames(),
38+
"queue names must be trimmed after splitting on comma");
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.component.springrabbit;
18+
19+
import org.apache.camel.test.junit6.CamelTestSupport;
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
22+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
23+
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
public class SpringRabbitPollingConsumerTest extends CamelTestSupport {
28+
29+
@Test
30+
void multipleQueuesShouldBeRejected() throws Exception {
31+
SpringRabbitMQEndpoint endpoint
32+
= context.getEndpoint("spring-rabbitmq:default?queues=q1,q2", SpringRabbitMQEndpoint.class);
33+
CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
34+
endpoint.setConnectionFactory(cf);
35+
RabbitTemplate template = new RabbitTemplate(cf);
36+
37+
SpringRabbitPollingConsumer consumer = new SpringRabbitPollingConsumer(endpoint, template);
38+
39+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, consumer::doInit);
40+
assertTrue(ex.getMessage().contains("does not support multiple queues"),
41+
"error message should explain that polling consumer does not support multiple queues");
42+
}
43+
}

0 commit comments

Comments
 (0)