This is an automated email from the ASF dual-hosted git repository.
Croway 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 795ce94fb2da CAMEL-26990: camel-spring-rabbitmq - the consumer starts
while RabbitMQ is down, and declares again on every new connection (#30051)
795ce94fb2da is described below
commit 795ce94fb2da9ba9204aa01effd85fa277ec0259
Author: Federico Mariani <[email protected]>
AuthorDate: Mon Nov 16 08:46:58 2026 +0100
CAMEL-26990: camel-spring-rabbitmq - the consumer starts while RabbitMQ is
down, and declares again on every new connection (#30051)
* CAMEL-26990: camel-spring-rabbitmq - the consumer starts while RabbitMQ
is down, and declares again on every new connection
With autoDeclare=true (the default) the consumer declared the exchange,
queue and binding once, in doStart: the route did not start while the
broker was not reachable (AmqpConnectException), and a queue the broker
deleted when the connection was lost (auto-delete, exclusive) was never
declared again after the listener container reconnected.
The consumer now registers a ConnectionListener on the connection factory
while it runs and declares again on every new connection, as Spring's
RabbitAdmin does; a generated queue name is reused. A declaration that
fails at start because the broker is not available is logged and left to
that listener, unless testConnectionOnStartup=true. A
CachingConnectionFactory
with cache mode CONNECTION keeps the former behaviour, as RabbitAdmin does
not declare on new connections with it either.
Document auto declare (on by default for the consumer) and add an
upgrade-guide entry.
* CAMEL-26990: camel-spring-rabbitmq - a consumer without queues picks its
generated queue name before it starts
Without the queues option, the consumer set the generated queue name on
the listener container only after the broker declared the queue. With
RabbitMQ down at start, the declaration failed first, so the container
started without a queue: an SMLC never connected, and a DMLC refused
setQueueNames while running on each new connection, which left one more
unbound queue on the broker each time.
The name is now generated first (Base64UrlNamingStrategy.DEFAULT, as
QueueBuilder.durable() does, with the same flags) and set on the container
before the broker is called; later declarations reuse it.
The upgrade guide says that the route shows as started while RabbitMQ is
down, and that testConnectionOnStartup=true keeps failing the start, for
example for the supervising route controller.
---
.../catalog/docs/spring-rabbitmq-component.adoc | 11 +
.../src/main/docs/spring-rabbitmq-component.adoc | 11 +
.../springrabbit/SpringRabbitMQConsumer.java | 106 +++++++-
.../springrabbit/SpringRabbitMQEndpoint.java | 39 ++-
.../SpringRabbitMQConsumerDeclareTest.java | 296 +++++++++++++++++++++
.../RabbitMQConsumerBrokerStartsLaterIT.java | 138 ++++++++++
.../integration/RabbitMQConsumerRedeclareIT.java | 155 +++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 20 ++
8 files changed, 761 insertions(+), 15 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-rabbitmq-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-rabbitmq-component.adoc
index 5639991d869b..9b5ce24ae78a 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-rabbitmq-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-rabbitmq-component.adoc
@@ -152,6 +152,17 @@ Configure the endpoint with the settings of the existing
elements, or turn the d
(on the endpoint or the component), or for some elements with
`skipDeclareExchange`, `skipDeclareQueue` or
`skipBindQueue`.
+The consumer declares them again each time the connection factory opens a new
connection, for example after the
+connection to RabbitMQ was lost: a queue that RabbitMQ deleted meanwhile, such
as an auto-delete or exclusive queue, is
+declared again when the consumer reconnects.
+
+When RabbitMQ is not available when the consumer starts, the consumer logs a
warning and starts anyway: the listener
+container keeps trying to connect, and the exchange, queue and binding are
declared when it connects.
+To fail the start instead, set `testConnectionOnStartup=true`.
+
+NOTE: With a `CachingConnectionFactory` whose cache mode is `CONNECTION`, the
consumer declares only when it starts, as
+Spring's `RabbitAdmin`, and fails to start when RabbitMQ is not available.
+
The elements can be configured using the multivalued `args` option.
For example, to specify the queue as exclusive, you can configure the endpoint
uri with `arg.queue.exclusive=true`.
diff --git
a/components/camel-spring-parent/camel-spring-rabbitmq/src/main/docs/spring-rabbitmq-component.adoc
b/components/camel-spring-parent/camel-spring-rabbitmq/src/main/docs/spring-rabbitmq-component.adoc
index 5639991d869b..9b5ce24ae78a 100644
---
a/components/camel-spring-parent/camel-spring-rabbitmq/src/main/docs/spring-rabbitmq-component.adoc
+++
b/components/camel-spring-parent/camel-spring-rabbitmq/src/main/docs/spring-rabbitmq-component.adoc
@@ -152,6 +152,17 @@ Configure the endpoint with the settings of the existing
elements, or turn the d
(on the endpoint or the component), or for some elements with
`skipDeclareExchange`, `skipDeclareQueue` or
`skipBindQueue`.
+The consumer declares them again each time the connection factory opens a new
connection, for example after the
+connection to RabbitMQ was lost: a queue that RabbitMQ deleted meanwhile, such
as an auto-delete or exclusive queue, is
+declared again when the consumer reconnects.
+
+When RabbitMQ is not available when the consumer starts, the consumer logs a
warning and starts anyway: the listener
+container keeps trying to connect, and the exchange, queue and binding are
declared when it connects.
+To fail the start instead, set `testConnectionOnStartup=true`.
+
+NOTE: With a `CachingConnectionFactory` whose cache mode is `CONNECTION`, the
consumer declares only when it starts, as
+Spring's `RabbitAdmin`, and fails to start when RabbitMQ is not available.
+
The elements can be configured using the multivalued `args` option.
For example, to specify the queue as exclusive, you can configure the endpoint
uri with `arg.queue.exclusive=true`.
diff --git
a/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumer.java
b/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumer.java
index bf4c215ce1e6..b7a895ccdd65 100644
---
a/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumer.java
+++
b/components/camel-spring-parent/camel-spring-rabbitmq/src/main/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumer.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.component.springrabbit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
import org.apache.camel.Endpoint;
import org.apache.camel.FailedToCreateConsumerException;
import org.apache.camel.Processor;
@@ -23,7 +25,11 @@ import org.apache.camel.Suspendable;
import org.apache.camel.support.DefaultConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.amqp.AmqpConnectException;
+import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.Connection;
+import org.springframework.amqp.rabbit.connection.ConnectionFactory;
+import org.springframework.amqp.rabbit.connection.ConnectionListener;
import org.springframework.amqp.rabbit.connection.RabbitUtils;
import
org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer;
@@ -34,6 +40,7 @@ public class SpringRabbitMQConsumer extends DefaultConsumer
implements Suspendab
private AbstractMessageListenerContainer listenerContainer;
private volatile EndpointMessageListener messageListener;
private volatile boolean initialized;
+ private volatile DeclaringConnectionListener declaringConnectionListener;
public SpringRabbitMQConsumer(Endpoint endpoint, Processor processor,
AbstractMessageListenerContainer listenerContainer) {
super(endpoint, processor);
@@ -102,7 +109,12 @@ public class SpringRabbitMQConsumer extends
DefaultConsumer implements Suspendab
createMessageListenerContainer();
}
- prepareAndStartListenerContainer();
+ try {
+ prepareAndStartListenerContainer();
+ } catch (Exception e) {
+ removeDeclaringConnectionListener();
+ throw e;
+ }
// mark as initialized for the first time
initialized = true;
@@ -110,6 +122,7 @@ public class SpringRabbitMQConsumer extends DefaultConsumer
implements Suspendab
@Override
protected void doStop() throws Exception {
+ removeDeclaringConnectionListener();
if (listenerContainer != null) {
stopAndDestroyListenerContainer();
}
@@ -149,7 +162,7 @@ public class SpringRabbitMQConsumer extends DefaultConsumer
implements Suspendab
if (getEndpoint().isAutoDeclare()) {
// auto declare but without spring
- getEndpoint().declareElements(listenerContainer);
+ autoDeclareElements();
}
// only start listener if auto start is enabled or we are explicit
invoking start later
@@ -162,6 +175,53 @@ public class SpringRabbitMQConsumer extends
DefaultConsumer implements Suspendab
}
}
+ /**
+ * Declares the exchange, queue and binding of the endpoint now, and again
on every new connection of the connection
+ * factory while this consumer runs, as Spring's {@code RabbitAdmin} does
for its declarables: a queue that the
+ * broker deleted when the connection was lost (auto-delete, exclusive, or
not durable and the broker restarted) is
+ * declared again when the listener container reconnects.
+ * <p/>
+ * When the broker is not available, the consumer starts anyway, unless
{@code testConnectionOnStartup} is enabled:
+ * the listener container keeps trying to connect, and the elements are
declared when it does.
+ */
+ private void autoDeclareElements() {
+ ConnectionFactory connectionFactory =
listenerContainer.getConnectionFactory();
+ DeclaringConnectionListener listener = null;
+ if (connectionFactory instanceof CachingConnectionFactory ccf
+ && ccf.getCacheMode() ==
CachingConnectionFactory.CacheMode.CONNECTION) {
+ // as RabbitAdmin, which does not declare on new connections with
this cache mode
+ LOG.debug("The connection factory caches connections: {} is
declared when the consumer starts only",
+ getEndpoint());
+ } else {
+ listener = new DeclaringConnectionListener(listenerContainer);
+ declaringConnectionListener = listener;
+ // a connection factory that is already connected may call the
listener now: it leaves that to this method
+ connectionFactory.addConnectionListener(listener);
+ }
+ try {
+ getEndpoint().declareElements(listenerContainer);
+ } catch (AmqpConnectException e) {
+ if (listener == null || getEndpoint().isTestConnectionOnStartup())
{
+ throw e;
+ }
+ LOG.warn("Cannot declare the exchange, queue and binding of {}
because the broker is not available: {}."
+ + " They are declared when the listener container
connects.",
+ getEndpoint(), e.getMessage());
+ } finally {
+ if (listener != null) {
+ listener.started();
+ }
+ }
+ }
+
+ private void removeDeclaringConnectionListener() {
+ DeclaringConnectionListener listener = declaringConnectionListener;
+ if (listener != null) {
+ listener.connectionFactory().removeConnectionListener(listener);
+ declaringConnectionListener = null;
+ }
+ }
+
protected void stopAndDestroyListenerContainer() {
if (listenerContainer != null) {
listenerContainer.stop();
@@ -175,4 +235,46 @@ public class SpringRabbitMQConsumer extends
DefaultConsumer implements Suspendab
initialized = false;
}
+ /**
+ * Declares the exchange, queue and binding of the endpoint on every new
connection.
+ */
+ private final class DeclaringConnectionListener implements
ConnectionListener {
+
+ private final AbstractMessageListenerContainer container;
+ private final AtomicBoolean declaring = new AtomicBoolean();
+ // the thread that starts the consumer declares by itself, also when
it creates the connection meanwhile
+ private volatile Thread startingThread = Thread.currentThread();
+
+ private DeclaringConnectionListener(AbstractMessageListenerContainer
container) {
+ this.container = container;
+ }
+
+ private ConnectionFactory connectionFactory() {
+ return container.getConnectionFactory();
+ }
+
+ private void started() {
+ startingThread = null;
+ }
+
+ @Override
+ public void onCreate(Connection connection) {
+ // skip a connection that the declarations create themselves, when
the one they use is lost meanwhile: they
+ // go on with the new one (as RabbitAdmin, which also skips it)
+ if (Thread.currentThread() == startingThread ||
!declaring.compareAndSet(false, true)) {
+ return;
+ }
+ try {
+ LOG.debug("Declaring the exchange, queue and binding of {} on
a new connection", getEndpoint());
+ getEndpoint().declareElements(container);
+ } catch (Exception e) {
+ // the connection may be shared with other consumers and
producers: do not fail it
+ LOG.warn("Cannot declare the exchange, queue and binding of {}
on a new connection. This exception is ignored.",
+ getEndpoint(), e);
+ } finally {
+ declaring.set(false);
+ }
+ }
+ }
+
}
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 317f3ba2a962..3b0b2dae1138 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
@@ -40,6 +40,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.AmqpAdmin;
+import org.springframework.amqp.core.Base64UrlNamingStrategy;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.Message;
@@ -727,6 +728,10 @@ public class SpringRabbitMQEndpoint extends
DefaultEndpoint implements AsyncEndp
public void declareElements(AbstractMessageListenerContainer container,
AmqpAdmin admin) {
if (admin != null && autoDeclare) {
+ // before the broker is called, so that a generated queue name is
set on the container also when the broker
+ // is not available
+ String queuesToDeclare = queuesToDeclare(container);
+
// bind dead letter exchange
if (deadLetterExchange != null) {
ExchangeBuilder eb = new ExchangeBuilder(deadLetterExchange,
deadLetterExchangeType);
@@ -773,16 +778,7 @@ public class SpringRabbitMQEndpoint extends
DefaultEndpoint implements AsyncEndp
admin.declareExchange(rabbitExchange);
}
- // if the consumer has no specific queue names then auto-create an
unique queue (auto deleted)
- String queuesToDeclare = queues;
String autoDeleteDefault = "false";
- boolean generateUniqueQueue = false;
- if (queuesToDeclare == null) {
- // no explicit queue names so use a single blank so we can
create a single new unique queue for the consumer
- queuesToDeclare = " ";
- generateUniqueQueue = true;
- }
-
for (String queue : queuesToDeclare.split(",")) {
queue = queue.trim();
Map<String, Object> map = getQueueArgs();
@@ -829,10 +825,6 @@ public class SpringRabbitMQEndpoint extends
DefaultEndpoint implements AsyncEndp
// 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);
- }
}
if (!skipBindQueue) {
// bind queue to exchange
@@ -845,6 +837,27 @@ public class SpringRabbitMQEndpoint extends
DefaultEndpoint implements AsyncEndp
}
}
+ /**
+ * The queues to declare. When no queues are configured, the consumer
consumes a queue with a generated name: the
+ * name is generated the first time and set on the listener container,
before the container starts, so that the
+ * container consumes that queue also when the broker is not available
yet, and every later declaration (on a new
+ * connection) declares the same queue again.
+ */
+ private String queuesToDeclare(AbstractMessageListenerContainer container)
{
+ if (queues != null) {
+ return queues;
+ }
+ if (container == null || skipDeclareQueue) {
+ // a single blank: the producer declares a queue with a generated
name (QueueBuilder)
+ return " ";
+ }
+ if (container.getQueueNames().length == 0) {
+ // the same naming strategy as QueueBuilder for a queue without a
name
+
container.setQueueNames(Base64UrlNamingStrategy.DEFAULT.generateName());
+ }
+ return String.join(",", container.getQueueNames());
+ }
+
private void prepareDeadLetterQueueArgs(Map<String, Object> args) {
if (deadLetterExchange != null) {
args.put(SpringRabbitMQConstants.DEAD_LETTER_EXCHANGE,
deadLetterExchange);
diff --git
a/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumerDeclareTest.java
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumerDeclareTest.java
new file mode 100644
index 000000000000..5dd50d157658
--- /dev/null
+++
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/SpringRabbitMQConsumerDeclareTest.java
@@ -0,0 +1,296 @@
+/*
+ * 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.ConnectException;
+import java.net.ServerSocket;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.FailedToStartRouteException;
+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.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.mockito.ArgumentCaptor;
+import org.springframework.amqp.AmqpConnectException;
+import org.springframework.amqp.core.AmqpAdmin;
+import org.springframework.amqp.core.Binding;
+import org.springframework.amqp.core.Exchange;
+import org.springframework.amqp.core.Queue;
+import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
+import org.springframework.amqp.rabbit.connection.Connection;
+import org.springframework.amqp.rabbit.connection.ConnectionFactory;
+import org.springframework.amqp.rabbit.connection.ConnectionListener;
+import
org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer;
+
+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.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * With {@code autoDeclare=true} (the default), the consumer declares the
exchange, queue and binding when it starts,
+ * and again on every new connection of the connection factory. When RabbitMQ
is not available, the consumer starts
+ * anyway, unless {@code testConnectionOnStartup} is enabled.
+ */
+class SpringRabbitMQConsumerDeclareTest {
+
+ private final List<AbstractMessageListenerContainer> containers = new
CopyOnWriteArrayList<>();
+ private CamelContext context;
+ private BrokerDownConnectionFactory brokerDown;
+
+ @BeforeEach
+ void setUp() {
+ context = new DefaultCamelContext();
+ }
+
+ @AfterEach
+ void tearDown() {
+ context.stop();
+ if (brokerDown != null) {
+ brokerDown.destroy();
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = { "DMLC", "SMLC" })
+ void consumerStartsWhileTheBrokerIsDown(String containerType) throws
Exception {
+ brokerDown();
+
addRoute("spring-rabbitmq:orders?connectionFactory=#cf&queues=orders&routingKey=new"
+ + "&messageListenerContainerType=" + containerType);
+
+ assertDoesNotThrow(() -> context.start());
+ assertEquals(ServiceStatus.Started,
context.getRouteController().getRouteStatus("orders"));
+ }
+
+ @Test
+ void consumerFailsToStartWhileTheBrokerIsDownWithTestConnectionOnStartup()
throws Exception {
+ brokerDown();
+
addRoute("spring-rabbitmq:orders?connectionFactory=#cf&queues=orders&routingKey=new&testConnectionOnStartup=true");
+
+ assertThrows(FailedToStartRouteException.class, () -> context.start());
+ }
+
+ @Test
+ void consumerFailsToStartWhileTheBrokerIsDownWhenConnectionsAreCached()
throws Exception {
+ // as RabbitAdmin, the consumer does not declare on new connections
with this cache mode: it declares on start only
+
brokerDown().setCacheMode(CachingConnectionFactory.CacheMode.CONNECTION);
+
addRoute("spring-rabbitmq:orders?connectionFactory=#cf&queues=orders&routingKey=new");
+
+ assertThrows(FailedToStartRouteException.class, () -> context.start());
+ }
+
+ @Test
+ void consumerDeclaresOnEveryNewConnection() throws Exception {
+ ConnectionFactory cf = connectedConnectionFactory();
+ AmqpAdmin admin = admin();
+
addRoute("spring-rabbitmq:orders?connectionFactory=#cf&queues=orders&routingKey=new&autoStartup=false");
+
+ context.start();
+
+ // once, although the connection factory called the listener when it
was added, as a connected one does
+ ConnectionListener listener = connectionListener(cf);
+ verify(admin).declareExchange(any(Exchange.class));
+ verify(admin).declareQueue(any(Queue.class));
+ verify(admin).declareBinding(any(Binding.class));
+
+ listener.onCreate(mock(Connection.class));
+ listener.onCreate(mock(Connection.class));
+
+ verify(admin, times(3)).declareExchange(any(Exchange.class));
+ ArgumentCaptor<Queue> queues = ArgumentCaptor.forClass(Queue.class);
+ verify(admin, times(3)).declareQueue(queues.capture());
+ assertEquals(List.of("orders", "orders", "orders"),
queues.getAllValues().stream().map(Queue::getName).toList());
+ verify(admin, times(3)).declareBinding(any(Binding.class));
+
+ context.getRouteController().stopRoute("orders");
+ verify(cf).removeConnectionListener(listener);
+ }
+
+ @Test
+ void consumerDeclaresTheQueueItGeneratedOnEveryNewConnection() throws
Exception {
+ ConnectionFactory cf = connectedConnectionFactory();
+ AmqpAdmin admin = admin();
+ // no queues: the consumer declares a queue with a generated name
+
addRoute("spring-rabbitmq:orders?connectionFactory=#cf&routingKey=new&autoStartup=false");
+
+ context.start();
+ connectionListener(cf).onCreate(mock(Connection.class));
+
+ ArgumentCaptor<Queue> queues = ArgumentCaptor.forClass(Queue.class);
+ verify(admin, times(2)).declareQueue(queues.capture());
+ String generated = queues.getAllValues().get(0).getName();
+ assertEquals(generated, queues.getAllValues().get(1).getName());
+ ArgumentCaptor<Binding> bindings =
ArgumentCaptor.forClass(Binding.class);
+ verify(admin, times(2)).declareBinding(bindings.capture());
+ assertEquals(List.of(generated, generated),
+
bindings.getAllValues().stream().map(Binding::getDestination).toList());
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = { "DMLC", "SMLC" })
+ void
consumerWithoutQueuesDeclaresTheSameQueueOnEveryNewConnectionWhenTheBrokerIsDownAtStart(String
containerType)
+ throws Exception {
+ BrokerDownConnectionFactory cf = brokerDown();
+ AmqpAdmin admin = admin();
+ // the declaration at start fails as the broker is down; it is up for
the next ones
+ doThrow(new AmqpConnectException(new ConnectException("Connection
refused"))).doNothing()
+ .when(admin).declareExchange(any(Exchange.class));
+ // no queues: the consumer consumes a queue with a generated name
+
addRoute("spring-rabbitmq:orders?connectionFactory=#cf&routingKey=new&messageListenerContainerType="
+ + containerType);
+
+ context.start();
+ assertEquals(ServiceStatus.Started,
context.getRouteController().getRouteStatus("orders"));
+ AbstractMessageListenerContainer container = containers.get(0);
+ // the running container consumes that queue already, so that it
connects to the broker by itself
+ String[] queueNamesAtStart = container.getQueueNames();
+
+ // the broker is up: each new connection declares the queue again
+ ConnectionListener listener = cf.connectionListener();
+ listener.onCreate(mock(Connection.class));
+ listener.onCreate(mock(Connection.class));
+
+ ArgumentCaptor<Queue> queues = ArgumentCaptor.forClass(Queue.class);
+ verify(admin, times(2)).declareQueue(queues.capture());
+ String generated = queues.getAllValues().get(0).getName();
+ assertTrue(generated.startsWith("spring.gen-"), generated);
+ // the same queue, with the flags of a generated queue: durable, not
auto-delete, not exclusive
+ for (Queue queue : queues.getAllValues()) {
+ assertEquals(generated, queue.getName());
+ assertTrue(queue.isDurable());
+ assertFalse(queue.isAutoDelete());
+ assertFalse(queue.isExclusive());
+ }
+ ArgumentCaptor<Binding> bindings =
ArgumentCaptor.forClass(Binding.class);
+ verify(admin, times(2)).declareBinding(bindings.capture());
+ assertEquals(List.of(generated, generated),
+
bindings.getAllValues().stream().map(Binding::getDestination).toList());
+ assertArrayEquals(new String[] { generated }, queueNamesAtStart);
+ assertArrayEquals(new String[] { generated },
container.getQueueNames());
+ }
+
+ @Test
+ void consumerWithoutAutoDeclareDoesNotListenToNewConnections() throws
Exception {
+ ConnectionFactory cf = connectedConnectionFactory();
+ AmqpAdmin admin = admin();
+
addRoute("spring-rabbitmq:orders?connectionFactory=#cf&queues=orders&routingKey=new&autoStartup=false"
+ + "&autoDeclare=false");
+
+ context.start();
+
+ verify(cf,
never()).addConnectionListener(any(ConnectionListener.class));
+ verify(admin, never()).declareQueue(any(Queue.class));
+ }
+
+ private BrokerDownConnectionFactory brokerDown() throws IOException {
+ // nothing listens on this port
+ brokerDown = new BrokerDownConnectionFactory(closedPort());
+ context.getRegistry().bind("cf", brokerDown);
+ DefaultListenerContainerFactory factory = new
DefaultListenerContainerFactory();
+ context.getComponent("spring-rabbitmq",
SpringRabbitMQComponent.class).setListenerContainerFactory(endpoint -> {
+ AbstractMessageListenerContainer container =
factory.createListenerContainer(endpoint);
+ // the container retries to connect meanwhile: a short interval so
that it stops quickly
+ container.setRecoveryInterval(100);
+ containers.add(container);
+ return container;
+ });
+ return brokerDown;
+ }
+
+ /**
+ * A connection factory that is connected: it calls a connection listener
when the listener is added, as
+ * {@link CachingConnectionFactory} does.
+ */
+ private ConnectionFactory connectedConnectionFactory() {
+ ConnectionFactory cf = mock(ConnectionFactory.class);
+ doAnswer(invocation -> {
+ invocation.getArgument(0,
ConnectionListener.class).onCreate(mock(Connection.class));
+ return null;
+ }).when(cf).addConnectionListener(any(ConnectionListener.class));
+ context.getRegistry().bind("cf", cf);
+ return cf;
+ }
+
+ private AmqpAdmin admin() {
+ AmqpAdmin admin = mock(AmqpAdmin.class);
+ when(admin.declareQueue(any(Queue.class))).thenAnswer(invocation ->
invocation.getArgument(0, Queue.class).getName());
+ context.getComponent("spring-rabbitmq",
SpringRabbitMQComponent.class).setAmqpAdmin(admin);
+ return admin;
+ }
+
+ private static ConnectionListener connectionListener(ConnectionFactory cf)
{
+ ArgumentCaptor<ConnectionListener> listener =
ArgumentCaptor.forClass(ConnectionListener.class);
+ verify(cf).addConnectionListener(listener.capture());
+ return listener.getValue();
+ }
+
+ private void addRoute(String from) throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from(from).routeId("orders").to("mock:orders");
+ }
+ });
+ }
+
+ /**
+ * A connection factory to a port where nothing listens, which keeps the
connection listeners added to it.
+ */
+ private static final class BrokerDownConnectionFactory extends
CachingConnectionFactory {
+
+ private final List<ConnectionListener> listeners = new
CopyOnWriteArrayList<>();
+
+ private BrokerDownConnectionFactory(int port) {
+ super("localhost", port);
+ }
+
+ @Override
+ public void addConnectionListener(ConnectionListener listener) {
+ super.addConnectionListener(listener);
+ listeners.add(listener);
+ }
+
+ private ConnectionListener connectionListener() {
+ assertEquals(1, listeners.size());
+ return listeners.get(0);
+ }
+ }
+
+ 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/RabbitMQConsumerBrokerStartsLaterIT.java
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerBrokerStartsLaterIT.java
new file mode 100644
index 000000000000..95a6a13dea3e
--- /dev/null
+++
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerBrokerStartsLaterIT.java
@@ -0,0 +1,138 @@
+/*
+ * 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.io.IOException;
+import java.net.ServerSocket;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
+import org.springframework.amqp.rabbit.core.RabbitAdmin;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * The broker is not reachable when Camel starts: the route starts, and the
consumer declares the exchange, queue and
+ * binding when its listener container connects, also when the queue has a
generated name (no queues option).
+ */
+public class RabbitMQConsumerBrokerStartsLaterIT extends RabbitMQITSupport {
+
+ private CachingConnectionFactory laterConnectionFactory;
+ private CachingConnectionFactory laterGeneratedConnectionFactory;
+ private CachingConnectionFactory testConnectionFactory;
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ // nothing listens on this port yet
+ laterConnectionFactory = new CachingConnectionFactory("localhost",
closedPort());
+ context.getRegistry().bind("laterCF", laterConnectionFactory);
+ laterGeneratedConnectionFactory = new
CachingConnectionFactory("localhost", closedPort());
+ context.getRegistry().bind("laterGeneratedCF",
laterGeneratedConnectionFactory);
+ return context;
+ }
+
+ @AfterEach
+ void destroyConnectionFactories() {
+ // stop the consumer before its connection factory
+ context.stop();
+ if (testConnectionFactory != null) {
+ testConnectionFactory.destroy();
+ }
+ if (laterConnectionFactory != null) {
+ laterConnectionFactory.destroy();
+ }
+ if (laterGeneratedConnectionFactory != null) {
+ laterGeneratedConnectionFactory.destroy();
+ }
+ }
+
+ @Test
+ public void testConsumerDeclaresWhenTheBrokerIsReachable() throws
Exception {
+ assertEquals(ServiceStatus.Started,
context.getRouteController().getRouteStatus("consumer"));
+
+ // the broker is reachable now
+ laterConnectionFactory.setHost(service.hostname());
+ laterConnectionFactory.setPort(service.port());
+
+ testConnectionFactory = (CachingConnectionFactory)
createConnectionFactory(false);
+ RabbitAdmin admin = new RabbitAdmin(testConnectionFactory);
+ await().atMost(60, TimeUnit.SECONDS).until(() -> consumers(admin,
uniqueName("orders-queue")) > 0);
+
+ getMockEndpoint("mock:result").expectedBodiesReceived("order 1");
+ new
RabbitTemplate(testConnectionFactory).convertAndSend(uniqueName("orders"),
"new", "order 1");
+
+ MockEndpoint.assertIsSatisfied(context, 30, TimeUnit.SECONDS);
+ }
+
+ @Test
+ public void testConsumerWithoutQueuesDeclaresWhenTheBrokerIsReachable() {
+ assertEquals(ServiceStatus.Started,
context.getRouteController().getRouteStatus("generated"));
+
+ // the broker is reachable now
+ laterGeneratedConnectionFactory.setHost(service.hostname());
+ laterGeneratedConnectionFactory.setPort(service.port());
+
+ // the queue has a generated name: send to the exchange until the
consumer declared, bound and consumes it
+ testConnectionFactory = (CachingConnectionFactory)
createConnectionFactory(false);
+ RabbitTemplate template = new RabbitTemplate(testConnectionFactory);
+ MockEndpoint generated = getMockEndpoint("mock:generated");
+ await().atMost(60, TimeUnit.SECONDS).pollInterval(1,
TimeUnit.SECONDS).until(() -> {
+ template.convertAndSend(uniqueName("generated"), "new", "order 1");
+ return generated.getReceivedCounter() > 0;
+ });
+ }
+
+ private static int consumers(RabbitAdmin admin, String queue) {
+ Properties properties = admin.getQueueProperties(queue);
+ return properties == null ? 0 : ((Number)
properties.get(RabbitAdmin.QUEUE_CONSUMER_COUNT)).intValue();
+ }
+
+ private static int closedPort() throws IOException {
+ try (ServerSocket socket = new ServerSocket(0)) {
+ return socket.getLocalPort();
+ }
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("spring-rabbitmq:" + uniqueName("orders") +
"?connectionFactory=#laterCF&queues="
+ + uniqueName("orders-queue") + "&routingKey=new")
+ .routeId("consumer")
+ .to("mock:result");
+
+ // no queues: the consumer consumes a queue with a generated
name
+ from("spring-rabbitmq:" + uniqueName("generated") +
"?connectionFactory=#laterGeneratedCF&routingKey=new")
+ .routeId("generated")
+ .to("mock:generated");
+ }
+ };
+ }
+}
diff --git
a/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerRedeclareIT.java
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerRedeclareIT.java
new file mode 100644
index 000000000000..a649d8899879
--- /dev/null
+++
b/components/camel-spring-parent/camel-spring-rabbitmq/src/test/java/org/apache/camel/component/springrabbit/integration/RabbitMQConsumerRedeclareIT.java
@@ -0,0 +1,155 @@
+/*
+ * 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.net.URI;
+import java.net.URLEncoder;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
+import org.springframework.amqp.rabbit.connection.ConnectionFactory;
+import org.springframework.amqp.rabbit.core.RabbitAdmin;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * A queue that the broker deletes when its consumer goes ({@code
arg.queue.autoDelete=true}) is declared again, with
+ * its binding, when the listener container reconnects after the broker closed
the connection.
+ */
+public class RabbitMQConsumerRedeclareIT extends RabbitMQITSupport {
+
+ private static final String CONNECTION_NAME = "camel-consumer";
+ private static final Pattern CONNECTION = Pattern.compile("\\{[^{}]*}");
+ private static final Pattern NAME =
Pattern.compile("\"name\":\"([^\"]+)\"");
+
+ private final HttpClient http = HttpClient.newHttpClient();
+ // the test publishes and checks the queue with its own connection
factory, so the consumer is alone on Camel's
+ private ConnectionFactory testConnectionFactory;
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ context.getRegistry().lookupByNameAndType("myCF",
CachingConnectionFactory.class)
+ .setConnectionNameStrategy(cf -> CONNECTION_NAME);
+ return context;
+ }
+
+ @AfterEach
+ void destroyTestConnectionFactory() {
+ if (testConnectionFactory != null) {
+ ((CachingConnectionFactory) testConnectionFactory).destroy();
+ }
+ }
+
+ @Test
+ public void testAutoDeleteQueueIsDeclaredAgainAfterTheConnectionIsClosed()
throws Exception {
+ String exchange = uniqueName("news");
+ String queue = uniqueName("instance");
+ testConnectionFactory = createConnectionFactory(false);
+ RabbitTemplate rabbitTemplate = new
RabbitTemplate(testConnectionFactory);
+ RabbitAdmin admin = new RabbitAdmin(testConnectionFactory);
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("news 1", "news 2");
+
+ rabbitTemplate.convertAndSend(exchange, "", "news 1");
+ await().atMost(30, TimeUnit.SECONDS).until(() ->
mock.getReceivedCounter() == 1);
+
+ closeConnections(CONNECTION_NAME);
+
+ // the broker deletes the queue, as its consumer is gone
+ await().atMost(30, TimeUnit.SECONDS).pollInterval(100,
TimeUnit.MILLISECONDS)
+ .until(() -> admin.getQueueProperties(queue) == null);
+ // the listener container reconnects: the queue is declared and bound
again, and consumed
+ await().atMost(60, TimeUnit.SECONDS).until(() -> consumers(admin,
queue) > 0);
+
+ rabbitTemplate.convertAndSend(exchange, "", "news 2");
+
+ MockEndpoint.assertIsSatisfied(context, 30, TimeUnit.SECONDS);
+ }
+
+ private static int consumers(RabbitAdmin admin, String queue) {
+ Properties properties = admin.getQueueProperties(queue);
+ return properties == null ? 0 : ((Number)
properties.get(RabbitAdmin.QUEUE_CONSUMER_COUNT)).intValue();
+ }
+
+ /**
+ * Closes the connections with the given name with the management API of
RabbitMQ.
+ */
+ private void closeConnections(String connectionName) throws Exception {
+ List<String> names = new ArrayList<>();
+ // the management API lists a connection shortly after it is opened
+ await().atMost(30, TimeUnit.SECONDS).until(() -> {
+ names.clear();
+ String json = send(HttpRequest.newBuilder(
+ URI.create(service.managementUri() +
"/api/connections?columns=name,user_provided_name")).GET());
+ Matcher connection = CONNECTION.matcher(json);
+ while (connection.find()) {
+ String fields = connection.group();
+ Matcher name = NAME.matcher(fields);
+ if (fields.contains("\"user_provided_name\":\"" +
connectionName + "\"") && name.find()) {
+ names.add(name.group(1));
+ }
+ }
+ return !names.isEmpty();
+ });
+ for (String name : names) {
+ String path = URLEncoder.encode(name,
StandardCharsets.UTF_8).replace("+", "%20");
+ send(HttpRequest.newBuilder(URI.create(service.managementUri() +
"/api/connections/" + path)).DELETE());
+ }
+ }
+
+ private String send(HttpRequest.Builder request) throws Exception {
+ String credentials = service.managementUsername() + ":" +
service.managementPassword();
+ HttpResponse<String> response = http.send(
+ request.header("Authorization",
+ "Basic " +
Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharsets.UTF_8)))
+ .build(),
+ HttpResponse.BodyHandlers.ofString());
+ assertEquals(2, response.statusCode() / 100, response.body());
+ return response.body();
+ }
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("spring-rabbitmq:" + uniqueName("news") +
"?exchangeType=fanout&queues=" + uniqueName("instance")
+ + "&arg.queue.autoDelete=true")
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index f1e4eecc6569..080f47d0a546 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -491,6 +491,26 @@ file consumer no longer adds the key to the idempotent
repository eagerly, and `
read lock adds it when it acquires the lock. Since Camel 4.7 the read lock was
refused, and such a consumer never
consumed a file.
+=== camel-spring-rabbitmq - the consumer starts while RabbitMQ is down, and
declares again on a new connection
+
+With `autoDeclare=true` (the default), a consumer now starts when RabbitMQ is
not available: the route shows as started,
+the consumer logs a warning, its listener container keeps trying to connect,
and the exchange, queue and binding are
+declared when it connects. Before, the declaration failed with an
`AmqpConnectException` and the route did not start.
+If you relied on that failure, for example so that the supervising route
controller retries to start the route, or so
+that Camel does not start while RabbitMQ is down, set
`testConnectionOnStartup=true` on the endpoint or on the
+component: the consumer then fails to start while RabbitMQ is not available,
as before.
+
+The consumer now also declares the exchange, queue and binding again each time
the connection factory opens a new
+connection, as Spring's `RabbitAdmin` does, so a queue that RabbitMQ deleted
while the connection was lost (an
+auto-delete or exclusive queue, for example) is declared again when the
consumer reconnects. Before, they were declared
+only when the consumer started. A consumer without the `queues` option picks
the generated name of its queue when it
+starts, before it connects, and declares that same queue again on a new
connection.
+
+With a `CachingConnectionFactory` whose cache mode is `CONNECTION`, the
consumer behaves as before: it declares only
+when it starts, and does not start while RabbitMQ is down.
+
+See
xref:components::spring-rabbitmq-component.adoc#_auto_declare_exchanges_queues_and_bindings[Auto
declare exchanges, queues and bindings].
+
=== camel-xslt, camel-xslt-saxon - transformerFactoryConfigurationStrategy and
secureProcessing are applied in all configurations
The `xslt` component now calls the `transformerFactoryConfigurationStrategy`
of the endpoint or the component also when