Croway opened a new pull request, #30051:
URL: https://github.com/apache/camel/pull/30051

   ## Problem
   
   [CAMEL-26990](https://issues.apache.org/jira/browse/CAMEL-26990)
   
   With `autoDeclare=true` (the default), the consumer declared the exchange, 
queue and binding once, in `doStart`, before it started the Spring listener 
container:
   
   1. **The route did not start while RabbitMQ was down.** 
`RabbitAdmin.declareExchange` threw `AmqpConnectException` and the CamelContext 
failed with `FailedToStartRouteException`, with both container types (DMLC, 
SMLC). With `autoDeclare=false` the same route starts and the container keeps 
retrying. Failing the start is what `testConnectionOnStartup` (off by default) 
is for. Spring AMQP itself does not fail: the container logs "Broker not 
available; cannot force queue declarations during start" and keeps going.
   2. **The declarations were never made again.** A queue that the broker 
deletes when the connection goes (`arg.queue.autoDelete=true`, 
`arg.queue.exclusive=true`) was not declared again after the container 
reconnected: the consumer logged "Queue not present, scheduling consumer ... 
for restart" forever, and messages published to the exchange were dropped. Yet 
the consumer logs, when it declares such a queue, "It will be redeclared if the 
broker stops and is restarted while the connection factory is alive". The 
`RabbitAdmin` the component creates registers no `ConnectionListener` (it is 
never initialised), and the container only redeclares beans of a Spring 
`ApplicationContext`, which the component does not have.
   
   ## Fix
   
   - While it runs, the consumer registers a `ConnectionListener` on the 
connection factory of its listener container and declares the exchange, queue 
and binding again on every new connection, as `RabbitAdmin` does for its 
declarables. The listener is removed when the consumer stops (or fails to 
start).
   - At start, the consumer still declares by itself, so a mismatched 
declaration still fails the start as before. When that declaration fails with 
`AmqpConnectException` (broker not reachable), the consumer logs a WARN and 
starts the listener container, which keeps trying to connect; the listener 
declares when it connects. With `testConnectionOnStartup=true`, the exception 
is rethrown as before.
   - No double declaration at start: the listener ignores the connection the 
starting thread creates, and the immediate call a connected 
`CachingConnectionFactory` makes when a listener is added. An `AtomicBoolean` 
skips a connection that the declarations themselves create (as `RabbitAdmin` 
does).
   - A declaration that fails on a new connection is logged as a WARN and not 
rethrown, so that it does not fail a connection that other consumers and 
producers may share.
   - When no `queues` is set, `SpringRabbitMQEndpoint.declareElements` 
generated a new queue name on each call; when the container already consumes a 
queue, its name is now declared again instead (DMLC also refuses 
`setQueueNames` while running).
   - A `CachingConnectionFactory` with cache mode `CONNECTION` keeps the former 
behaviour (declared on start only, start fails while the broker is down), as 
`RabbitAdmin` does not declare on new connections with that cache mode either.
   
   Docs: the "Auto declare exchanges, queues and bindings" section said the 
feature must be enabled with `autoDeclare=true`, but it is on by default; it 
now describes the default, the producer option, the redeclaration and the 
behaviour while RabbitMQ is down (catalog copy regenerated). Upgrade guide 
4.24: new entry, as a consumer now starts while RabbitMQ is down.
   
   ## Tests
   
   - `SpringRabbitMQConsumerDeclareTest` (no broker):
     - `consumerStartsWhileTheBrokerIsDown` (DMLC, SMLC): the connection 
factory points at a port where nothing listens; the route starts.
     - `consumerDeclaresOnEveryNewConnection`: with a connection factory that 
calls the listener when it is added (as a connected `CachingConnectionFactory` 
does), the consumer declares once at start, again on each new connection, and 
removes the listener when the route stops.
     - `consumerDeclaresTheQueueItGeneratedOnEveryNewConnection`: without 
`queues`, the same generated queue is declared and bound again.
     - Controls that pass before and after: 
`consumerFailsToStartWhileTheBrokerIsDownWithTestConnectionOnStartup`, 
`consumerFailsToStartWhileTheBrokerIsDownWhenConnectionsAreCached`, 
`consumerWithoutAutoDeclareDoesNotListenToNewConnections`.
   - `RabbitMQConsumerBrokerStartsLaterIT` (test-infra RabbitMQ): the 
consumer's connection factory first points at a closed port; the route starts; 
once it points at the broker, the queue is declared and consumed.
   - `RabbitMQConsumerRedeclareIT` (test-infra RabbitMQ): a fanout exchange 
with an auto-delete queue; the consumer's connection is closed on the broker 
with the management API; the broker deletes the queue; the container reconnects 
and the queue is declared, bound and consumed again.
   
   Without the change, the 4 new unit tests (the controls aside) and both ITs 
fail (`FailedToStartRouteException` caused by `AmqpConnectException`; the queue 
is not declared again within 60 s). The module's unit tests (28) and 
integration tests (37, 4 skipped as on `main`) pass with it.
   
   ## Open questions for reviewers
   
   - A declaration that fails on a new connection is logged and not rethrown. 
`RabbitAdmin` rethrows it from its own listener, which fails the 
`createConnection` call of whoever opened the connection, possibly another 
route's producer on a shared factory. Logging seemed the safer choice; happy to 
rethrow if preferred.
   - Only `AmqpConnectException` counts as "broker not available" at start. An 
unknown host (`AmqpIOException`), an authentication failure or a timeout still 
fail the start, as today. Spring's container treats any exception from its 
connection check as "broker not available".
   - With a `CachingConnectionFactory` in `CONNECTION` cache mode, nothing 
changes (no redeclaration, start still fails while the broker is down), as 
`RabbitAdmin` does not support it.
   - The listener is added through `ConnectionFactory.addConnectionListener`, 
which `AbstractConnectionFactory` also forwards to the publisher connection 
factory, so a new publisher connection also triggers a declaration (as with 
`RabbitAdmin`). This is harmless, since the declarations are idempotent.
   - Out of scope: the producer with `autoDeclareProducer=true` still declares 
once, in `doStart` (`lazyStartProducer=true` avoids the start failure). A queue 
deleted while the connection stays up (for example by an operator) is not 
declared again, as with plain Spring AMQP without an `ApplicationContext`.
   
   Related: CAMEL-26989 (`ignoreDeclarationExceptions=true` binds a null queue 
name). The two branches merge without conflicts; together, a consumer with 
`ignoreDeclarationExceptions=true` also starts while the broker is down and 
declares once it connects.
   
   _Claude Code on behalf of Croway_
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to