[ 
https://issues.apache.org/jira/browse/CAMEL-26990?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Federico Mariani reassigned CAMEL-26990:
----------------------------------------

    Assignee: Federico Mariani

> camel-spring-rabbitmq - the consumer declares exchange, queue and binding 
> only once, at start: the route does not start while RabbitMQ is down, and a 
> queue the broker deleted is not declared again after a reconnect
> ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: CAMEL-26990
>                 URL: https://issues.apache.org/jira/browse/CAMEL-26990
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-spring-rabbitmq
>    Affects Versions: 4.18.5, 4.22.2, 4.23.0
>            Reporter: Federico Mariani
>            Assignee: Federico Mariani
>            Priority: Major
>
> With the default {{autoDeclare=true}}, 
> {{SpringRabbitMQConsumer.prepareAndStartListenerContainer}} calls 
> {{SpringRabbitMQEndpoint.declareElements}} before it starts the Spring 
> listener container. The exchange, queue and binding are declared through a 
> {{RabbitAdmin}} once, when the consumer starts. Two consequences:
> *1. The route does not start while the broker is unreachable.* 
> {{RabbitAdmin.declareExchange}} throws {{AmqpConnectException}}, the consumer 
> fails, and the CamelContext fails to start:
> {noformat}
> org.apache.camel.FailedToStartRouteException: Failed to start route: route 
> because: java.net.ConnectException: Connection refused
> Caused by: org.springframework.amqp.AmqpConnectException: 
> java.net.ConnectException: Connection refused
>     at 
> org.springframework.amqp.rabbit.core.RabbitAdmin.declareExchange(RabbitAdmin.java:237)
>     at 
> org.apache.camel.component.springrabbit.SpringRabbitMQEndpoint.declareElements(SpringRabbitMQEndpoint.java:740)
>     at 
> org.apache.camel.component.springrabbit.SpringRabbitMQEndpoint.declareElements(SpringRabbitMQEndpoint.java:691)
>     at 
> org.apache.camel.component.springrabbit.SpringRabbitMQConsumer.prepareAndStartListenerContainer(SpringRabbitMQConsumer.java:152)
>     at 
> org.apache.camel.component.springrabbit.SpringRabbitMQConsumer.doStart(SpringRabbitMQConsumer.java:105)
> {noformat}
> This happens with both listener container types (DMLC, SMLC). With 
> {{autoDeclare=false}} the same route starts and the container keeps retrying 
> until the broker is back. Failing the start is what the separate option 
> {{testConnectionOnStartup}} is for ("If a connection cannot be granted then 
> Camel throws an exception on startup"), and it is off by default; the default 
> {{autoDeclare=true}} turns every consumer into a fail-fast one. Spring AMQP 
> itself does not fail here: the listener container logs "Broker not available; 
> cannot force queue declarations during start" 
> ({{AbstractMessageListenerContainer.checkMismatchedQueues}}) and keeps going, 
> and {{RabbitAdmin}} declares its declarables from a {{ConnectionListener}} 
> each time a connection is created ({{RabbitAdmin.afterPropertiesSet}}). The 
> former camel-rabbitmq consumer started in the background when the connection 
> failed (CAMEL-8742).
> *2. The declarations are not made again after a reconnect.* For a queue the 
> broker deletes when the connection goes ({{arg.queue.autoDelete=true}}, or 
> {{arg.queue.exclusive=true}}), the listener container reconnects by itself, 
> but nothing declares the queue again. The consumer then logs forever:
> {noformat}
> ERROR CamelDirectMessageListenerContainer - Queue not present, scheduling 
> consumer SimpleConsumer [queue=instance-1, ...] for restart
> {noformat}
> and the messages published to the exchange in the meantime are dropped (there 
> is no binding). 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" ({{SpringRabbitMQEndpoint.declareElements}}). The 
> {{RabbitAdmin}} the component creates ({{DefaultListenerContainerFactory}}) 
> is not initialised with {{afterPropertiesSet()}}, so it registers no 
> {{ConnectionListener}}, and does not set {{redeclareManualDeclarations}}; the 
> container's own {{redeclareElementsIfNecessary}} only redeclares the beans of 
> a Spring {{ApplicationContext}}, which the component does not have.
> The producer with {{autoDeclareProducer=true}} (off by default) declares in 
> {{doStart}} as well and fails the route start the same way; 
> {{lazyStartProducer=true}} avoids that.
> The "Auto declare exchanges, queues and bindings" section of the component 
> documentation says that the feature must be enabled with 
> {{autoDeclare=true}}, but it is on by default.
> *Steps to reproduce*
> * Broker down: point the connection factory at a host and port where nothing 
> listens, start a route 
> {{from("spring-rabbitmq:orders?queues=orders&routingKey=new")}}: 
> {{FailedToStartRouteException}} as above. Start Camel first and RabbitMQ 
> afterwards: Camel does not start.
> * Reconnect: start a route 
> {{from("spring-rabbitmq:news?exchangeType=fanout&queues=instance-1&arg.queue.autoDelete=true")}},
>  check that a message published to {{news}} is consumed, then close the 
> connections on the broker ({{rabbitmqctl close_all_connections}}). The 
> container reconnects, the queue {{instance-1}} is not declared again, and no 
> message is consumed any more.
> *Expected*
> * With the default options, Camel starts while RabbitMQ is down; the consumer 
> declares the exchange, queue and binding once it gets a connection and then 
> consumes. {{testConnectionOnStartup=true}} keeps failing the start.
> * After a reconnect the declarations are made again, so an auto-delete or 
> exclusive queue comes back.
> *Actual*
> * The CamelContext fails to start with {{FailedToStartRouteException}} caused 
> by {{AmqpConnectException}}.
> * After a reconnect an auto-delete queue is gone for good and the consumer 
> receives nothing.
> Reproduced on 4.23.0 and 4.24.0-SNAPSHOT; the code dates from the component's 
> first version (CAMEL-16003) and is the same in 4.18.x and 4.22.x.
> *Fix direction*
> * Declare again from a {{ConnectionListener}} registered on the endpoint's 
> connection factory while the consumer runs, as {{RabbitAdmin}} does for its 
> declarables, so the declarations are made on every new connection.
> * At start, a declaration that fails because the broker is not reachable 
> ({{AmqpConnectException}}) is logged and left to that listener, unless 
> {{testConnectionOnStartup=true}}.
> * When no {{queues}} is set, {{declareElements}} generates a new queue name 
> on each call; declaring again must reuse the name the container listens on.
> * Fix the documentation of auto declare.
> Workarounds today: the supervising route controller retries the route start 
> until the broker is back (startup only); {{autoDeclare=false}} with the 
> topology declared elsewhere; for the reconnect part, a component 
> {{amqpAdmin}} that is a {{RabbitAdmin}} with 
> {{redeclareManualDeclarations=true}}, initialised with 
> {{afterPropertiesSet()}}.
> Related: CAMEL-8742 (camel-rabbitmq consumer did not retry if the connection 
> failed), CAMEL-16003 (rework using Spring AMQP).
> _Claude Code on behalf of Croway_



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to