Dynamically add new queues, bindings and exchanges

2020-02-05 19:38发布

问题:

I'm currently working on a rabbit-amqp implementation project and use spring-rabbit to programmatically setup all my queues, bindings and exchanges. (spring-rabbit-1.3.4 and spring-framework versions 3.2.0)

The declaration in a javaconfiguration class or xml-based configuration are both quite static in my opinion declared. I know how to set a more dynamic value (ex. a name) for a queue, exchange or binding like this:

@Configuration
public class serverConfiguration {
   private String queueName;
   ...
   @Bean
   public Queue buildQueue() {
    Queue queue = new Queue(this.queueName, false, false, true, getQueueArguments());
    buildRabbitAdmin().declareQueue(queue);
    return queue;
   }
   ...
}

But I was wondering if it was possible to create a undefined amount instances of Queue and register them as beans like a factory registering all its instances.

I'm not really familiar with the Spring @Bean annotation and its limitations, but I tried

@Configuration
public class serverConfiguration {
   private String queueName;
   ...
   @Bean
   @Scope("prototype")
   public Queue buildQueue() {
    Queue queue = new Queue(this.queueName, false, false, true, getQueueArguments());
    buildRabbitAdmin().declareQueue(queue);
    return queue;
   }
   ...
}

And to see if the multiple beans instances of Queue are registered I call:

Map<String, Queue> queueBeans = ((ListableBeanFactory) applicationContext).getBeansOfType(Queue.class);

But this will only return 1 mapping:

name of the method := the last created instance.

Is it possible to dynamically add beans during runtime to the SpringApplicationContext?

回答1:

You can add beans dynamically to the context:

context.getBeanFactory().registerSingleton("foo", new Queue("foo"));

but they won't be declared by the admin automatically; you will have to call admin.initialize() to force it to re-declare all the AMQP elements in the context.

You would not do either of these in @Beans, just normal runtime java code.