This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new f7df97fa73a4 CAMEL-26989: camel-spring-rabbitmq - with
ignoreDeclarationExceptions=true, bind and consume a queue whose declaration
failed by its name (#30058)
f7df97fa73a4 is described below
commit f7df97fa73a48fd4a20db94067b65cd9670ae2f6
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Nov 15 19:04:03 2026 +0100
CAMEL-26989: camel-spring-rabbitmq - with ignoreDeclarationExceptions=true,
bind and consume a queue whose declaration failed by its name (#30058)
Co-authored-by: Claude <[email protected]>
---
.../springrabbit/SpringRabbitMQEndpoint.java | 5 +
...ingRabbitMQIgnoreDeclarationExceptionsTest.java | 127 +++++++++++++++++++++
...bitMQConsumerIgnoreDeclarationExceptionsIT.java | 68 +++++++++++
3 files changed, 200 insertions(+)
diff --git
a/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQEndpoint.java
b/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQEndpoint.java
index ba560b46b8c7..317f3ba2a962 100644
---
a/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQEndpoint.java
+++
b/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQEndpoint.java
@@ -824,6 +824,11 @@ public class SpringRabbitMQEndpoint extends
DefaultEndpoint implements AsyncEndp
rabbitQueue.getName(), durable, autoDelete,
exclusive);
}
qn = admin.declareQueue(rabbitQueue);
+ if (qn == null) {
+ // the declaration failed and the admin ignores
declaration exceptions (ignoreDeclarationExceptions):
+ // use the name of the queue (given, or generated on
the client) to bind and consume
+ qn = rabbitQueue.getName();
+ }
// if we auto created a new unique queue then the
container needs to know the queue name
if (generateUniqueQueue && container != null) {
container.setQueueNames(qn);
diff --git
a/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/SpringRabbitMQIgnoreDeclarationExceptionsTest.java
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/SpringRabbitMQIgnoreDeclarationExceptionsTest.java
new file mode 100644
index 000000000000..e50f9c918339
--- /dev/null
+++
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/SpringRabbitMQIgnoreDeclarationExceptionsTest.java
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.springrabbit;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.springframework.amqp.core.AmqpAdmin;
+import org.springframework.amqp.core.Binding;
+import org.springframework.amqp.core.Queue;
+import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
+import
org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer;
+import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * With {@code ignoreDeclarationExceptions=true}, {@code
RabbitAdmin.declareQueue} logs a failed queue declaration and
+ * returns {@code null}: the consumer binds and consumes the queue by its name.
+ */
+class SpringRabbitMQIgnoreDeclarationExceptionsTest {
+
+ private CachingConnectionFactory connectionFactory;
+ private CamelContext context;
+
+ @BeforeEach
+ void setUp() throws IOException {
+ // nothing listens on this port: every declaration fails
+ connectionFactory = new CachingConnectionFactory("localhost",
closedPort());
+ context = new DefaultCamelContext();
+ context.getRegistry().bind("cf", connectionFactory);
+ context.getComponent("spring-rabbitmq",
SpringRabbitMQComponent.class).setIgnoreDeclarationExceptions(true);
+ }
+
+ @AfterEach
+ void tearDown() {
+ context.stop();
+ connectionFactory.destroy();
+ }
+
+ @Test
+ void consumerStartsWhenTheDeclarationsFailAndAreIgnored() throws Exception
{
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
from("spring-rabbitmq:orders?connectionFactory=#cf&queues=orders&routingKey=new").routeId("orders")
+ .to("mock:orders");
+ }
+ });
+
+ assertDoesNotThrow(() -> context.start());
+ assertEquals(ServiceStatus.Started,
context.getRouteController().getRouteStatus("orders"));
+ }
+
+ @Test
+ void queueIsBoundByItsNameWhenItsDeclarationFailsAndIsIgnored() {
+ context.start();
+ SpringRabbitMQEndpoint endpoint = context.getEndpoint(
+
"spring-rabbitmq:orders?connectionFactory=#cf&queues=orders&routingKey=new",
SpringRabbitMQEndpoint.class);
+ AmqpAdmin admin = mock(AmqpAdmin.class);
+ // what RabbitAdmin answers when it ignores the failed declaration
+ when(admin.declareQueue(any(Queue.class))).thenReturn(null);
+
+ endpoint.declareElements(new
DirectMessageListenerContainer(connectionFactory), admin);
+
+ ArgumentCaptor<Binding> binding =
ArgumentCaptor.forClass(Binding.class);
+ verify(admin).declareBinding(binding.capture());
+ assertEquals("orders", binding.getValue().getDestination());
+ assertEquals("orders", binding.getValue().getExchange());
+ assertEquals("new", binding.getValue().getRoutingKey());
+ }
+
+ @Test
+ void
generatedQueueIsConsumedByItsNameWhenItsDeclarationFailsAndIsIgnored() {
+ context.start();
+ // no queues: the consumer declares a queue with a name generated on
the client
+ SpringRabbitMQEndpoint endpoint = context.getEndpoint(
+ "spring-rabbitmq:orders?connectionFactory=#cf&routingKey=new",
SpringRabbitMQEndpoint.class);
+ AbstractMessageListenerContainer container = new
DirectMessageListenerContainer(connectionFactory);
+ AmqpAdmin admin = mock(AmqpAdmin.class);
+ when(admin.declareQueue(any(Queue.class))).thenReturn(null);
+
+ endpoint.declareElements(container, admin);
+
+ ArgumentCaptor<Queue> queue = ArgumentCaptor.forClass(Queue.class);
+ verify(admin).declareQueue(queue.capture());
+ String generated = queue.getValue().getName();
+ assertArrayEquals(new String[] { generated },
container.getQueueNames());
+ ArgumentCaptor<Binding> binding =
ArgumentCaptor.forClass(Binding.class);
+ verify(admin).declareBinding(binding.capture());
+ assertEquals(generated, binding.getValue().getDestination());
+ }
+
+ private static int closedPort() throws IOException {
+ try (ServerSocket socket = new ServerSocket(0)) {
+ return socket.getLocalPort();
+ }
+ }
+}
diff --git
a/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerIgnoreDeclarationExceptionsIT.java
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerIgnoreDeclarationExceptionsIT.java
new file mode 100644
index 000000000000..c738c9046b90
--- /dev/null
+++
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerIgnoreDeclarationExceptionsIT.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.springrabbit.integration;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.springrabbit.SpringRabbitMQComponent;
+import org.junit.jupiter.api.Test;
+import org.springframework.amqp.core.QueueBuilder;
+import org.springframework.amqp.rabbit.connection.ConnectionFactory;
+import org.springframework.amqp.rabbit.core.RabbitAdmin;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+/**
+ * With {@code ignoreDeclarationExceptions=true}, a consumer starts on a queue
that exists with other arguments than the
+ * ones it declares, and consumes from it.
+ */
+public class RabbitMQConsumerIgnoreDeclarationExceptionsIT extends
RabbitMQITSupport {
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ context.getComponent("spring-rabbitmq",
SpringRabbitMQComponent.class).setIgnoreDeclarationExceptions(true);
+ return context;
+ }
+
+ @Test
+ public void testConsumerOnQueueDeclaredWithOtherArguments() throws
Exception {
+ String exchange = uniqueName("orders");
+ String queue = uniqueName("orders-queue");
+ // the queue is a quorum queue, the consumer declares a classic queue:
the broker refuses that declaration
+ ConnectionFactory cf =
context.getRegistry().lookupByNameAndType("myCF", ConnectionFactory.class);
+ new
RabbitAdmin(cf).declareQueue(QueueBuilder.durable(queue).quorum().build());
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("order 1");
+
+ assertDoesNotThrow(() -> context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("spring-rabbitmq:" + exchange + "?queues=" + queue +
"&routingKey=new")
+ .to("mock:result");
+ }
+ }));
+
+ template.sendBody("spring-rabbitmq:" + exchange + "?routingKey=new",
"order 1");
+
+ MockEndpoint.assertIsSatisfied(context, 30, TimeUnit.SECONDS);
+ }
+}