Home > Mobile >  Several queueNames for Rabbit in Spring
Several queueNames for Rabbit in Spring

Time:10-29

Here is my rabbit yml.config of Spring app:

event.rabbit:
  host: {{ params.app_properties.mq_host }}
  port: {{ params.app_properties.mq_port }}
  username: ${mq_username}
  password: ${mq_password}
  virtual-host: {{ params.app_properties.mq_vhost }}
  exchange.name: {{ params.app_properties.mq_exchange }}
  queue.name: {{ params.app_properties.event_rabbit_queue_name}}

Is it possible to make rabbit listen to several queues of several hosts?

CodePudding user response:

The ListenerContainerFactoryBean (as a top-level) abstraction in Spring AMQP, can be configured for several queues to consume:

public void setQueueNames(String... queueName) {
    this.queueNames = queueName;
}

public void setQueues(Queue... queues) {
    this.queues = queues;
}

Several hosts can be provided on the AbstractConnectionFactory:

/**
 * Set addresses for clustering.
 * This property overrides the host port properties if not empty.
 * @param addresses list of addresses with form "host[:port],..."
 */
public synchronized void setAddresses(String addresses) {

But as you see all of them has to be in cluster and therefore all those queues must be mirrored on all those cluster members for fault-tolerant behavior of your solution.

Doesn't looks like the properties you show are related to Spring Boot auto-configuration somehow but here is the respective RabbitProperties to investigate.

NOTE: there is not need to know an exchange if you just implement a consumer side. You always consume from the specific queue. An exchange is a matter of producer how to distribute and deliver a message you send.

CodePudding user response:

I've done that with MQ, but never with Rabbit. It seems likely that the MQ solution will still apply to Rabbit.

  1. Create a different ConnectionFactory for each server (use your own configuration property setup, not the default Rabbit).
  2. Create a ContainerFactory for each server, refer to the connectionFactory in the listener annotation. Manually the connectionFactory in the containerFactory to the "correct" connectionFactory (as defined by the correct server).
  3. Refer to your listen-to queue name in the listener annotation.
  4. Create a jmsTemplate for each server, as with the containerFactory, manually set the connectionFactory in the jmsTemplate to the "correct" value. Also, set a unique qualifier value for each jmsTemplate.
  5. When you inject the JmsTemplate, refer to the qualifier.
  • Related